How to Use transpose_opencl in FFmpeg

Rotating videos in FFmpeg using the CPU can be resource-intensive and slow, especially for high-resolution files. This article provides a straightforward guide on how to leverage your GPU using the transpose_opencl filter in FFmpeg to perform hardware-accelerated video rotation quickly and efficiently.

Prerequisites

Before using OpenCL filters, ensure your system meets the following requirements: 1. FFmpeg Build: Your FFmpeg binary must be compiled with OpenCL support (verify this by running ffmpeg -protocols and checking for OpenCL, or ensure --enable-opencl was used during compilation). 2. GPU Drivers: You must have the appropriate OpenCL runtime drivers installed for your graphics card (Intel, AMD, or NVIDIA).

The Basic Command Structure

Because OpenCL runs on the GPU, you must initialize the GPU hardware device, upload the video frames from system memory (RAM) to GPU memory (VRAM), apply the rotation filter, and then download the frames back to system memory.

Here is the standard command template:

ffmpeg -init_hw_device opencl=gpu -filter_hw_device gpu -i input.mp4 -vf "hwupload,transpose_opencl=dir=clock,hwdownload,format=yuv420p" -c:a copy output.mp4

Parameter Breakdown

Available Rotation Directions (dir)

The transpose_opencl filter accepts the dir parameter to specify how the video should be rotated:

Value Name Description
0 cclock_flip Rotate 90 degrees counter-clockwise and flip vertically.
1 clock Rotate 90 degrees clockwise.
2 cclock Rotate 90 degrees counter-clockwise.
3 clock_flip Rotate 90 degrees clockwise and flip vertically.

You can use either the numerical value or the name. For example, to rotate 90 degrees counter-clockwise:

transpose_opencl=dir=cclock

How to Rotate 180 Degrees

The transpose_opencl filter does not have a native 180-degree rotation option. To achieve a 180-degree rotation, you must chain two 90-degree transpose filters together within the GPU memory space:

ffmpeg -init_hw_device opencl=gpu -filter_hw_device gpu -i input.mp4 -vf "hwupload,transpose_opencl=dir=clock,transpose_opencl=dir=clock,hwdownload,format=yuv420p" -c:a copy output.mp4