How to Count Total Frames in a Video on Linux Using FFmpeg

Counting video frames can be useful in various scenarios, such as video editing, quality analysis, or determining frame rates.

FFmpeg, a popular open-source tool for handling multimedia files, provides an easy way to count video frames.

This article will walk you through how to use FFmpeg on Linux to count video frames.

Installing FFmpeg in Linux

FFmpeg installed on your Linux system, if you don’t have it installed, use the following command to install FFmpeg.

sudo apt install ffmpeg         [On Debian, Ubuntu and Mint]
sudo yum install ffmpeg         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/ffmpeg  [On Gentoo Linux]
sudo apk add ffmpeg             [On Alpine Linux]
sudo pacman -S ffmpeg           [On Arch Linux]
sudo zypper install ffmpeg      [On OpenSUSE]    
sudo pkg install ffmpeg         [On FreeBSD]

Using FFmpeg to Count Video Frames

Before running FFmpeg, make sure you know the exact path to your video file, which could be in your Downloads folder, Videos directory, or another location.

To count frames in a video, use the following command:

ffmpeg -i video.mp4 -map 0:v:0 -c copy -f null -

Explanation of the Command:

  • ffmpeg: Runs the FFmpeg program.
  • -i video.mp4: Specifies the input video file.
  • -map 0:v:0: Maps the first video stream (the main video).
  • -c copy: Copies the video codec without re-encoding (faster processing).
  • -f null -: Sends the output to a null sink (discards the output).

FFmpeg will display output in the terminal, including statistics for each frame it processes. Near the end of the output, look for a line showing the number of frames processed.

frame= 4926 fps=0.0 q=-1.0 Lsize=N/A time=00:03:16.96 bitrate=N/A speed=1.97e+04x

The frame value represents the total number of frames in the video. For example, if it shows frame= 4926, this means your video has 4926 frames.

If you want to save the frame count result to a text file, use the grep command to filter only the frame information and redirect it to a file.

ffmpeg -i video.mp4 -map 0:v:0 -c copy -f null - 2>&1 | grep "frame=" > frame_count.txt

Open the frame_count.txt file to see the total frame count.

cat frame_count.txt

Sample Output:

frame= 4926 fps=0.0 q=-1.0 Lsize=N/A time=00:03:16.96 bitrate=N/A speed=2.15e+04x    

Using FFprobe to Count Video Frames

FFprobe, another tool within the FFmpeg suite, can directly extract information about the frames without decoding the video. It’s often faster than using FFmpeg for counting frames.

To count frames with ffprobe, use the following command:

ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of csv=p=0 video.mp4

This command will output the total frame count as a single number.

4926    
Conclusion

Counting frames in a video is easy with FFmpeg on Linux, and there are a few different ways to do it. The most direct way is to use the -f null - command with FFmpeg, but you can also use FFprobe for a quicker result.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.