How to Transcode Video with FFmpeg hevc_vaapi

This guide explains how to use the hardware-accelerated hevc_vaapi encoder in FFmpeg to transcode videos into the High Efficiency Video Coding (HEVC/H.265) format. You will learn the system prerequisites, the basic command-line structure for full hardware transcoding, and how to adjust quality and bitrate settings to optimize your output.

Prerequisites

To use VAAPI (Video Acceleration API) for HEVC encoding, your system must meet the following requirements: * Hardware: An Intel GPU (Skylake/Gen 9 or newer) or an AMD GPU that supports HEVC hardware encoding. * Drivers: Installed VAAPI drivers (typically intel-media-driver for Intel or mesa-va-drivers for AMD on Linux). * FFmpeg: A version of FFmpeg compiled with the --enable-vaapi flag.

The Basic Transcoding Command

For the fastest transcoding speed, you should decode, process, and encode the video entirely on the GPU. This prevents bottlenecking caused by transferring video frames between system memory (RAM) and graphics memory (VRAM).

Use the following command template for a full hardware-accelerated transcode:

ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -c:v hevc_vaapi -c:a copy output.mp4

Command Breakdown:

Controlling Bitrate and Quality

Hardware encoders do not support standard software-based CRF (Constant Rate Factor) settings. Instead, you must control the output size and quality using bitrate modes or Constant QP (Quantization Parameter).

Variable Bitrate (VBR)

Use VBR to allow the encoder to allocate more data to complex scenes and less data to simple scenes. Set a target bitrate with -b:v and a maximum limit with -maxrate.

ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -c:v hevc_vaapi -b:v 5M -maxrate 8M -c:a copy output.mp4

Constant QP (CQP)

For constant quality encoding, you can set a fixed QP value using the -qp flag. Lower values produce higher quality and larger files, while higher values produce lower quality and smaller files. A value between 20 and 25 is ideal for most projects.

ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -c:v hevc_vaapi -qp 23 -c:a copy output.mp4

Software Decoding to Hardware Encoding

If your input video format is not supported by your GPU hardware decoder, you must decode the video using the CPU and then upload the frames to the GPU for encoding.

ffmpeg -i input.mp4 -vf "format=nv12,hwupload" -c:v hevc_vaapi -qp 23 -c:a copy output.mp4

The -vf "format=nv12,hwupload" filter converts the CPU frames into a compatible pixel format and uploads them directly to the GPU’s memory space before reaching the encoder.