NVDEC FFmpeg H.264 Transcoding on Linux
This guide demonstrates how to use NVIDIA’s hardware-accelerated decoder (NVDEC) with FFmpeg on Linux to transcode H.264 videos. By offloading the video decoding and encoding processes to an NVIDIA GPU, you can dramatically increase transcoding speeds and reduce CPU utilization.
Prerequisites
Before proceeding, ensure your Linux system has the following
installed: * A CUDA-supported NVIDIA Graphics Card. * NVIDIA Proprietary
Drivers (and CUDA Toolkit). * FFmpeg compiled with NVIDIA hardware
acceleration support (--enable-cuda-nvcc,
--enable-libnpp, --enable-ffnvcodec).
To verify if your FFmpeg installation supports CUDA and NVDEC, run:
ffmpeg -hwaccelsLook for cuda in the output list.
Hardware-Accelerated Transcoding Commands
For maximum efficiency, you should keep the entire transcoding pipeline on the GPU. This means using NVDEC to decode the input video, keeping the decoded frames in GPU memory, and using NVENC to encode the output video.
Method 1: Full GPU Pipeline (Recommended)
This command decodes an H.264 input using NVDEC, keeps the frames in CUDA memory, and encodes the output to H.264 using NVENC.
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:v h264_nvenc -preset fast output.mp4Parameter Breakdown: * -hwaccel cuda:
Instructs FFmpeg to use CUDA (NVDEC) for hardware decoding. *
-hwaccel_output_format cuda: Keeps the decoded video frames
in GPU memory (VRAM) instead of copying them back to system memory
(RAM). This prevents CPU-GPU bottlenecks. * -i input.mp4:
The input H.264 video file. * -c:v h264_nvenc: Selects the
NVIDIA NVENC encoder for H.264 output. * -preset fast: Sets
the encoding preset for NVENC.
Method 2: GPU Decoding with CPU Encoding
If you want to decode the H.264 video on the GPU but encode the
output using a CPU-based encoder like libx264 (for maximum
compression efficiency), use the following command:
ffmpeg -hwaccel cuda -i input.mp4 -c:v libx264 -crf 23 output.mp4In this scenario, the decoded frames are automatically copied from GPU memory back to system memory so the CPU encoder can process them.
Method 3: Explicit CUVID Decoding (Alternative)
If -hwaccel cuda does not work with your FFmpeg build,
you can explicitly call the CUVID H.264 decoder wrapper:
ffmpeg -c:v h264_cuvid -i input.mp4 -c:v h264_nvenc output.mp4Note: The -c:v h264_cuvid option must be placed
before the input file -i to instruct
FFmpeg to use that specific decoder for the input.
Verifying GPU Usage
To confirm that NVDEC is active during the transcoding process, open a second terminal window and run:
nvidia-smiLook at the GPU-Util and the
Processes list at the bottom of the output. You should
see ffmpeg listed, and your GPU utilization should reflect
active video decoding and encoding.