CUDA GPU Accelerated h264/h265/HEVC Video Encoding with FFMPEG
High Quality FFMPEG Video Encoding
High quality is not always easy to define. I would define high quality as equal or near same quality as the original and this is the goal.
The commandline tool FFMPEG offers great flexibility when it comes to video and audio encoding with a great advantage: it’s free of use.
Prerequisites for High Quality – High Efficiency Video Encoding
- Source Movie File: DNxHD 4:2:2 10-bit or ProResHQ / Cineform with minimum video bitrate 185mbit movie in HD resolution or better
- ffmpeg Commandline Video Encoder
- NVidia GPU at least Kepler generation like GTX 6xx/7xx or higher with minimum driver version v390.77 (or higher).
Quality
Using the preset option delivers quality that easyily surpasses Adobe Media Encoder, especially colorbanding and macroblocks artifacts reduced significantly with ffmpeg when using the h264 codec with the -crf option.
Speed
With ffmpeg and a decent GPU (like a Quadro P4000) the h265/HEVC encoder finaly gets a good speedboost with up to 90fps when encoding HD movies with the below parameter values and 200fps when using the GPU accelerated h264 encoder.
This speed is achieved with a Quadro P4000 from NVidia when encoding 1920×1080 movies with -preset slow. The GTX 780 Ti still encodes with 50-60fps in FullHD with the h264 hardware GPU encoder with high quality settings – currently a graphics adapter with good good speed/cost factor would be a GTX 1070 or GTX 1080.
Encoding high quality h264 video with CPU only:
Parameters
-preset slow preset for HQ encoding (see x264 preset profiles below)
-tune film preset for film content (see x264 tune profiles below)
-crf 16 constant quality factor (lower is better – good values for HD are 14-19)
-a aac AAC audio codec is most common for MP4 movie files
NOTE: Use -crf if bitrate is not so important and the quality factor is more important. Choose -b:v if you need a limitation for the bitrate and size of the video.
Encoding high quality h264 video via GPU:
Parameters
-hwaccel cuvid uses NVidia CUDA GPU acceleration for decoding (also working: dxva2)
-c:v h264_nvenc uses NVidia h264 GPU Encoder
-pix_fmt yuv420p 4:2:0 color subsampling
-preset slow HQ gpu encoding
-rc vbr_hq uses RC option to enable variable bitrate encoding with GPU encoding
-qmin:v 19 -qmax:v 14 sets minimum and maximum quantization values (optional)
-b:v 6M -maxrate:v 10M sets average (target) and maximum bitrate allowed for the encoder
NOTE: There are a lot more options for GPU hardware encoding/decoding. With the options above the GPU is used for DECODING and ENCODING – these are the most reliable GPU encoding options. This way it is possible to process the frames, for example with the -vf option.
Encoding high quality h265/HEVC 10-bit video via GPU:
Parameters
-hwaccel cuvid uses NVidia CUDA GPU acceleration for decoding (also working: dxva2)
-pix_fmt p010le YUV 4:2:0 10-bit
-c:v hevc_nvenc uses HEVC/h265 GPU hardware encoder
-preset slow HQ gpu encoding
-rc vbr_hq uses RC option to enable variable bitrate encoding with GPU encoding
-qmin:v 19 -qmax:v 14 sets minimum and maximum quantization values (optional)
-b:v 6M -maxrate:v 10M sets average and maximum bitrate allowed for the encoder
NOTE: HEVC 10-bit has very good quality when playing with an advanced media player like Media Player Classic – Home Cinema (supports 10-bit playback on supported graphics cards like Quadro P4000 and supported displays like HP Dreamcolor), but because Youtube and Vimeo lacks 10-bit support, the h264 8-bit movies look better on the online video platforms once uploaded there.
FFMPEG -preset slow Parameters: