CUDA GPU Accelerated h264/h265/HEVC Video Encoding with ffmpeg

High Quality FFMPEG Video Encoding

High quality is always not 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 with at least Kepler generation like GTX 6xx/7xx 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.

Compression comparison: 700x400px 1:1 crop from a 1920×1080 sequence. Booth are encoded with 6 Mbit average bitrate – fot the ffmpeg version, the -crf option was used and for the AME version, the CBR option was used to encode. See the difference on the arm in motion.

CUDA GPU Accelerated h264/h265/HEVC Video Encoding with ffmpeg
ffmpeg gpu hardware acceleration
Left: ffmpeg -crf / Right: Premiere Adobe Media Encoder CBR

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:

ffmpeg.exe -i inmovie.mov -c:v libx264 -pix_fmt yuv420p -preset slow -tune film -crf 19 -c:a aac -b:a 224k outmovie.mp4

 

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:

ffmpeg.exe -hwaccel cuvid -i inmovie.mov -c:v h264_nvenc -pix_fmt yuv420p -preset slow -rc vbr_hq -b:v 6M -maxrate:v 10M -c:a aac -b:a 224k outmovie.mp4

 

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:

ffmpeg.exe -hwaccel cuvid -i inmovie.mov -pix_fmt p010le -c:v hevc_nvenc -preset slow -rc vbr_hq -b:v 6M -maxrate:v 10M -c:a aac -b:a 240k outmovie.mp4

 

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 does have a 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:

cabac=1 / ref=5 / deblock=1:0:0 / analyse=0x3:0x113 / me=umh / subme=8 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=3 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=2 / b_bias=0 / direct=3 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=50 / rc=crf / mbtree=1 / crf=23.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00

 

Sources and Detailed Infos

FFMPEG Hardware Acceleration Introduction  
ffmpeg GPU Acceleration by NVidia  

@