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:

  1. An FFmpeg build compiled with OpenCL support (--enable-opencl). Verify this by running ffmpeg -buildconf and checking for --enable-opencl.
  2. 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:

  1. format=yuv420p (or appropriate pixel format): Ensures the software pixel format is compatible with the OpenCL implementation.
  2. hwupload: Uploads system memory frames to GPU memory.
  3. transpose_opencl: Performs the rotation on the GPU.
  4. hwdownload: Retrieves the processed frames back to system memory.
  5. format=yuv420p: Restores the output pixel format so software encoders (like libx264) can encode the stream.

Rotation Options (dir)

The dir parameter defines the rotation and transpose behavior:

  • 0 or cclock_flip: Rotate 90 degrees counter-clockwise and vertically flip (default).
  • 1 or clock: Rotate 90 degrees clockwise.
  • 2 or cclock: Rotate 90 degrees counter-clockwise.
  • 3 or clock_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.mp4

Rotating 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.mp4

Device 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.