How to Set Constant Quantizer QP in FFmpeg x264
This article explains how to configure the Constant Quantizer (QP) mode for the x264 encoder using FFmpeg. You will learn the exact command-line syntax, understand how the QP scale works, and see practical examples to achieve consistent video quality across your encoding projects.
To specify Constant Quantizer (QP) mode in FFmpeg for the
libx264 encoder, you must use the -qp flag.
This mode tells the encoder to apply a fixed level of compression to
every frame of the video, regardless of scene complexity.
The Basic Command Syntax
The basic syntax for setting a constant quantizer value is as follows:
ffmpeg -i input.mp4 -c:v libx264 -qp 23 output.mp4In this command: * -i input.mp4 specifies the input
file. * -c:v libx264 selects the H.264 video encoder. *
-qp 23 sets the constant quantizer value to 23. *
output.mp4 is the name of the output file.
Understanding the QP Scale
The QP scale for the x264 encoder ranges from 0 to 51:
- QP 0: This enables lossless encoding. The output video will be mathematically identical to the input, but the file size will be extremely large.
- QP 18: This is generally considered “visually lossless.” It provides high-fidelity video where compression artifacts are virtually invisible to the human eye.
- QP 23: This is the default value. It offers a strong balance between visual quality and file size.
- QP 51: This represents the maximum compression, resulting in the lowest possible quality and smallest file size.
As a general rule, decreasing the QP value by 6 halves the compression (resulting in roughly double the bitrate and file size), while increasing the QP value by 6 doubles the compression (halving the file size).
QP vs. CRF
While Constant Rate Factor (CRF) is generally recommended for
everyday video encoding because it adjusts the quantizer dynamically to
save bandwidth on high-motion scenes, QP mode is useful for specific use
cases. Use QP mode when you need mathematically consistent compression
across all frames, such as during video quality analysis, scientific
testing, or when creating reference files. Using -qp
automatically disables adaptive quantization and psychoacoustic
optimizations that would otherwise alter the compression rate per
block.