4

Every answer I've found to get a video's duration with ffmpeg contains grep, which cmd doesn't natively have.

I know there's a "Grep for windows" thing, but I'd rather use a function that comes with the command line.

So when I do ffmpeg -i input I get

Input #0, flv, from 'vid_test001.flv':
  Metadata:
    creationdate    : Fri Sep 19 15:58:17
  Duration: 00:00:12.44, start: 0.000000, bitrate: 806 kb/s
    Stream #0:0: Video: flv1, yuv420p, 320x240, 1k tbr, 1k tbn, 1k tbc
    Stream #0:1: Audio: nellymoser, 22050 Hz, mono, flt

I'd like to to get the Duration from this, which is "00:00:12.44" in this case

3
  • 1
    Install Cygwin. Sep 24, 2014 at 19:18
  • You can pipe command output in cmd just like you do in unix shell. And there are some commands like find, findstr that may mimick grep to some extent. Otherwise, get cygwin as @DanielRHicks suggested. Sep 24, 2014 at 20:30
  • I'm looking for "a function that comes with the command line".
    – user229061
    Sep 24, 2014 at 21:56

2 Answers 2

4
ffmpeg -i "inputFile" 2>&1 | for /f "tokens=2 delims=, " %a in ('findstr /r /c:"^ *Duration:"') do echo %a

Execute ffmpeg on the input file, sending the output of stderr (where ffmpeg writes its information by default) to the standard output. Filter the list with findstr to retrieve the lines that begin with spaces followed by Duration:. For each line found, spaces and commas as considered as field delimiters and the second token in the line is requested, that is, the time after the Duration: label. This value is retrieved into the for replaceable parameter %a that is echoed to console.

To use from batch file, escape percent signs, replacing % with %%

2
  • I think you are missing a closing ' at: ('findstr /r /c:"^ *Duration:"). I think it should be ('findstr /r /c:"^ *Duration:"')
    – cliffclof
    Nov 11, 2016 at 2:14
  • @cliffclof, of course you are right. Answer updated, thank you
    – MC ND
    Nov 11, 2016 at 6:52
1

If you're not opposed to using Powershell (comes standard in recent versions of Windows), you can make use of the Select-String command.

Here's a great post from another Superuser question - link

Basically, you can take the output of the command and pipe the results to select-string.