How to Use transpose_opencl Filter in FFmpeg

This guide provides a straightforward explanation of how to use the transpose_opencl filter in FFmpeg to rotate or flip videos using GPU acceleration. You will learn the system requirements, the necessary command-line syntax, and practical examples of different transposition directions to help optimize your video processing workflows.

Prerequisites

To use OpenCL-accelerated filters in FFmpeg, your system must meet the following requirements:

  1. FFmpeg build: Your FFmpeg binary must be compiled with OpenCL support (--enable-opencl).
  2. GPU Drivers: Compatible OpenCL drivers (for Intel, AMD, or NVIDIA GPUs) must be installed on your system.

Basic Syntax and Device Initialization

Unlike standard CPU-based filters, OpenCL filters require you to initialize the hardware device and upload the video frames to the GPU memory before processing, and then download them back to system memory afterward.

The basic command structure is:

ffmpeg -init_hw_device opencl=gpu:0.0 -filter_hw_device gpu -i input.mp4 -vf "hwupload,transpose_opencl=dir=1,hwdownload,format=yuv420p" output.mp4

Command Breakdown

  • -init_hw_device opencl=gpu:0.0: Initializes the OpenCL hardware device. gpu:0.0 refers to the first platform and first GPU device.
  • -filter_hw_device gpu: Instructs FFmpeg to use the initialized hardware device for the filter graph.
  • hwupload: Uploads the input video frames from system memory to GPU memory.
  • transpose_opencl=dir=1: Applies the rotation/flip filter on the GPU.
  • hwdownload: Downloads the processed video frames from GPU memory back to system memory.
  • format=yuv420p: Converts the pixel format back to a widely compatible format like YUV 420p for the output encoder.

Transposition Directions (dir parameters)

The dir option determines how the video is rotated or flipped. You can use either the numerical value or the string name:

  • 0 or cclock_flip: Rotate 90 degrees counter-clockwise and flip vertically.
  • 1 or clock: Rotate 90 degrees clockwise (standard portrait rotation).
  • 2 or cclock: Rotate 90 degrees counter-clockwise.
  • 3 or clock_flip: Rotate 90 degrees clockwise and flip vertically.

Example: Rotating a Video 90 Degrees Clockwise

To rotate a landscape video to portrait mode (90 degrees clockwise), run the following command:

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

(Note: -c:a copy is used here to copy the audio stream without re-encoding, saving time and preserving quality).