How to Set B-Frames in FFmpeg libx265

This article provides a quick guide on how to configure the number of B-frames (bi-directional frames) when encoding video with the libx265 HEVC encoder in FFmpeg. You will discover the exact command-line arguments needed to control B-frame frequency, allowing you to balance video quality, file size, and encoding performance.

To configure the number of B-frames in libx265, you can use two different methods in your FFmpeg command: the generic FFmpeg -bf option, or the encoder-specific -x265-params option.

Method 1: Using the -bf Option

The simplest way to set the maximum number of consecutive B-frames is by using the standard FFmpeg -bf flag.

ffmpeg -i input.mp4 -c:v libx265 -bf 4 output.mp4

In this command:

  • -c:v libx265 selects the H.265/HEVC encoder.
  • -bf 4 restricts the encoder to a maximum of 4 consecutive B-frames.

Method 2: Using -x265-params

For more direct control over the x265 library, you can pass the bframes parameter directly to the encoder using the -x265-params flag. This method is useful if you are already passing other x265-specific options.

ffmpeg -i input.mp4 -c:v libx265 -x265-params bframes=4 output.mp4

To pass multiple parameters, separate them with colons:

ffmpeg -i input.mp4 -c:v libx265 -x265-params bframes=4:keyint=240 output.mp4

Recommendation and Best Practices

  • Default Value: The default number of B-frames for libx265 is 4.
  • Higher B-frames (e.g., 5 to 8): Increasing the number of B-frames can improve compression efficiency and reduce file size, but it requires more CPU power to encode and increases encoding latency.
  • Lower B-frames (e.g., 0 to 2): Decreasing the number of B-frames reduces encoding complexity and latency, making it ideal for live streaming or low-latency applications. Setting -bf 0 or bframes=0 disables B-frames entirely.