dotlinux blog

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

Whether you’re a video editor, a developer working with multimedia, or simply curious about the technical details of a video file, knowing the total number of frames in a video is often essential. Frames are the building blocks of video—each frame is a still image, and their sequence creates the illusion of motion. Counting frames helps with tasks like synchronizing audio, ensuring smooth playback, debugging encoding issues, or analyzing video quality.

FFmpeg, a powerful open-source multimedia framework, is the go-to tool for this task on Linux. It’s lightweight, flexible, and capable of handling almost any video format. In this guide, we’ll explore step-by-step methods to count total frames in a video using FFmpeg and its companion tool, ffprobe (a specialized utility for analyzing media files). We’ll also cover troubleshooting tips and advanced use cases to ensure you get accurate results.

2026-03

Table of Contents#

  1. Prerequisites: Install FFmpeg on Linux
  2. Method 1: Using ffprobe (Fast and Recommended)
    • 2.1 Understanding the ffprobe Command
    • 2.2 Example: Count Frames in a Video File
  3. Method 2: Using ffmpeg Directly (Slower, but Works)
    • 3.1 How This Method Works
    • 3.2 Example: Count Frames with ffmpeg
  4. Advanced: Count Specific Frame Types (I, P, B Frames)
  5. Troubleshooting Common Issues
  6. Conclusion
  7. References

Prerequisites: Install FFmpeg on Linux#

Before we start, ensure FFmpeg is installed on your Linux system. FFmpeg is available in the default repositories of most Linux distributions. Use the commands below to install it:

For Ubuntu/Debian-based systems:#

sudo apt update && sudo apt install ffmpeg -y

For Fedora/RHEL-based systems:#

sudo dnf install ffmpeg -y

For Arch Linux:#

sudo pacman -S ffmpeg

Verify the installation by checking the version:

ffmpeg -version

You should see output with FFmpeg’s version and supported codecs.

ffprobe is a command-line tool included with FFmpeg that analyzes media files and extracts metadata (e.g., duration, codecs, frame count). It’s faster than ffmpeg for frame counting because it doesn’t process the entire video—instead, it reads metadata directly (when available) or scans the file efficiently.

2.1 Understanding the ffprobe Command#

The basic ffprobe command to count total frames is:

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

Let’s break down the flags:

  • -v error: Suppresses all output except errors (keeps the result clean).
  • -count_frames: Forces ffprobe to count frames in the selected stream.
  • -select_streams v:0: Selects the first video stream (v:0; use v:1 for the second video stream if the file has multiple).
  • -show_entries stream=nb_read_frames: Extracts the nb_read_frames field (total frames read from the stream).
  • -of default=nokey=1:noprint_wrappers=1: Formats the output to show only the frame count (no extra labels or brackets).

2.2 Example: Count Frames in a Video File#

Let’s test this with a sample video file (replace input.mp4 with your video path):

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

Sample Output:

1200

This means the video has 1200 frames.

Method 2: Using ffmpeg Directly (Slower, but Works)#

If ffprobe isn’t available (unlikely, since it’s part of FFmpeg), or if you need to process the video while counting frames, you can use ffmpeg directly. This method is slower because ffmpeg decodes the entire video, but it’s reliable for files with incomplete metadata.

3.1 How This Method Works#

The ffmpeg command below transcodes the video to a "null" output (discards the result) and counts frames during processing. The frame count is printed in the progress output:

ffmpeg -i input.mp4 -f null - 2>&1 | grep -i "frame"
  • -i input.mp4: Specifies the input video file.
  • -f null -: Sets the output format to "null" (no actual output file is created).
  • 2>&1: Redirects error output (where progress is printed) to standard output.
  • grep -i "frame": Filters the output to show only lines containing "frame".

3.2 Example: Count Frames with ffmpeg#

Run the command on your video:

ffmpeg -i input.mp4 -f null - 2>&1 | grep -i "frame"

Sample Output:

frame= 1200 fps=0.0 q=-0.0 Lsize=N/A time=00:00:40.00 bitrate=N/A speed=123x

Here, frame= 1200 indicates the total frame count.

Advanced: Count Specific Frame Types (I, P, B Frames)#

Videos use different frame types to compress data:

  • I-frames (Intra-coded frames): Complete images (keyframes).
  • P-frames (Predictive frames): Reference previous frames to save space.
  • B-frames (Bidirectional frames): Reference both previous and next frames.

To count specific frame types, use ffprobe with -show_frames and grep:

Count I-frames (Keyframes):#

ffprobe -v error -show_frames -select_streams v:0 input.mp4 | grep -c "pict_type=I"

Count P-frames:#

ffprobe -v error -show_frames -select_streams v:0 input.mp4 | grep -c "pict_type=P"

Count B-frames:#

ffprobe -v error -show_frames -select_streams v:0 input.mp4 | grep -c "pict_type=B"

Example Output (for I-frames):

30

Troubleshooting Common Issues#

1. "No such file or directory"#

  • Fix: Ensure the video path is correct. Use absolute paths (e.g., /home/user/videos/input.mp4) if the file isn’t in the current directory.

2. "Stream not found" (for v:0)#

  • Issue: The video may have multiple video streams (e.g., a video with multiple resolutions).
  • Fix: List all streams first with:
    ffprobe -show_streams input.mp4 | grep "stream #0:"
    Look for stream #0:v:0 (first video stream), stream #0:v:1 (second), etc. Adjust -select_streams v:X accordingly.

3. Frame count mismatch with duration × FPS#

  • Issue: Some videos use variable frame rates (VFR), where FPS varies. The formula duration (seconds) × FPS may not match the actual frame count.
  • Fix: Use ffprobe (Method 1) for accurate counts, as it scans the actual frames.

4. Corrupt or unreadable files#

  • Fix: Use ffmpeg to repair the video first:
    ffmpeg -i input.mp4 -c:v copy -c:a copy repaired.mp4
    Then count frames in repaired.mp4.

Conclusion#

Counting frames in a video on Linux is straightforward with FFmpeg. For most use cases, ffprobe (Method 1) is the fastest and most reliable option, as it extracts frame data without full decoding. If you need to process the video anyway, ffmpeg (Method 2) works, but it’s slower. For advanced analysis, use ffprobe to count specific frame types like I-frames.

FFmpeg’s flexibility makes it indispensable for video-related tasks—explore its documentation to unlock more features!

References#