Configure B-Frame Bias in FFmpeg libx264
This guide explains how to configure the B-frame bias in the FFmpeg
libx264 encoder. You will learn what B-frame bias is, how
it affects your video compression, and the exact command-line parameters
required to adjust this setting to optimize your video quality and file
size.
What is B-Frame Bias?
B-frame bias controls how aggressively the libx264
encoder chooses B-frames (bidirectional predicted frames) over P-frames
(predictive frames).
- B-frames offer the highest compression efficiency but require more CPU power to decode and encode.
- P-frames offer moderate compression and require less processing power.
By adjusting the B-frame bias, you can influence the encoder’s decision-making process. The value ranges from -100 to 100, with 0 being the default. * Positive values (e.g., 20, 50): Encourage the encoder to use more B-frames. This is useful for slow-moving scenes or high-compressibility content to reduce file size. * Negative values (e.g., -20, -50): Discourage the encoder from using B-frames. This is useful for fast-action content where B-frames might introduce compression artifacts, or for reducing latency.
How to Set B-Frame Bias in FFmpeg
To configure the B-frame bias, you must use the libx264
encoder. Because B-frame bias only works when B-frames are enabled, you
should also specify the maximum number of consecutive B-frames using the
-bf option.
There are two primary ways to set the B-frame bias in FFmpeg:
Method 1:
Using the -x264-params Flag (Recommended)
The most reliable way to pass parameters directly to the underlying
x264 library is by using the -x264-params
option.
ffmpeg -i input.mp4 -c:v libx264 -bf 3 -x264-params b-bias=20 output.mp4In this command: * -c:v libx264: Uses the H.264 encoder.
* -bf 3: Sets the maximum number of consecutive B-frames to
3. * -x264-params b-bias=20: Increases the bias to 20,
making the encoder more likely to use B-frames up to the limit of 3.
Method 2: Using
the Native FFmpeg -b-bias Flag
FFmpeg also maps this setting to a native option, though support can occasionally vary depending on your FFmpeg build.
ffmpeg -i input.mp4 -c:v libx264 -bf 3 -b-bias 20 output.mp4Practical Examples
For Highly Compressible or Static Video (Slideshows, Talking Heads)
To maximize compression and reduce file size, increase the B-frame bias:
ffmpeg -i input.mp4 -c:v libx264 -bf 5 -x264-params b-bias=50 output.mp4For High-Motion Video (Sports, Gameplay)
To prevent compression artifacts in fast-moving scenes, decrease the B-frame bias:
ffmpeg -i input.mp4 -c:v libx264 -bf 3 -x264-params b-bias=-20 output.mp4