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

  • width (w): The target width of the output video.
  • height (h): The target height of the output video.

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
  • -hwaccel vaapi: Enables VAAPI hardware decoding.
  • -hwaccel_device /dev/dri/renderD128: Specifies the GPU device to use (standard path on Linux).
  • -hwaccel_output_format vaapi: Keeps decoded frames in GPU memory.
  • -vf "scale_vaapi=w=1280:h=720": Scales the hardware frames to 1280x720.
  • -c:v h264_vaapi: Encodes the scaled frames using the VAAPI H.264 hardware encoder.

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
  • -init_hw_device vaapi=gpu:/dev/dri/renderD128: Initializes the VAAPI device and labels it as "gpu".
  • -filter_hw_device gpu: Instructs the filtergraph to use the initialized GPU device.
  • format=nv12,hwupload: Converts the software frames to a compatible pixel format (NV12) and uploads them to the GPU.
  • scale_vaapi=w=1920:h=1080: Scales the uploaded frames on the hardware.