Use FFprobe to extract video bitrate by running the command with specific format options that target stream metadata. This process allows developers and media engineers to quickly verify encoding quality without re-encoding or opening heavy video players, providing precise data on bandwidth requirements for streaming or storage optimization.
Understanding Bitrate in Video Files
Bitrate represents the amount of data processed over a given time period, typically measured in kilobits per second (kbps) or megabits per second (Mbps). In video files, this metric is crucial because it directly influences visual quality and file size. A higher bitrate generally means better quality but also larger files, while a lower bitrate results in smaller files but potential artifacts like blockiness or blurring. Understanding how to measure this value accurately is essential for content creators who need to balance quality with bandwidth constraints.
Basic FFprobe Command for Bitrate Extraction
The most straightforward way to get the overall bitrate of a video file is to use FFprobe's default output format. However, the default JSON or XML outputs can be verbose. To get a clean, human-readable number, you should specify the output format. The following command extracts the stream information and filters for the bitrate:
ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 input.mp4
This command tells FFprobe to suppress error messages, select only the first video stream, show only the bit rate entry, and format the output as plain text without keys. The result will be a single number representing the average bitrate in bits per second. If you are working with multiple streams or complex containers, ensuring you select the correct stream index is vital. You can often cross-reference this with other metadata tools as described in
Crop Detection: Ensuring Frame Cropless Output with FFprobe and FFmpeg to ensure your media pipeline is consistent.
Distinguishing Between Average and Peak Bitrate
It is important to note that many video encoders produce variable bitrate (VBR) streams rather than constant bitrate (CBR). The command above typically returns the average bitrate calculated from the total file size and duration. If you need to know the peak bitrate, which indicates the maximum instantaneous data rate, you must look at different metadata fields. Some advanced codecs store peak bitrate information in the stream tags or side data.
For more granular control, you might need to parse the full stream dump. Use the following command to see all available stream attributes:
ffprobe -v quiet -print_format json -show_streams input.mp4
Look for fields like "avg_frame_rate" or "r_frame_rate" alongside the bitrate. Sometimes, the average bitrate is not explicitly listed if the encoder calculates it dynamically. In such cases, you may need to calculate it manually using the file size and duration. This calculation is particularly relevant when comparing different resolutions, such as understanding the difference between
Understanding the Difference Between 240p and 1080p Video Conversions, where bitrate scaling varies significantly based on pixel count.
Handling Audio and Mixed Streams
Video files often contain audio tracks, which also have their own bitrates. When you run the basic video-only command, you ignore the audio contribution to the total file size. If your goal is to determine the total container bitrate, you must include audio streams. Change the stream selector from
v:0 to
a:0 for audio, or remove the
-select_streams flag entirely to get all streams.
To get the total bitrate of both video and audio combined, you can sum the individual stream bitrates. However, FFprobe does not natively provide a "total container bitrate" field in standard outputs. You would need to parse the JSON output and add the values programmatically. This is useful for compliance checks in broadcasting where total throughput matters more than individual component rates.
Scripting and Automation
For batch processing, hardcoding commands into shell scripts or Python wrappers is efficient. When automating, always handle cases where the file might be corrupted or lack video streams. Check the exit code of FFprobe; a non-zero exit usually indicates an error. Additionally, ensure your environment has the correct version of FFmpeg installed, as older versions might lack certain flags or produce slightly different JSON structures. Issues with color numbers or extraction errors can sometimes arise, so refer to guides on
Fixing FFmpeg Color Number Extraction Issue if you encounter unexpected metadata anomalies during parsing.
Frequently Asked Questions
Why is my extracted bitrate different from the player display?
Players often round numbers or display nominal bitrates rather than exact calculated averages. FFprobe provides the precise mathematical average based on file size and duration, which may differ slightly from user interface displays due to rounding conventions or inclusion of overhead data.
Can FFprobe extract bitrate for live streams?
FFprobe is primarily designed for static files. While it can probe live inputs, extracting a stable bitrate requires sufficient data buffering. For live streams, monitoring tools within your streaming server or RTMP dashboard are more reliable for real-time bitrate tracking.
How do I get the bitrate in kbps instead of bps?
FFprobe outputs bits per second by default. To convert to kilobits per second, divide the output by 1000 in your script or use command-line tools like
awk or
bc to perform the arithmetic operation immediately after extraction.
What if the bitrate field is missing in the JSON output?
If the bitrate field is absent, the encoder may not have stored explicit bitrate metadata. In this case, calculate the bitrate manually by dividing the total file size in bits by the duration in seconds obtained from the
duration field in the same JSON output.
Does this method work for HLS playlists?
Standard FFprobe commands work on individual segment files (.ts) or master playlists. For HLS, you may need to point FFprobe at a specific segment URL or parse the m3u8 file to identify the resolution and bitrate tags defined in the playlist header, which are often more accurate than probing individual segments.