How to Configure weightb in FFmpeg libx264
This article provides a quick guide on how to configure the
weightb (weighted prediction for B-frames) option in the
FFmpeg libx264 encoder. You will learn what this setting
does, why it is useful for video compression, and the exact command-line
syntax required to enable or disable it during your encoding
workflows.
Understanding weightb in x264
The weightb option enables weighted prediction for
B-frames in the H.264 codec. Weighted prediction allows the encoder to
scale the reference frames used to predict B-frames, which significantly
improves compression efficiency and video quality. This is particularly
effective in scenes containing fades, dissolves, transitions, or rapid
changes in brightness.
By default, libx264 enables weightb.
However, you may need to explicitly configure it to ensure compatibility
with older hardware decoders or to fine-tune your encoding
parameters.
How to Configure weightb in FFmpeg
To configure weightb in FFmpeg, you must pass the option
to the libx264 encoder using the -x264-params
or -x264opts flag. The parameter accepts a binary value:
1 to enable it, or 0 to disable it.
1. Enabling weightb (Default)
To explicitly enable weighted B-frames, set weightb=1
inside the -x264-params argument:
ffmpeg -i input.mp4 -c:v libx264 -x264-params weightb=1 -c:a copy output.mp42. Disabling weightb
If you are targeting legacy playback devices or hardware players that
do not support weighted B-frames, you can disable the feature by setting
weightb=0:
ffmpeg -i input.mp4 -c:v libx264 -x264-params weightb=0 -c:a copy output.mp4Using Multiple x264 Parameters
If you are configuring other encoder settings alongside
weightb, separate the key-value pairs with a colon
(:) when using -x264-params:
ffmpeg -i input.mp4 -c:v libx264 -x264-params weightb=1:keyint=24:ref=4 -c:a copy output.mp4When to Adjust This Setting
- Keep it enabled (weightb=1): For almost all standard web streaming, archiving, and modern device playback. It offers better visual quality at lower bitrates.
- Disable it (weightb=0): Only when encoding for highly restrictive, older hardware profiles (such as Baseline Profile devices) that fail to decode streams utilizing weighted B-frames.