Extract Frame Count Using ffprobe (Number of Frames in a Video)

In this quick tutorial, we’ll teach you how to extract the number of frames (of frame count) in a video using ffprobe, an utility written using the FFmpeg video processing library. To learn more about these tools, please explore all our FFmpeg articles and do check out our deep dive into ffprobe.

frame count or count the number of frames  using ffmpeg or ffprobe

Frame Count using ffprobe

It is very easy to count the number of frames in a video using the ffprobe tool.

ffprobe -v error -select_streams v:0 -count_frames -show_entries stream=nb_read_frames -print_format csv BigBuckBunny.mp4

What does this command do?

  • -v error : this is a neat trick to hide the banner information printed by ffprobe (read more about this here)
  • -select streams v:0 is used to target the first video stream (more information here). You can modify suitably for your use case.
  • -count_frames as the name suggests, gives you a count of the number of frames in the video.
  • nb_read_frames is the number of frames returned by the decoder. stream is a selector that allows you to narrow down on what data to print. Here is more information on ffprobe’s specifiers.

Running this command will instruct ffprobe to print the number of frames in the video. It’s as simple as that!

ffprobe -v error -select_streams v:0 -count_frames -show_entries stream=nb_read_frames  Big_Buck_Bunny_1080_10s_20MB.mp4

Output
stream,300

If you want only the number and not the specifier (stream), then you can modify the commandline as instructed here. This will ensure that the keys are not printed and only the value is output by ffprobe.

ffprobe -v error -select_streams v:0 -count_frames -show_entries stream=nb_read_frames -print_format default=nokey=1:noprint_wrappers=1 Big_Buck_Bunny_1080_10s_20MB.mp4

Conclusion

I hope you found this quick tutorial on the use of ffprobe to count the number of frames in a video to be useful. Please subscribe to OTTVerse for more such articles, deep-dives, and interviews. Until next time, happy streaming!

2 thoughts on “Extract Frame Count Using ffprobe (Number of Frames in a Video)”

  1. Python function to do the same:

    def get_video_info(filename):
    #r_frame_rate is “the lowest framerate with which all timestamps can be
    #represented accurately (it is the least common multiple(LCM) of all framerates in the stream).”
    result = subprocess.run(
    [
    “ffprobe”,
    “-v”,
    “error”,
    “-select_streams”,
    “v:0”,
    “-show_entries”,
    “stream=avg_frame_rate,r_frame_rate,nb_frames,width,height”,
    filename,
    ],
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    )
    ffprobe_out = str(result.stdout, ‘utf-8’)
    print(ffprobe_out)
    r_frame_rate = int(ffprobe_out.split(‘r_frame_rate=’)[1].split(‘\n’)[0].split(‘/’)[0])
    avg_frame_rate = int(ffprobe_out.split(‘avg_frame_rate=’)[1].split(‘\n’)[0].split(‘/’)[0])
    nb_frames = int(ffprobe_out.split(‘nb_frames=’)[1].split(‘\n’)[0])
    height = int(ffprobe_out.split(‘height=’)[1].split(‘\n’)[0])
    width = int(ffprobe_out.split(‘width=’)[1].split(‘\n’)[0])
    return (r_frame_rate, avg_frame_rate, nb_frames, height, width)

  2. Pingback: CBR, CRF, and Changing Resolution using FFmpeg - OTTVerse

Leave a Comment

Your email address will not be published. Required fields are marked *

Enjoying this article? Subscribe to OTTVerse and receive exclusive news and information from the OTT Industry.