Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose we have video file some_video.

How to get length of it from a shell script (with mplayer/transcode/gstreamer/vlc/ffmpeg/whatever)?

VIDEO_LENGTH_IN_SECONDS=`ffmpeg .... -i some_video ... | grep -o .....`
share|improve this question
up vote 9 down vote accepted
ffprobe -i some_video -show_entries format=duration -v quiet -of csv="p=0"

will return the video duration in seconds.

share|improve this answer

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

share|improve this answer
    
Also tcprobe is desiged for it, but it doesn't work well on my system. – Vi. Nov 25 '11 at 16:33
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 Apr 21 '13 at 19:20
    
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 Apr 21 '13 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .