Transcode Video with FFmpeg and h264_vaapi

This article explains how to transcode video files using the hardware-accelerated h264_vaapi encoder in FFmpeg. You will learn the necessary command-line syntax to offload the encoding process to your Intel or AMD GPU, resulting in faster render times and significantly lower CPU utilization.

Prerequisites

To use VAAPI (Video Acceleration API) with FFmpeg, your system must meet the following requirements: * A compatible Intel or AMD GPU. * The appropriate VAAPI drivers installed on your Linux system (e.g., intel-media-driver for Intel or mesa-va-drivers for AMD). * FFmpeg compiled with VAAPI support (verify by running ffmpeg -encoders | grep vaapi).

Fully Hardware-Accelerated Transcoding

For the fastest performance, you should decode the input video in hardware, keep the video frames in GPU memory, and then encode them using h264_vaapi.

Use the following command structure:

ffmpeg -init_hw_device vaapi=hw:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -i input.mp4 -c:v h264_vaapi -b:v 5M -c:a copy output.mp4

Command Breakdown

Software Decoding to Hardware Encoding

If your input format is not supported for hardware decoding, or if you need to apply software filters, you must decode the video in software (CPU), upload the frames to the GPU, and then encode them with VAAPI.

Use this command:

ffmpeg -init_hw_device vaapi=hw:/dev/dri/renderD128 -filter_hw_device hw -i input.mp4 -vf "format=nv12,hwupload" -c:v h264_vaapi -b:v 5M output.mp4

Command Breakdown for Software-to-Hardware

Fine-Tuning VAAPI Quality and Bitrate

VAAPI offers different rate control modes depending on your hardware driver: