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.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Ravi Saive
Ravi Saive
Administrator
About the author

I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

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.