How to Get NVIDIA’s GPU Assisted Video Encoding (NVENC) to Work in Ubuntu

Encoding video files can be a complete drag. Even with the fastest CPUs out on the market, the process can take up to about as long as the video itself (e.g. thirty minutes of encoding for a thirty-minute video). No matter what encoding programs offer you, they are always limited by the power of your chips.

To solve this issue, NVIDIA cards have a nifty little trick that they’ve been using since the release of the GeForce 600 series graphics cards back in 2012 called NVENC. This allows you to use the GPU to encode video streams. Using NVENC, not only can you convert videos more quickly than with a conventional CPU, but you can also stream video through software like Open Broadcaster Software (OBS) while reducing or completely eliminating stutter and lag. Since Linux doesn’t have NVENC by default, you’re going to have to get it working by hand. I explain here how you can do this in distributions that are based on Ubuntu.

To get NVIDIA’s NVENC working on Ubuntu, you first need to download the NVIDIA Video Codec software development kit (SDK) from this site. You will need to make an account, but the process is pretty straightforward. You don’t need all the files in the ZIP, so we’re now going to navigate to “Samples/common/inc.” Copy everything you see there to your “/usr/local/include” folder. You need to copy these files so that “ffmpeg” (an encoder frequently found with linux distros) will recognize NVENC and incorporate it.

nvenclinux-softwaresources

The next step will involve recompiling your current version of ffmpeg. This means that you’re going to need the build dependencies for the software, and for that you will have to configure Linux to allow downloading from source code repositories in your software sources.

In Ubuntu, you can navigate to “System Settings -> Software & Updates,” then click “Source code” under “Ubuntu Software.” In Linux Mint (the distribution I’m using because I’m lazy), this can be done by accessing the Update Manager, clicking “Edit,” clicking “Software Sources,” then checking the little box with “Enable source code repositories.” This method may vary depending on your desktop environment and distribution of Linux, so you may need to do a little soul searching to get this done.

Now for the tooth-grinding part of the process!

Since ffmpeg doesn’t build with many of its options automatically enabled (meaning they’re almost all opt-in rather than opt-out), you’ll have to find out how it was built in your system and then build it using the same configuration plus support for NVENC and AAC audio. This is where the headers you copied earlier come in. See, ffmpeg will take a little peek at your files in “/usr/local/include” to find any codecs it doesn’t find in its own pockets.

First, we build dependencies with

Next, we want to download the AAC audio codec with

Once those things are done, we will need to get the source to ffmpeg’s current version with

And when that’s done, you’re going to have to do a little hunting in your Home folder. Find a folder that starts with “ffmpeg.” It will have the version number right after it separated by a dash. Here’s mine:

nvenclinux-ffmpeg

Open your terminal in that folder. If you can’t do that, open the terminal and type:

where “<version>” represents the full version number you saw in that folder’s name. In my case, I would type

Now type

It will show you the entire configuration of your current ffmpeg installation. You’ll need to copy everything after “configuration:” without the bits below that massive wall of text. In my case, I’m copying this: (Your configuration might be different, so don’t copy what I’m showing you here.)

Now, paste whatever you got into gedit or some other text editor and add this to the end:

OK, now all you have to do is type

where “<that massive wall of text>” represents the massive wall of text you just edited to add support for non-free codecs, NVENC, and AAC audio. This might take a while.

After all that is finished, type

Wait for that to be over with, then type

You’re done!

OK, so how do you know you’ve successfully gotten support for NVENC? It’s time to take ffmpeg out for a spin and encode a video with it.

To do this, you will need a video file to convert. Let’s say that our video is in “/home/miguel/Desktop/test.mkv,” and we want to encode it as “/home/miguel/Desktop/test.mp4.” Here’s how we go about doing that with ffmpeg and its newfound love for NVENC:

You should see something like this:

nvenclinux-encoding

And that’s it! We killed two birds with one stone: you learned how to get NVENC on your Linux system and how to encode a video using ffmpeg’s handy terminal command. If you get stuck somewhere or find a better way of doing this, be sure to leave a comment!

6 comments

  1. Hi,
    In Ubuntu,
    When i do this step : “./configure ” I’ve got an error :

    ERROR: nvEncodeAPI.h not found.

    Any idea? Thx !

    • To be a bit more precise, you should download the NVIDIA SDK that I mentioned in the “First Things First” section of this article.

      You should get a file called “Video_Codec_SDK_x.x.x” where “x” is some version number. Open the ZIP and navigate to /Samples/common/inc.

      All those files need to be copied to /usr/local/include.

  2. Thanks, works great! Theres a few differences now. I had to download the source from:
    git clone https://anonscm.debian.org/git/pkg-multimedia/ffmpeg.git
    … and the massive wall of text was a bit tricky as I had to put it in a text file and remove all the line breaks. But after it compiled fine, I have nvenc working on Linux Mint 18.2 running on a MacBook Pro with a Geforce 750M GPU. Didn’t need to download the SDK, it just worked.

    Only question I have is, I am able to use -c:v h264_nvenc but it doesn’t seem to recognise h265_nvenc. I get ‘unknown encoder ‘h265_nvenc’. Is there something else I need to compile this with to get h265 support?

  3. Title says ubuntu, uses Mint.

    Can you at the very least use the distro you are supposed to?

Comments are closed.

Sponsored Stories