How to Use scale_vaapi Filter in FFmpeg

This article provides a straightforward guide on how to use the scale_vaapi filter in FFmpeg for hardware-accelerated video scaling. You will learn the basic command structure, how to configure VAAPI (Video Acceleration API) hardware acceleration, and view practical examples for scaling videos efficiently using your GPU.

The scale_vaapi filter resizes video frames directly within GPU memory. By performing the scaling on the graphics card instead of the CPU, you significantly reduce CPU usage and speed up the transcoding process. To use this filter, your system must have a VAAPI-compatible GPU (such as Intel or AMD) with the correct drivers installed, and your FFmpeg build must support VAAPI.

Basic Syntax and Parameters

The basic syntax for the scale_vaapi filter is:

scale_vaapi=w=width:h=height

To maintain the aspect ratio of the input video automatically, set one of the dimensions to -1 or -2 (which forces the calculated dimension to be divisible by 2). For example, scale_vaapi=w=1920:h=-2 scales the width to 1920 pixels and calculates the appropriate height.

For the best performance, you should decode, scale, and encode entirely on the GPU. This prevents expensive memory copies between system RAM and GPU memory.

ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -vf "scale_vaapi=w=1280:h=720" -c:v h264_vaapi output.mp4

Example 2: Software Decode with Hardware Scale

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

ffmpeg -init_hw_device vaapi=gpu:/dev/dri/renderD128 -filter_hw_device gpu -i input.mp4 -vf "format=nv12,hwupload,scale_vaapi=w=1920:h=1080" -c:v h264_vaapi output.mp4