FFmpeg’s NVEnc encoder relies on the CPU for decoding source formats like MPEG-2 Transport Stream because NVIDIA’s hardware acceleration stack does not support direct hardware decoding of this specific codec. To offload more work to the GPU, you must use a decodable format such as H.264 or HEVC with NVDEC, or accept that CPU-based decoding is required for legacy MPEG-2 inputs.
Understanding the Hardware Acceleration Stack
When you use FFmpeg with NVIDIA GPUs, it is crucial to distinguish between two separate processes: decoding and encoding. Many users assume that adding a flag like-c:v hevc_nvenc will accelerate the entire pipeline, but this only applies to the final step of creating the video file. The first step involves reading the input file and converting raw data into a format the GPU can understand.
For NVIDIA cards, the encoding side uses NVEnc (NVENC), which is a dedicated hardware block on the GPU. However, the decoding side uses NVDEC. If your source file is in a format that NVDEC cannot handle, FFmpeg falls back to software decoding using your CPU cores. This creates a bottleneck where the GPU sits idle while waiting for the CPU to process the frames.
The original issue stems from attempting to decode MPEG-2 TS files. While NVENC supports HEVC output, NVDEC does not support MPEG-2 decoding. Therefore, even if you specify hardware encoding, the heavy lifting of parsing the .ts container and decompressing the MPEG-2 stream happens entirely on the CPU. This explains why monitoring tools like NVTop show low GPU usage during the initial stages of conversion.
Supported Decoding Formats for Full GPU Offloading
To achieve true end-to-end hardware acceleration, both the input and output codecs must be supported by NVIDIA’s respective hardware blocks. You should switch your workflow to use formats that are fully compatible with NVDEC for decoding and NVENC for encoding. Common supported combinations include:- H.264 Input to H.264 Output: Both decoding and encoding are handled by hardware.
- HEVC Input to HEVC Output: Fully accelerated path for modern high-efficiency workflows.
- H.264 Input to HEVC Output: Allows you to compress existing H.264 archives into smaller HEVC files using full hardware assistance.
Optimizing FFmpeg Commands for Available Hardware
Once you have identified a supported input format, you can construct FFmpeg commands that maximize GPU utilization. The key is to explicitly tell FFmpeg to use the CUDA context for both decoding and encoding. For example, if you are converting an H.264 MP4 file to HEVC, use the following command structure:ffmpeg -hwaccel cuda -i input.mp4 -c:v hevc_nvenc -preset p4 output.hevc
Here, -hwaccel cuda instructs FFmpeg to use the CUDA API for hardware-accelerated decoding. The -c:v hevc_nvenc flag selects the NVIDIA hardware encoder. The -preset parameter controls the trade-off between speed and quality; p4 is a balanced choice for general use.
If you are dealing with large files, consider adding -threads 0 to allow FFmpeg to automatically determine the optimal number of threads for the CPU components that remain in the pipeline, such as color space conversion or scaling.
For advanced users who need to scale resolution during the conversion, ensure you use filters that support hardware contexts. The scale_npp filter is often cited in online forums, but it is important to verify its availability in your specific FFmpeg build. In many standard builds, the default scale filter will automatically utilize hardware acceleration if the input and output formats are compatible with the CUDA context.
Verifying GPU Usage
To confirm that your GPU is actively participating in the process, monitor the system using NVTop or NVIDIA’snvidia-smi tool. Look for active sessions under the "Video Encode" and "Video Decode" columns. If you see activity in "Video Decode," your input format is being handled by hardware. If the "Video Decode" column is empty but "Video Encode" shows activity, your source is still being decoded by the CPU.
This diagnostic step helps isolate whether the bottleneck is the decoder or the encoder. In the case of MPEG-2 TS, you will consistently see zero activity in "Video Decode" regardless of your FFmpeg flags, confirming that the limitation is at the codec level, not the configuration level.
Alternative Approaches for Legacy Content
If you must process MPEG-2 TS files and cannot change the source format, you can try to minimize CPU overhead by using multi-threaded software decoding. FFmpeg automatically utilizes all available CPU cores for software decoding, so ensuring your system has sufficient core count is vital. You can also experiment with different CPU thread allocation settings within FFmpeg to prevent resource contention with other system tasks. However, do not expect GPU acceleration here, as the hardware simply lacks the necessary circuitry for MPEG-2 decoding. For high-volume production environments involving legacy tape formats or old broadcasts, consider transcoding these assets to a modern, hardware-decodable format like H.264 or ProRes as a first pass. This one-time cost allows subsequent edits and exports to leverage full GPU acceleration, significantly speeding up the overall workflow compared to real-time MPEG-2 processing.Frequently Asked Questions
Why does my GPU usage stay low even with hevc_nvenc?
Your GPU usage remains low because the input format (MPEG-2) is not supported by NVIDIA's NVDEC hardware decoder. Consequently, the CPU handles the entire decoding process before passing frames to the GPU for encoding. Switching to a supported input format like H.264 or HEVC resolves this bottleneck.
Can I force FFmpeg to use hardware decoding for MPEG-2?
No, you cannot force hardware decoding for MPEG-2 on NVIDIA GPUs because the NVDEC engine does not support this legacy codec. You must rely on CPU-based software decoding or convert the source material to a supported format first.
What is the difference between hwaccel cuda and hwaccel nvdec?
The hwaccel cuda flag is the standard method in modern FFmpeg versions to enable hardware-accelerated decoding via the CUDA interface. It automatically selects the appropriate backend (like NVDEC) based on the input codec. Using explicit device flags is generally unnecessary unless debugging specific compatibility issues.
How do I check if my FFmpeg build supports NVDEC?
You can check support by running ffmpeg -hwaccels in your terminal. If cuda appears in the list, your build supports hardware acceleration. You can also test decoding a known supported file with -hwaccel cuda and monitor GPU usage with NVTop to confirm activation.
Are there any risks in using hardware encoding over software?
Hardware encoding is generally safe and offers better performance for real-time streaming or quick exports. However, some software encoders like x264 may offer slightly better quality-per-bitrate ratios at very low bitrates. For most users, the speed and efficiency gains of NVENC outweigh minor quality differences.