ffmpeg with nvenc

December 27, 2017

When encoding a video with the x264 encoder, I did not feel right noticing the CPU usage spiked and hovered around 95% and above while GPU usage did not go up at all from 2%. And I thought “this universe would be a better place if I could use the GPU for video encoding”. An experiment sounds nice, right?

Usually and in almost all cases, this is the command I use to utilize the libx264 encoding library:

ffmpeg -i input.mov -c:v libx264 -c:a aac output.mp4

Where -c:v defines the video codec which is libx264 and -c:a defines the audio codec which is aac. When I was writing this piece, I learned that a video wrapped in MKV container can utilize the Opus video codec, which seems to perform better at a lower bitrate. The recommended range for Opus is 64kbps and above, whereas 128kbps and above for AAC.

By default, libx264 encoder makes a full use of the CPU and generally it is pretty slow. How slow? On an R5 1400 box (4c/8t) when encoding a video file, the speed is typically around 2.50x - 3.00x as shown below (using FFmpeg Zeranoe build for Windows).

ffmpeg with CPU FFmpeg Zeranoe with libx264.

Now, let’s get our GPU to work. Heads up: only Nvidia cards with this command. Sorry AMD users, it is kind of complicated as of now.

ffmpeg -i input.mov -c:v h264_nvenc -c:a aac output.mp4

NVENC is the API that enables a user to use Nvidia GPU to perform video encoding. H.264 AVC (h264_nvenc) and H.265 HEVC (hevc_nvenc) are supported. What about the speed gained from this NVENC? Pretty darn good.

ffmpeg with Nvidia GPU FFmpeg Zeranoe with h264_nvenc.

What about the CPU and GPU usage? CPU usage would hover around 45-52% while the GPU usage would be in the neighborhood of 30%.

GPU stat GPU Video Engine = 30.0% usage.

This is certainly a good thing. Not only I can spend more CPU power for something else, it also does not put a huge stress on the GPU. What a win!