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.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

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.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Ravi Saive
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.

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

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.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.