Configure NVIDIA Hardware Accelerated FFmpeg Pipeline

Operating a fully hardware-accelerated video processing pipeline in FFmpeg using NVIDIA GPUs significantly reduces CPU overhead and accelerates processing times. This article provides a step-by-step guide on how to configure FFmpeg to decode, scale, and encode video streams entirely on NVIDIA hardware using NVDEC, CUDA filters, and NVENC, ensuring that video frames remain in GPU memory throughout the entire process.

Prerequisites

To utilize full GPU pipeline acceleration, your system must meet the following requirements: * An NVIDIA GPU that supports NVDEC and NVENC. * Up-to-date NVIDIA proprietary graphics drivers. * FFmpeg compiled with support for CUDA (--enable-cuda-nvcc, --enable-libnpp) and NVENC/NVDEC (--enable-ffnvcodec).

The Core Concept: Avoiding Memory Bottlenecks

The key to a high-performance GPU pipeline is preventing video frames from being copied from GPU memory (VRAM) back to system memory (RAM). By default, FFmpeg decodes video to RAM, applies software filters via the CPU, and then uploads the frames back to the GPU for encoding.

To achieve full hardware acceleration, you must force FFmpeg to keep decoded frames in CUDA device memory, apply GPU-based filters, and pass those CUDA frames directly to the hardware encoder.

The Hardware-Accelerated Command

The following command demonstrates a complete pipeline that decodes an input video, downscales it to 1080p, and encodes it to H.264, executing all operations on the NVIDIA GPU:

ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -vf "scale_cuda=1920:1080" -c:v h264_nvenc -c:a copy output.mp4

Command Breakdown

Advanced Configuration: Transcoding to HEVC (H.265)

To scale and encode to HEVC instead of H.264, use the following modified command:

ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -vf "scale_cuda=3840:2160" -c:v hevc_nvenc -preset slow -cq 24 output.mp4

In this variation, the -preset slow flag optimizes encoding efficiency, and -cq 24 sets the Constant Quality parameter for the NVENC encoder to manage output file size while maintaining visual quality.