Sunday, November 2, 2008

One Liner: Determine Length of Video Clip in Seconds or Frames

Using mplayer you can extract all sorts of interesting details about your video clip:
mplayer -identify -frames 0 video.avi

which, among other stuff, pukes out lines looking like this:

...
ID_FILENAME=video.avi
ID_DEMUXER=avi
ID_VIDEO_FORMAT=MP42
ID_VIDEO_BITRATE=1840648
ID_VIDEO_WIDTH=854
ID_VIDEO_HEIGHT=480
ID_VIDEO_FPS=24.000
ID_VIDEO_ASPECT=0.0000
ID_AUDIO_FORMAT=85
ID_AUDIO_BITRATE=245736
ID_AUDIO_RATE=0
ID_AUDIO_NCH=0
ID_LENGTH=596.46
ID_SEEKABLE=1
...
ID_VIDEO_CODEC=ffmp42
...
ID_AUDIO_BITRATE=160000
ID_AUDIO_RATE=48000
ID_AUDIO_NCH=2
...
ID_AUDIO_CODEC=mp3
...

In our case the important line is the one starting with ID_LENGTH, so that the complete one-liner is:
$ mplayer -identify -frames 0 video.avi 2>&1 | grep ID_LENGTH | sed s/ID_LENGTH=//
596.46

To extract the number of frames, you need to set the frame rate to one frame per second (stumbled upon this trick at Yahoo Answers):
$ mplayer -identify -fps 1 -frames 0 video.avi 2>&1 | grep ID_LENGTH | sed s/ID_LENGTH=//
14315.00

No comments:

Post a Comment