How to Set ffmpeg libx264 B-Frame Strategy

This article explains how to configure the B-frame decision strategy in FFmpeg using the libx264 encoder. It covers the available configuration options, the differences between strategy levels, and provides practical command-line examples to help you optimize your video encoding for speed or quality.

The B-frame strategy determines how the libx264 encoder decides when and how many B-frames (bi-directional predictive frames) to use. Adjusting this setting allows you to balance encoding efficiency, visual quality, and processing speed.

The B-Frame Strategy Options

In FFmpeg, you configure the B-frame strategy using the -b_strategy flag or via the -x264-params option. The strategy accepts three main values:

  • 0 (Disabled / Fast): The encoder does not use adaptive B-frame placement. It will strictly use the maximum number of B-frames specified by the -bf parameter, regardless of the video content. This is the fastest method but offers the lowest compression efficiency.
  • 1 (Fast / Standard): A fast, low-complexity decision algorithm. It dynamically chooses when to use B-frames to save space, offering a good balance between encoding speed and compression.
  • 2 (Optimal / Slow): A high-complexity path-finding algorithm (sometimes referred to as the "Trellis" decision). It evaluates multiple frame combinations to find the absolute best placement for B-frames. This yields the highest quality and best compression but significantly slows down the encoding process.

How to Configure the Strategy in FFmpeg

To change the B-frame strategy, you must combine it with the -bf option, which sets the maximum consecutive B-frames the encoder is allowed to use.

Method 1: Using native FFmpeg flags

You can set the strategy directly using the -b_strategy flag:

ffmpeg -i input.mp4 -c:v libx264 -bf 3 -b_strategy 2 -c:a copy output.mp4

In this command:

  • -bf 3 sets the maximum number of consecutive B-frames to 3.
  • -b_strategy 2 enables the optimal, high-quality decision algorithm.

Passing the parameter directly to the libx264 library via -x264-params is often the most reliable method:

ffmpeg -i input.mp4 -c:v libx264 -bf 4 -x264-params b-strategy=1 -c:a copy output.mp4

In this command:

  • -bf 4 allows up to 4 consecutive B-frames.
  • -x264-params b-strategy=1 applies the fast adaptive algorithm.

Preset Defaults

If you do not manually set the B-frame strategy, it is determined by the -preset you choose:

  • ultrafast and superfast use 0.
  • veryfast, faster, and fast use 1.
  • medium (the default preset), slow, slower, and veryslow use 2.