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