How to Use NVDEC for FFmpeg Hardware Decoding
This article provides a straightforward guide on how to perform hardware-accelerated video decoding using NVIDIA’s NVDEC engine within FFmpeg. You will learn the system prerequisites, the core command-line syntax, and practical examples for decoding video streams directly on your NVIDIA GPU to reduce CPU utilization and speed up processing.
Prerequisites
To use NVDEC hardware decoding, your system must meet the following
requirements: * An NVIDIA GPU that supports NVDEC (NVIDIA Kepler
architecture or newer). * Installed NVIDIA proprietary graphics drivers.
* A build of FFmpeg compiled with CUDA and NVDEC support. You can verify
your FFmpeg capabilities by running:
bash ffmpeg -hwaccels Ensure that cuda and
nvdec are listed in the output.
Basic NVDEC Decoding Syntax
To enable NVDEC hardware acceleration, use the -hwaccel
flag before the input file.
The standard flag to initiate hardware acceleration with NVIDIA is:
ffmpeg -hwaccel cuda -i input.mp4 output.mp4Advanced Workflows: GPU Decoding to GPU Memory
By default, -hwaccel cuda decodes the video into GPU
memory, but transfers the frames back to system memory (RAM) for
processing. To keep the decoded frames inside the GPU memory
(VRAM)—which is highly efficient for transcoding—you must specify the
hardware acceleration output format using
-hwaccel_output_format cuda.
Example 1: Full GPU Transcoding (NVDEC to NVENC)
To decode an H.264 video using NVDEC, keep the frames in GPU memory, and re-encode them using NVENC (NVIDIA’s hardware encoder), use the following command:
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:v h264_nvenc output.mp4Example 2: GPU Decoding to CPU Encoding
If you want to decode the video using the GPU to save CPU resources, but want to encode the output using a software encoder like x264 for maximum compatibility or quality, use:
ffmpeg -hwaccel cuda -i input.mp4 -c:v libx264 output.mp4Note: In this case, do not use
-hwaccel_output_format cuda, as the software encoder
requires the frames to be copied back to the system memory.
Specifying the Decoder Manually
While -hwaccel cuda automatically selects the correct
hardware decoder for your input format, you can also force a specific
NVDEC decoder using the -c:v option before the input:
ffmpeg -c:v h264_cuvid -i input.mp4 output.mp4(Common NVDEC decoder names include h264_cuvid,
hevc_cuvid, vp9_cuvid, and
mjpeg_cuvid)