
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: November 6, 2024
FFmpeg is one of the most popular tools to work with audio and video files. It is an open-source, cross-platform tool, and also offers a command line interface on Linux. It is very handy for performing basic tasks with audio and video (AV) such as viewing the stats and metadata about AV files, and converting, cropping, or joining them.
In this tutorial, let’s look at how we can count the total number of frames in a video file on the Linux command line, using FFmpeg.
The FFmpeg downloads page lists instructions for downloading and installing FFmpeg on various Linux distributions. On Ubuntu systems, we can install it using the following command:
$ sudo apt install ffmpeg
Once the installation is complete, we’ll have three commands at our disposal: ffmpeg, ffprobe, and ffplay. However, for this tutorial, we’ll be using only ffmpeg and ffprobe.
ffprobe is a command line tool bundled with the FFmpeg package that gives us utilities to gather information from multimedia streams. This information includes duration, bitrate, and also the frame count.
Using ffprobe, we can get the frame count of a video, say test.mp4, as follows:
$ ffprobe -v error
-select_streams v:0
-count_frames
-show_entries stream=nb_read_frames
-of csv=p=0
test.mp4
962
The above command outputs 962 as the frame count without any additional text. Let’s look at the parameters we passed to the ffprobe command in detail:
We can also count the number of packets instead of counting the number of frames. In any case, the result will be the same, but counting the packets is usually faster than counting the frames. We can do this as follows:
$ ffprobe -v error
-select_streams v:0
-count_packets
-show_entries stream=nb_read_packets
-of csv=p=0
test.mp4
962
In the above command, we substituted -count_frames with -count_packets, and nb_read_frames with nb_read_packets. We see that the output remains the same.
While the ffprobe command is meant for gathering information and the ffmpeg command is meant for processing the file, the ffmpeg command also prints the frame numbers when it runs on a file. If, for some reason, we do not have ffprobe installed, we can use the ffmpeg to count the frames, by copying our video file, test.mp4, to the virtual null device /dev/null. We are not interested in the output of the command, but only in the logs, which contain the frame number. Let’s see how to do this:
$ ffmpeg -i test.mp4 -f null /dev/null
ffmpeg version 6.1.1-3ubuntu5 Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 13 (Ubuntu 13.2.0-23ubuntu3)
...
...
...
[out#0/null @ 0x5c24f2c3a980] video:451kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
frame= 962 fps=0.0 q=-0.0 Lsize=N/A time=00:00:40.04 bitrate=N/A speed=81.8x
We see that we have the frame count of 962 shown in the last line of the above output. Further, we can filter the output using grep and cut to show just the number:
$ ffmpeg -i test.mp4 -f null /dev/null 2>&1 | grep 'frame=' | cut -d ' ' -
f 3
962
In the above command, we used grep to select only the output line that has frame= and the cut command to extract the number from the line. The result is just the frame count, 962.
In this article, we looked at how to count the total number of frames in a video using FFmpeg’s command line tools. Using the ffprobe command is the simple and straightforward option and the ffmpeg command can be used as a quick backup if the ffprobe command is unavailable.