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 nvdecLook 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.mp4Command Breakdown:
-hwaccel cuda: Activates CUDA hardware acceleration, automatically selecting NVDEC for decoding the input video.-hwaccel_output_format cuda: Keeps the decoded video frames inside the GPU’s memory (VRAM) to prevent costly memory transfers between the GPU and CPU.-i input_hevc.mp4: Specifies the input HEVC video file.-c:v h264_nvenc: Selects the NVIDIA NVENC H.264 encoder for the output video. (You can change this tohevc_nvencif you want the output to also be HEVC).-preset fast: Adjusts the encoder preset for a balance between speed and quality.-b:v 5M: Sets the target video bitrate to 5 Mbps.
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.mp4In this command, NVDEC handles the heavy lifting of decoding the HEVC input, while the CPU handles the compression using the x264 software library.