How to extract duration time from ffmpeg output?

Louise picture Louise · Jun 4, 2011 · Viewed 100.1k times · Source

To get a lot of information about a media file one can do

ffmpeg -i <filename>

where it will output a lot of lines, one in particular

Duration: 00:08:07.98, start: 0.000000, bitrate: 2080 kb/s

I would like to output only 00:08:07.98, so I try

ffmpeg -i file.mp4 | grep Duration| sed 's/Duration: \(.*\), start/\1/g'

But it prints everything, and not just the length.

Even ffmpeg -i file.mp4 | grep Duration outputs everything.

How do I get just the duration length?

Answer

Ivan Neeson picture Ivan Neeson · Mar 7, 2014

You can use ffprobe:

ffprobe -i <file> -show_entries format=duration -v quiet -of csv="p=0"

It will output the duration in seconds, such as:

154.12

Adding the -sexagesimal option will output duration as hours:minutes:seconds.microseconds:

00:02:34.12