FFmpeg overlay_cuda Guide for NVIDIA GPU PiP

This article explains how to use the overlay_cuda filter in FFmpeg to create hardware-accelerated picture-in-picture (PiP) video overlays using NVIDIA GPUs. You will learn the necessary prerequisites, the core command-line syntax, and how to keep the entire decoding, scaling, compositing, and encoding pipeline on the GPU to maximize performance and minimize CPU overhead.

Prerequisites

To use overlay_cuda, your system must meet the following requirements: * An NVIDIA GPU that supports NVDEC (hardware decoding) and NVENC (hardware encoding). * NVIDIA proprietary graphics drivers installed. * A build of FFmpeg compiled with CUDA support (--enable-cuda-nvcc and --enable-libnpp).

The FFmpeg Command

To perform hardware-accelerated picture-in-picture processing, you must decode both input videos into CUDA memory, scale the overlay video (if necessary) on the GPU, apply the overlay_cuda filter, and then encode the output using h264_nvenc or hevc_nvenc.

Here is the standard command template:

ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i background.mp4 \
       -hwaccel cuda -hwaccel_output_format cuda -i pip_video.mp4 \
       -filter_complex "[1:v]scale_cuda=w=480:h=270[pip];[0:v][pip]overlay_cuda=x=1400:y=50" \
       -c:v h264_nvenc -y output.mp4

Command Breakdown

Advanced Options

The overlay_cuda filter supports specific parameters to control the layout and timing: