How to Configure libvpx Quality in FFmpeg
Configuring the quality parameter in the FFmpeg libvpx
(VP8) and libvpx-vp9 (VP9) encoders involves choosing the
right rate control mode, setting the Constant Rate Factor (CRF), and
adjusting speed presets. This guide explains how to set up these
parameters for both VP8 and VP9 to achieve the best balance between
video quality, file size, and encoding time.
1. Constant Quality Mode (Recommended for VP9)
For most encoding tasks, Constant Quality (CRF) mode is the recommended method. It ensures that every frame gets the exact number of bits it needs to maintain a consistent level of visual quality.
To use Constant Quality mode in VP9, you must set the video bitrate
(-b:v) to 0 and define a CRF value using the
-crf flag.
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 output.webm- CRF Range: 0 to 63.
- Recommended values: 20 to 35. Lower numbers mean higher quality and larger file sizes. A value of 31 is the default.
- The
-b:v 0requirement: For VP9, you must set-b:v 0to enable pure CRF mode. If you omit this, the encoder will target a default bitrate instead of relying purely on the CRF quality score.
For VP8 (libvpx), pure CRF with -b:v 0 is
not supported in the same way. Instead, use Constrained Quality
mode.
2. Constrained Quality Mode (Recommended for VP8)
Constrained Quality (CQ) mode allows you to target a specific quality level while also setting a maximum bitrate limit to prevent sudden file size spikes during complex scenes.
For VP9:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 2M output.webmIn this command, the encoder tries to maintain a CRF of 30, but the bitrate will not exceed 2 Megabits per second (2M).
For VP8:
ffmpeg -i input.mp4 -c:v libvpx -crf 10 -b:v 2M output.webm- VP8 CRF Range: 4 to 63 (lower is better quality).
- Recommended VP8 CRF: 10 to 20.
3. Two-Pass Bitrate Mode (For Target File Sizes)
If you need your output file to be an exact size, you should use two-pass encoding. This method analyzes the video in the first pass and performs the actual encoding in the second pass.
Pass 1:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -pass 1 -f null /dev/null(On Windows, replace /dev/null with
NUL)
Pass 2:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -pass 2 output.webm4. Balancing Speed and Quality
The speed of the encoding process directly impacts the quality. You
can control this trade-off using the -deadline and
-cpu-used flags.
The -deadline
Parameter
This flag sets the quality program deadline. It accepts three
arguments: * best: Slowest speed, highest quality. *
good: Recommended balance for most encodes (default). *
realtime: Fastest speed, lowest quality (ideal for live
streaming).
The -cpu-used
Parameter
Once -deadline is set, use -cpu-used to
fine-tune the speed. * For VP9: The values range from
0 to 5 when using -deadline good
or best. 0 is the slowest and highest quality,
while 5 is the fastest. * For VP8: The
values range from -16 to 16.
Example command optimized for quality and speed balance:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -deadline good -cpu-used 2 output.webm