How to Use transpose_vaapi in FFmpeg
This article provides a quick guide on how to use the
transpose_vaapi filter in FFmpeg to perform
hardware-accelerated video rotation and flipping. By leveraging Intel’s
Video Acceleration API (VAAPI), you can rotate videos efficiently with
minimal CPU usage using practical command-line examples.
Understanding transpose_vaapi Parameters
The transpose_vaapi filter requires the dir
(direction) parameter to specify how the video should be rotated or
flipped. The parameter accepts the following integer values:
0: Rotate 90 degrees counter-clockwise and flip vertically.1: Rotate 90 degrees clockwise.2: Rotate 90 degrees counter-clockwise.3: Rotate 90 degrees clockwise and flip vertically.
FFmpeg Command Examples
To use VAAPI filters, you must initialize your hardware acceleration
device (usually /dev/dri/renderD128 on Linux).
Example 1: Full Hardware Pipeline (Decode, Rotate, and Encode)
The most efficient way to use transpose_vaapi is to keep
the entire pipeline inside the GPU. The following command decodes the
input video using VAAPI, rotates it 90 degrees clockwise
(dir=1), and encodes the output using the H.264 VAAPI
encoder:
ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -vf "transpose_vaapi=dir=1" -c:v h264_vaapi output.mp4Example 2: Rotating 180 Degrees
The transpose_vaapi filter does not have a single
180-degree rotation parameter. To rotate a video 180 degrees, you must
chain two 90-degree rotation filters together in the filtergraph:
ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -vf "transpose_vaapi=dir=1,transpose_vaapi=dir=1" -c:v h264_vaapi output.mp4Example 3: Software Input to VAAPI Rotate and Encode
If your source video cannot be decoded via hardware, you can decode
it using the CPU, upload the frames to the GPU using
hwupload, apply the rotation, and encode with VAAPI:
ffmpeg -init_hw_device vaapi=gpu:/dev/dri/renderD128 -filter_hw_device gpu -i input.mp4 -vf "format=nv12,hwupload,transpose_vaapi=dir=2" -c:v h264_vaapi output.mp4