How to Initialize VAAPI and Use scale_vaapi in FFmpeg
This article provides a straightforward guide on how to initialize a
VAAPI (Video Acceleration API) hardware device in FFmpeg and use it to
hardware-accelerate video scaling via the scale_vaapi
filter. You will learn the exact command-line syntax required to set up
the hardware device, decode the input, perform the scaling operation on
the GPU, and encode the output efficiently.
Prerequisites
To use VAAPI acceleration, your system must have a compatible GPU
(such as Intel or AMD) with the appropriate VAAPI drivers installed.
Additionally, your FFmpeg build must be compiled with VAAPI support
(--enable-vaapi).
The FFmpeg Command
To initialize a VAAPI device and scale a video, use the following template command:
ffmpeg -init_hw_device vaapi=gpu:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_device gpu -hwaccel_output_format vaapi -i input.mp4 -filter_hw_device gpu -vf "scale_vaapi=w=1920:h=1080" -c:v h264_vaapi output.mp4Parameter Breakdown
-init_hw_device vaapi=gpu:/dev/dri/renderD128: This initializes the VAAPI hardware device. It maps the hardware device at/dev/dri/renderD128(the standard render node on Linux) and aliases it with the namegpu.-hwaccel vaapi: Enables hardware acceleration for the input decoding process.-hwaccel_device gpu: Instructs the decoder to use thegpudevice initialized in the first step.-hwaccel_output_format vaapi: Keeps the decoded video frames in GPU memory (VAAPI surfaces) instead of copying them back to system memory (RAM). This is crucial for performance.-filter_hw_device gpu: Tells FFmpeg’s filter graph to use the initializedgpuhardware device. This is required for hardware filters likescale_vaapi.-vf "scale_vaapi=w=1920:h=1080": Applies the VAAPI hardware scaling filter. Replace1920and1080with your desired target width and height.-c:v h264_vaapi: Encodes the output video using the VAAPI H.264 hardware encoder, ensuring the frame stays on the GPU from decode to encode.
Handling Aspect Ratios
If you want to scale the video while maintaining the original aspect
ratio, you can set one of the dimensions to -1 or
-2 (which ensures the dimension is divisible by 2, required
by most encoders):
-vf "scale_vaapi=w=1280:h=-2"