How to Use the FFmpeg overlay_opencl Filter

This article provides a quick guide on how to use the overlay_opencl filter in FFmpeg to overlay one video or image onto another using GPU acceleration. You will learn the necessary OpenCL hardware initialization steps, how to upload video frames to the GPU, how to apply the overlay filter with custom coordinates, and how to download the final output back for encoding.

To use OpenCL filters in FFmpeg, you cannot simply pass standard software video streams directly to the filter. You must initialize your OpenCL hardware device, upload the input streams to GPU memory, apply the OpenCL filter, and then download the results back to system memory.

The Basic Command Structure

Here is a complete command-line example that overlays a secondary video (or image) onto a main video at specific coordinates (X=10, Y=10) using OpenCL:

ffmpeg -init_hw_device opencl=gpu -filter_hw_device gpu \
-i main_video.mp4 -i overlay_image.png \
-filter_complex "[0:v]format=nv12,hwupload[main]; [1:v]format=nv12,hwupload[over]; [main][over]overlay_opencl=x=10:y=10[out]; [out]hwdownload,format=nv12" \
-c:v libx264 -an output.mp4

Command Breakdown

Supported Parameters

The overlay_opencl filter supports several parameters to control positioning:

By utilizing these steps, you can offload the CPU-intensive video overlay process directly to your graphics hardware, significantly increasing rendering speeds.