Issue
The video encoding process in the pipeline was significantly slower than the frame rendering stage, causing a severe bottleneck. Despite achieving a high frame rendering speed of 104 frames per second in parallel, the final video encoding stage operated at only 7-8 frames per second, leading to a total processing time of 20-25 minutes for 15MB video files. During this process, CPU utilization remained low (10-30%), and the GPU was entirely idle, rendering the parallel optimizations ineffective for batch processing of multiple videos.
Culprit
The primary issue was the use of CPU-based libx264 encoding, which does not leverage GPU acceleration. The FFmpeg configuration relied on CPU-only encoding with a libx264 codec, despite the availability of GPU resources. Even with the use of 32 CPU threads, the encoding remained inefficient for screencast content (such as static backgrounds and syntax-highlighted code), leading to suboptimal performance.
Solution
To address the bottleneck, the system was updated to use NVIDIA NVENC for hardware-accelerated GPU encoding. This involved:
Hardware-Accelerated Encoding
- Replaced the CPU-based
libx264codec with the NVIDIA NVENC encoder (h264_nvenc), which leverages GPU resources for significantly faster encoding. - Configured the encoder with a preset (
p4) that balances speed and quality, suitable for screencast content.
Dynamic Configuration
- Introduced environment variables to allow for tunable quality and speed settings, such as:
NVENC_PRESET: Allows selecting between different encoding presets (e.g.,p4for balanced performance).NVENC_CQ: Enables Constant Quality mode to maintain consistent visual quality across different resolutions.
Technical Improvements
- Removed CPU threading: NVENC does not utilize CPU threads, eliminating the need for thread configuration.
- Variable Bitrate (VBR): Enabled automatic bitrate adjustment based on content complexity, optimizing file size without compromising quality.
- Constant Quality (CQ): Maintained consistent quality across different video resolutions, ensuring visually lossless output.
Performance Impact
The transition to NVENC encoding resulted in substantial improvements:
- Encoding Speed: Increased from 7-8 fps to 25-60 fps (6-8x faster).
- Total Processing Time: Reduced from 20-25 minutes to 5-8 minutes (3-4x faster).
- CPU Usage: Decreased from 10-30% to 5-10%.
- GPU Utilization: Increased from 0% to 10-15%.
- File Size: Remained consistent (~30MB for 1080p videos).
Verification
Before deploying NVENC encoding, the system checks for the availability of NVENC support in FFmpeg and ensures the NVIDIA drivers are up to date (minimum driver version: 418.81 for Pascal/Turing GPUs). FFmpeg must be compiled with NVENC support enabled (--enable-nvenc).
This solution ensures that the video encoding process leverages GPU resources efficiently, reducing bottlenecks and improving overall pipeline performance.