Transcode VP9 Video with vp9_vaapi in FFmpeg

This guide provides a straightforward tutorial on how to transcode videos to the VP9 format using hardware acceleration via the vp9_vaapi encoder in FFmpeg. You will learn the necessary prerequisites, the essential command-line structure, and practical examples to leverage your GPU for fast and efficient video encoding.

Prerequisites

To use VAAPI (Video Acceleration API) for VP9 encoding, you need:

  • A Linux-based operating system.
  • A GPU that supports VP9 hardware encoding (such as Intel Kaby Lake or newer, or AMD Radeon graphics).
  • The correct VAAPI drivers installed (e.g., intel-media-driver for Intel or mesa-va-drivers for AMD).
  • FFmpeg compiled with VAAPI support enabled (--enable-vaapi).

The Basic Command Structure

To hardware-transcode a video using the vp9_vaapi encoder, you must initialize the VAAPI device, decode the input (optionally in hardware), and specify the VP9 VAAPI encoder.

Here is the standard command:

ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -c:v vp9_vaapi -b:v 2M -c:a libopus output.mkv

Parameter Breakdown

  • -hwaccel vaapi: Enables VAAPI hardware acceleration for decoding the input file.
  • -hwaccel_device /dev/dri/renderD128: Specifies the hardware device path (usually the primary GPU render node).
  • -hwaccel_output_format vaapi: Keeps the decoded video frames in GPU memory to avoid slow transfers between GPU and system RAM.
  • -i input.mp4: Defines the source video file.
  • -c:v vp9_vaapi: Selects the hardware-accelerated VP9 encoder.
  • -b:v 2M: Sets the target video bitrate (e.g., 2 Megabits per second).
  • -c:a libopus: Encodes the audio to Opus, which is the standard companion audio codec for VP9 in MKV/WebM containers.

Hardware Scaling and Filtering

If you need to resize the video during transcoding, you must use hardware-based filters to keep the video frames in the GPU memory. Use the scale_vaapi filter instead of the standard software scale filter:

ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -vf "scale_vaapi=w=1920:h=1080" -c:v vp9_vaapi -b:v 4M output.mkv

Quality and Rate Control

VAAPI offers different rate control modes depending on your driver support.

  • Constant Bitrate (CBR): Specified using -b:v 3M -maxrate 3M.

  • Variable Bitrate (VBR): Specified using -b:v 3M (the driver automatically determines peak bitrates).

  • Constant Quality (CQP): If supported by your driver, you can set the quality level using the -q option (lower values mean higher quality):

    ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -c:v vp9_vaapi -q 25 output.mkv