Linux Command Line – How to Get Number of Frames in a Video

command lineframeratelinuxvideo

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.

In theory one should be able to multiply the length (in seconds) by the FPS to get the number of frames, but in this case the length (34.43 seconds) and the framerate (29.97 fps) give a non-integer, which makes me think I'm doing something wrong.

I need to be able to do this on the command line in a totally automated and non-graphical manner. I also need this to be pretty exact, and not an estimate (if that's even possible with video files)

I tried using tcprobe on some files. For some AVI files it works, but for some VOB files, the tcprobe output doesn't have the number of frames. I get this output:

[tcprobe] MPEG program stream (PS)
[tcprobe] summary for myfile.vob, (*) = not default, 0 = not detected
import frame size: -g 720x480 [720x576] (*)
     aspect ratio: 4:3 (*)
       frame rate: -f 29.970 [25.000] frc=4 (*)
                   PTS=2199.3972, frame_time=33ms bitrate=7000 kbps
      audio track: -a 0 [0] -e 48000,16,5 [48000,16,2] -n 0x2000 [0x2000] (*)
                   PTS=2199.2763, bitrate=192 kbps
                   -D 3 --av_fine_ms 20 (frames & ms) [0] [0]

Best Answer

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)

Related Question