How can I get the length of a video file from the console?

26

5

Suppose we have a video file some_video.

How can I get its length from a shell script (with mplayer/transcode/gstreamer/vlc/ffmpeg/whatever)?

VIDEO_LENGTH_IN_SECONDS=`ffmpeg .... -i some_video ... | grep -o .....`

Vi.

Posted 2011-11-25T12:13:10.013

Reputation: 13 705

Answers

35

ffprobe -i some_video -show_entries format=duration -v quiet -of csv="p=0"

will return the video duration in seconds.

John Lemberger

Posted 2011-11-25T12:13:10.013

Reputation: 520

Did not know about ffprobe, thanks!

– ckujau – 2018-01-22T02:02:36.593

21

Something similar to:

ffmpeg -i input 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,//

This will deliver: HH:MM:SS.ms. You can also use ffprobe, which is supplied with most FFmpeg installations:

ffprobe -show_format input | sed -n '/duration/s/.*=//p'

… or:

ffprobe -show_format input | grep duration | sed 's/.*=//')

To convert into seconds (and retain the milliseconds), pipe into:

awk '{ split($1, A, ":"); print 3600*A[1] + 60*A[2] + A[3] }'

To convert it into milliseconds, pipe into:

awk '{ split($1, A, ":"); print 3600000*A[1] + 60000*A[2] + 1000*A[3] }'

If you want just the seconds without the milliseconds, pipe into:

awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }'

Example:

enter image description here

slhck

Posted 2011-11-25T12:13:10.013

Reputation: 182 472

Also tcprobe is desiged for it, but it doesn't work well on my system. – Vi. – 2011-11-25T16:33:43.767

1...my edit was rejected, so I'll post here that the first step can be more concisely accomplished with ffprobe, a tool designed for exactly these sort of purposes that is packaged with ffmpeg: ffprobe -show_format input | sed -n '/duration/s/.*=//p' (or ffprobe -show_format input | grep duration | sed 's/.*=//'). Maybe @slhck can edit this straight into the answer. – evilsoup – 2013-04-21T19:20:22.127

Sorry about that, @evilsoup. Maybe I should make a disclaimer that you and LordNeckbeard are allowed to freely edit my posts—I've had this problem a few times already. Next time just add a little note to the edit message or so :) – slhck – 2013-04-21T19:45:39.237

0

In case you don't have access to ffprobe, you could use mediainfo.

# Outputs a decimal number in seconds
mediainfo some_video --Output=JSON | jq '.media.track[0].Duration' | tr -d '"'`

ToBeReplaced

Posted 2011-11-25T12:13:10.013

Reputation: 63

1Eliminate the need for jq and tr: mediainfo --Output="General;%Duration/String%" input – llogan – 2019-03-20T17:45:37.757

Neat! I'm going to leave my answer unedited for now because the output of your command is of the form X s YYY ms versus X.YYY. Easy enough to adjust with | sed -e 's/ s /./' -e 's/ ms//' if you want to go that route and do not have access to jq. – ToBeReplaced – 2019-03-21T04:49:37.663

That can be changed with mediainfo --Output="General;%Duration/String3%" input to output 00:01:48.501 instead of 1 min 48 s. – llogan – 2019-03-21T16:53:44.480