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.

Quality and Speed Modes

The behavior of -cpu-used is tied to the -deadline parameter, which sets the overall quality/speed mode:

  1. Good Quality Mode (-deadline good): This is the default mode and is recommended for most file-based transcodes.
    • The effective range for -cpu-used is 0 to 5.
    • -cpu-used 0: Slowest speed, highest quality.
    • -cpu-used 1 or 2: 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.
  2. Realtime Mode (-deadline realtime): Used for live streaming where low latency is critical.
    • The effective range for -cpu-used is -16 to 16, but values between 5 and 8 are most common.
    • Higher values (like 5 to 8) are necessary to achieve real-time frame rates.

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.webm

In 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.webm

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