How to Use VAAPI for Hardware Scaling in FFmpeg
Using the Video Acceleration API (VAAPI) in FFmpeg allows you to offload video scaling and processing tasks to your graphics hardware, significantly reducing CPU usage and improving processing speeds. This article provides a straightforward guide on how to configure FFmpeg to use VAAPI hardware acceleration specifically for scaling video files, including the necessary command-line arguments, device selection, and practical command examples.
Prerequisites
To use VAAPI with FFmpeg, your system must have: * A compatible GPU
(such as Intel integrated graphics or AMD graphics cards). * The
appropriate VAAPI drivers installed (intel-media-driver,
libva-intel-driver, or mesa-va-drivers). * An
FFmpeg build configured with --enable-vaapi.
The VAAPI Scaling Filter
In FFmpeg, hardware-accelerated scaling using VAAPI is achieved via
the scale_vaapi video filter. To maximize performance, you
should keep the video frames in hardware memory (GPU VRAM) throughout
the entire pipeline—from decoding to scaling and finally encoding. This
prevents costly copying of data back and forth between system RAM (CPU)
and video RAM (GPU).
Method 1: Full Hardware Pipeline (Recommended)
This method performs hardware decoding, hardware scaling, and hardware encoding entirely within the GPU. It offers the best performance.
ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device /dev/dri/renderD128 -i input.mp4 -vf "scale_vaapi=w=1920:h=1080" -c:v h264_vaapi output.mp4Command Breakdown:
-hwaccel vaapi: Enables VAAPI hardware acceleration for decoding.-hwaccel_output_format vaapi: Keeps the decoded video frames in GPU memory.-hwaccel_device /dev/dri/renderD128: Specifies the hardware device node (usually/dev/dri/renderD128on Linux).-vf "scale_vaapi=w=1920:h=1080": Calls the VAAPI scaler to resize the video to 1920x1080.-c:v h264_vaapi: Encodes the output video using the VAAPI H.264 hardware encoder.
Method 2: Software Decode with VAAPI Scaling and Encoding
If your input video format is not supported by your GPU’s hardware decoder, you must decode the video using the CPU, upload the frames to the GPU for scaling, and then encode them.
ffmpeg -init_hw_device vaapi=gpu:/dev/dri/renderD128 -filter_hw_device gpu -i input.mkv -vf "format=nv12|vaapi,hwupload,scale_vaapi=1280:720" -c:v h264_vaapi output.mp4Command Breakdown:
-init_hw_device vaapi=gpu:/dev/dri/renderD128: Initializes the VAAPI hardware device and names itgpu.-filter_hw_device gpu: Tells the filter graph to use the initializedgpudevice.-vf "format=nv12|vaapi,hwupload,scale_vaapi=1280:720":format=nv12|vaapi: Prepares the software frame format.hwupload: Uploads the software frames from system memory to GPU memory.scale_vaapi=1280:720: Scales the video to 1280x720 in hardware.
Maintaining Aspect Ratio
If you want to scale a video while maintaining its original aspect
ratio, you can set one of the dimensions (width or height) to
-1. The scale_vaapi filter will automatically
calculate the other dimension.
To scale a video to a height of 720 pixels while keeping the aspect ratio:
ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device /dev/dri/renderD128 -i input.mp4 -vf "scale_vaapi=w=-1:h=720" -c:v h264_vaapi output.mp4