Transcode Video with FFmpeg and h264_vaapi
This article explains how to transcode video files using the
hardware-accelerated h264_vaapi encoder in FFmpeg. You will
learn the necessary command-line syntax to offload the encoding process
to your Intel or AMD GPU, resulting in faster render times and
significantly lower CPU utilization.
Prerequisites
To use VAAPI (Video Acceleration API) with FFmpeg, your system must
meet the following requirements: * A compatible Intel or AMD GPU. * The
appropriate VAAPI drivers installed on your Linux system (e.g.,
intel-media-driver for Intel or
mesa-va-drivers for AMD). * FFmpeg compiled with VAAPI
support (verify by running
ffmpeg -encoders | grep vaapi).
Fully Hardware-Accelerated Transcoding
For the fastest performance, you should decode the input video in
hardware, keep the video frames in GPU memory, and then encode them
using h264_vaapi.
Use the following command structure:
ffmpeg -init_hw_device vaapi=hw:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -i input.mp4 -c:v h264_vaapi -b:v 5M -c:a copy output.mp4Command Breakdown
-init_hw_device vaapi=hw:/dev/dri/renderD128: Initializes the VAAPI hardware device using your GPU’s render node (typically/dev/dri/renderD128on Linux).-hwaccel vaapi: Enables hardware-accelerated decoding for the input video.-hwaccel_output_format vaapi: Ensures the decoded video frames remain in GPU memory to avoid slow transfers back to system RAM.-i input.mp4: Specifies the input video file.-c:v h264_vaapi: Selects the VAAPI H.264 hardware encoder.-b:v 5M: Sets the target video bitrate to 5 Mbps (adjust this value based on your quality needs).-c:a copy: Copies the audio stream without re-encoding to save time and preserve quality.output.mp4: The output file name.
Software Decoding to Hardware Encoding
If your input format is not supported for hardware decoding, or if you need to apply software filters, you must decode the video in software (CPU), upload the frames to the GPU, and then encode them with VAAPI.
Use this command:
ffmpeg -init_hw_device vaapi=hw:/dev/dri/renderD128 -filter_hw_device hw -i input.mp4 -vf "format=nv12,hwupload" -c:v h264_vaapi -b:v 5M output.mp4Command Breakdown for Software-to-Hardware
-filter_hw_device hw: Tells FFmpeg’s filter graph which hardware device to use for uploading frames.-vf "format=nv12,hwupload": A video filter chain that converts the software frames to thenv12pixel format (which is widely compatible with VAAPI) and then uploads them to the GPU memory.
Fine-Tuning VAAPI Quality and Bitrate
VAAPI offers different rate control modes depending on your hardware driver:
Constant QP (CQP): Focuses on consistent quality. Lower values mean better quality but larger file sizes.
ffmpeg -init_hw_device vaapi=hw:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -i input.mp4 -c:v h264_vaapi -qp 20 output.mp4Variable Bitrate (VBR): Balances quality and file size.
ffmpeg -init_hw_device vaapi=hw:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -i input.mp4 -c:v h264_vaapi -b:v 4M -maxrate 6M output.mp4