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.mp4Command Breakdown
-hwaccel cuda: Instructs FFmpeg to use CUDA hardware acceleration for decoding the input file.-hwaccel_output_format cuda: Crucial parameter that forces the decoder to keep the decoded frames in GPU memory (VRAM) instead of copying them back to system memory.-i input.mp4: Specifies the input video file.-vf "scale_cuda=1920:1080": Applies the CUDA-based hardware scaler filter. Because the frames are already in CUDA memory, this filter scales the video directly on the GPU. (Note: Standardscalewill not work here; you must usescale_cudaorscale_npp).-c:v h264_nvenc: Selects the NVIDIA hardware-accelerated H.264 encoder. For H.265/HEVC encoding, usehevc_nvenc.-c:a copy: Copies the audio stream without re-encoding to save CPU cycles.
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.mp4In 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.