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

Parameter Breakdown

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"