FFmpeg VAAPI HEVC Hardware Transcoding on Linux

This guide explains how to use the Video Acceleration API (VAAPI) in FFmpeg to perform hardware-accelerated HEVC (H.265) video transcoding on Linux. By offloading both the decoding and encoding processes to your graphics hardware (Intel or AMD), you can achieve exceptionally fast transcode speeds and minimal CPU usage. The following sections cover driver verification and the exact FFmpeg commands needed for efficient hardware transcoding.

Step 1: Verify VAAPI Driver Support

Before running FFmpeg, you must ensure your system has the correct VAAPI drivers installed and that your GPU supports HEVC decoding and encoding.

Install the vainfo utility via your package manager (e.g., sudo apt install vainfo or sudo dnf install vainfo) and run it in your terminal:

vainfo

Look through the output for HEVC entry points. You should see lines similar to:

If these profiles are visible, your hardware is ready.

Step 2: Full Hardware Transcoding Command (HEVC to HEVC)

To transcode an HEVC video using hardware acceleration for both decoding and encoding, you must tell FFmpeg to initialize the VAAPI device, keep the decoded frames in GPU memory, and encode the output using the VAAPI HEVC encoder.

Run the following command:

ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input_hevc.mp4 -c:v hevc_vaapi -b:v 5M -c:a copy output.mp4

Command breakdown: * -hwaccel vaapi: Enables VAAPI hardware acceleration for the input decoding. * -hwaccel_device /dev/dri/renderD128: Specifies the GPU render node to use (usually renderD128). * -hwaccel_output_format vaapi: Keeps decoded video frames in GPU memory to prevent slow transfer speeds between the GPU and CPU. * -i input_hevc.mp4: The source video file (encoded in HEVC). * -c:v hevc_vaapi: Uses the hardware-accelerated HEVC VAAPI encoder for the output file. * -b:v 5M: Sets the target video bitrate to 5 Mbps (adjust this to control quality and file size). * -c:a copy: Copies the audio stream directly without re-encoding to save CPU cycles.

Step 3: Hardware Transcoding with Scaling

If you need to resize the video during the transcode process, you must use VAAPI-specific video filters. Standard filters will force the frames back to the CPU, ruining performance.

To scale a video to 1080p during transcoding, use the scale_vaapi filter:

ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input_hevc.mp4 -vf "scale_vaapi=w=1920:h=1080" -c:v hevc_vaapi -b:v 4M -c:a copy output_1080p.mp4