Rotate Video in FFmpeg with transpose_opencl
This guide explains how to use FFmpeg's transpose_opencl
filter to achieve hardware-accelerated video rotation using your GPU.
You will learn the required OpenCL device initialization, the hardware
upload and download filter chain, available rotation parameters, and a
complete command-line example for rotating videos quickly without
overloading your CPU.
Prerequisites
To use transpose_opencl, your system must meet two
requirements:
- An FFmpeg build compiled with OpenCL support
(
--enable-opencl). Verify this by runningffmpeg -buildconfand checking for--enable-opencl. - Proper OpenCL drivers and runtimes installed for your graphics hardware (Intel, AMD, or NVIDIA).
Understanding the Filter Pipeline
OpenCL filters operate in GPU memory rather than system memory.
Therefore, processing frames with transpose_opencl requires
a specific filter chain:
format=yuv420p(or appropriate pixel format): Ensures the software pixel format is compatible with the OpenCL implementation.hwupload: Uploads system memory frames to GPU memory.transpose_opencl: Performs the rotation on the GPU.hwdownload: Retrieves the processed frames back to system memory.format=yuv420p: Restores the output pixel format so software encoders (likelibx264) can encode the stream.
Rotation Options (dir)
The dir parameter defines the rotation and transpose
behavior:
0orcclock_flip: Rotate 90 degrees counter-clockwise and vertically flip (default).1orclock: Rotate 90 degrees clockwise.2orcclock: Rotate 90 degrees counter-clockwise.3orclock_flip: Rotate 90 degrees clockwise and vertically flip.
Basic Command Syntax
Initialize the OpenCL device using -init_hw_device and
designate it for filtering with -filter_hw_device:
ffmpeg -init_hw_device opencl=gpu:0.0 -filter_hw_device gpu \
-i input.mp4 \
-vf "format=yuv420p,hwupload,transpose_opencl=dir=clock,hwdownload,format=yuv420p" \
-c:v libx264 -c:a copy output.mp4Rotating 180 Degrees
Because transpose_opencl only supports 90-degree steps
and flips, chain two transpose_opencl filters together to
rotate a video by 180 degrees without multiple memory uploads:
ffmpeg -init_hw_device opencl=gpu:0.0 -filter_hw_device gpu \
-i input.mp4 \
-vf "format=yuv420p,hwupload,transpose_opencl=dir=clock,transpose_opencl=dir=clock,hwdownload,format=yuv420p" \
-c:v libx264 -c:a copy output.mp4Device Selection
If your machine has multiple GPUs (for example, an integrated GPU and
a discrete GPU), specify the correct device index in
-init_hw_device opencl=gpu:X.Y, where X is the
platform index and Y is the device index. Running
clinfo in your terminal will help identify the correct
indices for your hardware.