Home » Questions » Computers [ Ask a new question ]

How do I get the number of frames in a video on the linux command line?

How do I get the number of frames in a video on the linux command line?

I have a video file and I want to get the number of video frames that are in it. I can use ffmpeg to get the length of the video and the FPS. However I can't see anything obvious for the total number of frames.

Asked by: Guest | Views: 67
Total answers/comments: 5
Guest [Entry]

"This is horrible, and stupid, and slow, but seems to work:

ffmpeg -i foo.avi -vcodec copy -f rawvideo -y /dev/null 2>&1 | tr ^M '\n' | awk '/^frame=/ {print $2}'|tail -n 1

It will also work right on truncated files and raw streams(that is why you get nothing for .vob files)"
Guest [Entry]

"I posted this on another question. Using the tcprobe tool (from the transcode package), the number of frames is included in the info. Use the -i switch to get an info dump from the file:

$ tcprobe -i foo.avi
[tcprobe] RIFF data, AVI video
[avilib] V: 29.970 fps, codec=XVID, frames=38630, width=512, height=384
[avilib] A: 48000 Hz, format=0x55, bits=16, channels=2, bitrate=128 kbps,
[avilib] 53707 chunks, 21768720 bytes, VBR
[tcprobe] summary for foo.avi, (*) = not default, 0 = not detected
import frame size: -g 512x384 [720x576] (*)
frame rate: -f 29.970 [25.000] frc=4 (*)
audio track: -a 0 [0] -e 48000,16,2 [48000,16,2] -n 0x55 [0x2000] (*)
bitrate=128 kbps
length: 38630 frames, frame_time=33 msec, duration=0:21:28.954

Note the number of frames is given on two lines here (2nd output line and last output line)."
Guest [Entry]

"ffprobe -select_streams v -show_frames -count_frames INPUT_FILE | grep pkt_duration_time=

Add up the duration. Could be fancier with sed/awk and what not.

From our testing I can say that for now it has shown to be the best most reliable. You get a precise framecount and exact duration. Even with variable framerate which all other tools like mediainfo seem to go gaga."
"ffprobe -select_streams v -show_frames -count_frames INPUT_FILE | grep pkt_duration_time=

Add up the duration. Could be fancier with sed/awk and what not.

From our testing I can say that for now it has shown to be the best most reliable. You get a precise framecount and exact duration. Even with variable framerate which all other tools like mediainfo seem to go gaga."
Guest [Entry]

"I've found that the number of frames is actually twice the fps*duration (no idea why, I'd be happy know).

In a script of mine, I have:

# Get duration and fps
duration=$($FFMPEG -i $input 2>&1 | sed -n ""s/.* Duration: \([^,]*\), start: .*/\1/p"")
fps=$($FFMPEG -i $input 2>&1 | sed -n ""s/.*, \(.*\) tb.*/\1/p"")

hours=$(echo $duration | cut -d"":"" -f1)
minutes=$(echo $duration | cut -d"":"" -f2)
seconds=$(echo $duration | cut -d"":"" -f3)
# For some reason, we have to multiply by two (no idea why...)
# Get the integer part with cut
frames=$(echo ""($hours*3600+$minutes*60+$seconds)*$fps*2"" | bc | cut -d""."" -f1)

And yes, for some reason I have to get the integer part of it. It doesn't make sense, but this script has always managed to convert my videos properly so far."
Guest [Entry]

"Tested on Ubuntu.

melt icecap.mp4 -consumer xml

melt - melt was meant as a test tool for the MLT framework, but it is also a powerful multitrack command line oriented video editor. It could also used as an minimalistic media player for audio and video files.
-consumer id[:arg] [name=value]*
Set the consumer (sink)
xml - Set the consumer (sink) to xml formatted output
<property name=""length"">nnnn</property> - shows the number of frames, where nnnn is replaced by an integer number that equals the number of frames

If you don't have melt you can install it on Ubuntu and other Debian based systems with sudo apt-get install melt"