Configure libvpx-vp9 Speed in FFmpeg
This guide explains how to configure the speed and quality trade-off
when encoding VP9 video using the libvpx-vp9 encoder in
FFmpeg. You will learn about the crucial -cpu-used
parameter, its recommended values for different encoding scenarios, and
how to structure your FFmpeg commands to optimize encoding times without
sacrificing visual quality.
The Speed Parameter:
-cpu-used
In the libvpx-vp9 encoder, encoding speed is controlled
primarily by the -cpu-used parameter. This parameter
determines how much CPU effort is spent on compression efficiency.
- Range: Integer values from
-8to8(though the effective range depends on the deadline mode). - Behavior: Lower numbers result in slower encoding speeds and higher visual quality (better compression). Higher numbers result in faster encoding speeds but lower compression efficiency (larger file size or lower quality at the same bitrate).
Quality and Speed Modes
The behavior of -cpu-used is tied to the
-deadline parameter, which sets the overall quality/speed
mode:
- Good Quality Mode (
-deadline good): This is the default mode and is recommended for most file-based transcodes.- The effective range for
-cpu-usedis0to5. -cpu-used 0: Slowest speed, highest quality.-cpu-used 1or2: Recommended balance for high-quality archival.-cpu-used 4: Recommended balance for general use and faster encoding.-cpu-used 5: Fastest speed in this mode, with some quality loss.
- The effective range for
- Realtime Mode (
-deadline realtime): Used for live streaming where low latency is critical.- The effective range for
-cpu-usedis-16to16, but values between5and8are most common. - Higher values (like
5to8) are necessary to achieve real-time frame rates.
- The effective range for
Practical FFmpeg Command Examples
1. Constant Quality (CRF) Encoding
For standard video-on-demand (VOD) encoding where you want a balance
of speed and quality, use a -cpu-used value of
1 to 4.
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -cpu-used 2 -c:a libopus output.webmIn this command, -cpu-used 2 is used to achieve
excellent quality at a reasonable encoding speed.
2. High-Speed Encoding
If you need to encode quickly and are willing to accept slightly
lower compression efficiency, set -cpu-used to
4 or 5.
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -cpu-used 4 -c:a libopus output.webm3. Recommended Two-Pass Encoding
For the best possible quality-to-file-size ratio, use two-pass
encoding. You can speed up the first pass by using a higher
-cpu-used value (like 4), and then use a lower
value (like 1 or 2) for the second pass to
ensure high quality.
Pass 1:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -speed 4 -pass 1 -f null /dev/null(Note: -speed is an alias for
-cpu-used)
Pass 2:
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -speed 1 -pass 2 -c:a libopus output.webm