Limit Max QP in FFmpeg x265 Encoding

This article provides a straightforward guide on how to restrict the maximum Quantization Parameter (QP) value during x265 video encoding using FFmpeg. By defining a maximum QP limit, you can prevent the encoder from dropping the visual quality below a certain threshold during highly complex or fast-moving scenes.

Understanding QP in x265

The Quantization Parameter (QP) regulates the amount of spatial detail saved during encoding. In x265, QP values typically range from 0 (lossless) to 51 for 8-bit video, or up to 69 for 10-bit video. A lower QP value results in higher quality and larger file sizes, while a higher QP value results in lower quality and smaller file sizes. Setting a maximum QP ceiling ensures that the video quality never dips below your acceptable standard, even during high-motion scenes.

The FFmpeg Command Syntax

To limit the maximum QP in FFmpeg when using the libx265 encoder, you must pass the qpmax option inside the -x265-params flag.

The basic syntax is:

ffmpeg -i input.mp4 -c:v libx265 -x265-params qpmax=X output.mp4

Replace X with your desired maximum QP value.

Practical Examples

1. Standard CRF Encoding with a QP Ceiling

When using Constant Rate Factor (CRF) mode, the encoder dynamically adjusts the QP. You can set a safety net by defining a qpmax value (e.g., 38) to ensure the encoder never utilizes a worse quality level:

ffmpeg -i input.mp4 -c:v libx265 -crf 22 -x265-params qpmax=38 output.mp4

2. Combining Multiple x265 Parameters

If you are already utilizing other x265 parameters, you can append qpmax by separating the parameters with a colon (:):

ffmpeg -i input.mp4 -c:v libx265 -crf 20 -x265-params preset=slow:qpmax=35:keyint=240 output.mp4

Key Considerations