Fetch frame count with ffmpeg
Categories:
Accurately Fetching Frame Count from Video Files with FFmpeg

Learn various methods to determine the total frame count of a video using FFmpeg, including stream analysis and direct calculation, ensuring accuracy for your video processing workflows.
Determining the exact frame count of a video file is a common task in video processing, especially when dealing with frame-accurate operations, seeking, or analysis. FFmpeg, the powerful open-source multimedia framework, provides several ways to extract this information. However, the reliability and accuracy of these methods can vary depending on the video container, codec, and how the frame rate is stored. This article explores the most effective FFmpeg commands and techniques to reliably obtain the frame count.
Understanding Frame Count Challenges
Before diving into solutions, it's crucial to understand why fetching frame count isn't always straightforward. Video files often store frame rate information in different ways: as a constant frame rate (CFR), variable frame rate (VFR), or even with a timebase that requires calculation. Some containers might not explicitly store the total number of frames, making it necessary to derive it from duration and frame rate. Inaccurate frame counts can lead to synchronization issues, incorrect trimming, or failed processing tasks.
flowchart TD A[Start: Get Video Frame Count] --> B{Video Metadata Available?} B -->|Yes| C[Extract 'nb_frames' from Stream] C --> D[Result: Direct Frame Count] B -->|No| E{Duration & Frame Rate Available?} E -->|Yes| F[Calculate: Duration * FPS] F --> G[Result: Calculated Frame Count] E -->|No| H[Decode & Count Frames (Slowest)] H --> I[Result: Decoded Frame Count] D --> J[End] G --> J I --> J
Decision flow for determining video frame count using FFmpeg.
Method 1: Using ffprobe
for Stream Analysis
The most robust and generally recommended method is to use ffprobe
, a companion tool to ffmpeg
that gathers information from multimedia streams. ffprobe
can often directly read the nb_frames
property from the video stream's metadata, if available and accurate. This property represents the number of frames in the stream.
ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=noprint_wrappers=1:nokey=1 input.mp4
Extracting 'nb_frames' directly using ffprobe.
-select_streams v:0
option ensures we only look at the first video stream. -of default=noprint_wrappers=1:nokey=1
formats the output to just the value, making it easy to parse in scripts. If nb_frames
returns N/A
or an incorrect value, it means the container or codec doesn't store this information reliably, and you'll need to use a different method.Method 2: Calculating from Duration and Frame Rate
If nb_frames
is not available or unreliable, you can calculate the frame count by multiplying the video's duration by its average frame rate. This method works well for Constant Frame Rate (CFR) videos. For Variable Frame Rate (VFR) videos, this will give an approximate count based on the average FPS.
DURATION=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4)
FPS=$(ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 input.mp4)
# Calculate frames (requires basic arithmetic, e.g., using 'bc')
# Example: 25/1 -> 25, 30000/1001 -> 29.97
IFS='/' read -r NUM DEN <<< "$FPS"
AVG_FPS=$(echo "scale=4; $NUM / $DEN" | bc)
FRAME_COUNT=$(echo "scale=0; $DURATION * $AVG_FPS / 1" | bc)
echo "Frame Count: $FRAME_COUNT"
Calculating frame count from duration and average frame rate.
avg_frame_rate
property is a rational number (e.g., 30000/1001
for 29.97 FPS). You must perform floating-point division to get the actual FPS value before multiplying by the duration. Be aware that this method might introduce minor rounding errors, and for VFR content, it's an approximation.Method 3: Decoding and Counting (Most Accurate, but Slow)
For absolute accuracy, especially with problematic files or VFR content where metadata might be misleading, the most reliable method is to actually decode every frame and count them. This is computationally intensive and slow, making it unsuitable for large-scale processing, but it guarantees an accurate count.
ffmpeg -i input.mp4 -map 0:v:0 -c:v rawvideo -f null -y /dev/null 2>&1 | grep 'frame=' | tail -n 1 | awk '{print $2}'
Decoding and counting frames using ffmpeg.
-map 0:v:0
) to raw video (-c:v rawvideo
) and outputs it to a null device (-f null -y /dev/null
), effectively just processing the frames without saving them. The output is then parsed to find the last reported frame count. This method is resource-intensive and should be used as a last resort for critical accuracy.1. Prioritize ffprobe
nb_frames
Always try to use ffprobe -show_entries stream=nb_frames
first. It's the fastest and most direct method if the metadata is reliable.
2. Fall back to Duration x FPS
If nb_frames
is missing or incorrect, calculate the frame count using ffprobe
's duration
and avg_frame_rate
. Remember to handle rational numbers for FPS.
3. Use Decode & Count for Absolute Accuracy
For critical applications or problematic VFR files, resort to decoding and counting frames with ffmpeg
. Be mindful of the performance impact.
4. Validate Results
If possible, cross-reference the frame count with other tools or manual inspection for critical workflows, especially when dealing with diverse video sources.