Transcode HEVC with NVDEC and FFmpeg on Linux

This guide provides a straightforward walkthrough on how to leverage NVIDIA’s hardware-accelerated decoder (NVDEC) within FFmpeg on Linux to decode HEVC (H.265) video and transcode it. By offloading the decoding process to your NVIDIA GPU, you can drastically reduce CPU utilization and speed up transcoding times.

Prerequisites

Before proceeding, ensure your Linux system has the following installed: * NVIDIA Proprietary Drivers: Ensure you have the official NVIDIA drivers installed (version 418.30 or newer recommended). * CUDA Toolkit: Required for hardware-accelerated processing. * FFmpeg with CUDA/NVDEC Support: Your FFmpeg binary must be compiled with --enable-cuda, --enable-cuvid, and --enable-nvenc flags.

You can verify if your FFmpeg installation supports NVDEC by running:

ffmpeg -decoders | grep nvdec

Look for hevc_nvdec or hevc_cuvid in the output.

Command for Full Hardware Transcoding (GPU to GPU)

To achieve the fastest transcoding speeds, you should decode the HEVC video on the GPU using NVDEC, keep the video frames in the GPU memory, and encode the output using NVENC (NVIDIA’s hardware encoder).

Run the following command to transcode an HEVC video to H.264 using full hardware acceleration:

ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input_hevc.mp4 -c:v h264_nvenc -preset fast -b:v 5M output.mp4

Command Breakdown:

Command for Hardware Decoding to Software Encoding (GPU to CPU)

If you require the high quality of a software encoder like libx264 or libx265 but still want to offload the decoding process to the GPU, omit the -hwaccel_output_format cuda flag. This copies the decoded frames back to system memory for the CPU encoder:

ffmpeg -hwaccel cuda -i input_hevc.mp4 -c:v libx264 -crf 20 output.mp4

In this command, NVDEC handles the heavy lifting of decoding the HEVC input, while the CPU handles the compression using the x264 software library.