How to install / compile NVENC in ubuntu?

32,641

Solution 1

As always with FFmpeg continuing development shifts the goal posts many times and this is certainly true of nvenc and FFmpeg. There have been 3 phases:

  1. Roll your own: Pre August 27th 2016 it was necessary to track down your own headers to build nvenc. Arduous and more than a little confusing.
  2. Built into the source: August 27th 2016 saw the required headers incorporated as part of the FFmpeg source, nothing required but the ability to compile the FFmpeg source from git, or use a 'release' version cut from git at this time.
  3. Use nv-codec-headers: On February 26th 2018 the NVidia headers were removed from the FFmpeg source. A separate git repository was created to hold continuing work with these headers. At the moment these headers must be installed as well as FFmpeg from git to get access to nvenc and friends.

Two steps are required:

1. Install the nv-codec-headers package:

Something like the following Terminal commands should suffice:

sudo apt-get install make git
mkdir $HOME/nv-codec-headers_build && cd $HOME/nv-codec-headers_build
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
make && sudo make install

If for some reason you change your mind and wish to completely remove the installed files as well as the build directory simply run the following two Terminal commands:

sudo rm -v /usr/local/{lib/pkgconfig/ffnvcodec.pc,include/ffnvcodec/*.h}
rm -rfv $HOME/nv-codec-headers_build

And your system has been cleaned. If however you wish to continue use the following step:

2. Compile FFmpeg:

Use this very well tested guide:

The nv-codec-headers will be automagically recognised (no extra ./configure options are needed) and nvenc will be successfully built.

Testing:

I have tested this on Bionic Beaver LTS and the results are:

andrew@ilium:~$ ffmpeg -encoders 2>/dev/null | grep nvenc
 V..... h264_nvenc           NVIDIA NVENC H.264 encoder (codec h264)
 V..... nvenc                NVIDIA NVENC H.264 encoder (codec h264)
 V..... nvenc_h264           NVIDIA NVENC H.264 encoder (codec h264)
 V..... nvenc_hevc           NVIDIA NVENC hevc encoder (codec hevc)
 V..... hevc_nvenc           NVIDIA NVENC hevc encoder (codec hevc)
andrew@ilium:~$

And if you see a similar picture as above, and you have a supported graphics card, you are right to go :)

Notes:

  • The latest version corresponds to Video Codec SDK version 11.0.10 and requires NVidia drivers version 455.28 or newer.
  • Older versions: There is now an automated github copy of the nv-codec-headers which also creates tarballs perfect for using an older version of the headers. Great if you want the headers to sync with an older version of the NVidia drivers that you are unable or unwilling to update.
  • Hardware Acceleration Guide for FFmpeg: FFmpeg Wiki guide for hardware acceleration in general with FFmpeg, a canonical section for compiling and using nvenc...

Solution 2

Looks like that according to Focal's changelog that nv-codec-headers are now included.

ffmpeg (7:4.2.2-1ubuntu1) focal; urgency=medium

  • Build with external nv-codec-headers for NVENC accelerated video encoding. (LP: #1810649)

    • debian/control:
      • Add B-D libffmpeg-nvenc-dev
    • debian/rules:
      • Add --enable-nvenc for amd64 only

    -- Martin Wimpress Sat, 07 Mar 2020 08:39:35 +0000

Solution 3

@andrew.46 your response is great, thank you. My reputation is below the threshold to post this as a comment. If possible, please expand your response with the following information.

If you need to revert to an earlier version of nv-codec-headers, download and compile an earlier release from https://github.com/FFmpeg/nv-codec-headers/releases.

This may be necessary if you compile ffmpeg against latest nv-codec-headers, but your graphics driver is of a version that does not yet handle the minimum CUDA API version required by nv-codec-headers. The symptom will be that ffmpeg will fail transcoding video streams. In my case nv-codec-headers required CUDA API 9.1, while the latest nvidia-driver for my distribution (testing) was still at a version using 9.0.

Reverting to an earlier nv-codec-headers is probably safer than downloading the latest driver directly from Nvidia and building it outside of your distribution's update mechanism.

Share:
32,641

Related videos on Youtube

Fast OS
Author by

Fast OS

Updated on September 18, 2022

Comments

  • Fast OS
    Fast OS almost 2 years

    I see support for nvidia NVENC in this document, but it seems confusing, some step by step? or PPA with ffmpeg-nvenc for ubuntu?

    http://developer.download.nvidia.com/compute/redist/ffmpeg/1511-patch/FFMPEG-with-NVIDIA-Acceleration-on-Ubuntu_UG_v01.pdf

  • The Unknown Dev
    The Unknown Dev about 5 years
    Thanks so much for this detailed guide. I got similar output from your testing section, which is great. Aside from the nVidia drivers, do we also need to install the Cuda SDK to get FFMPEG to use the installed GPU?
  • andrew.46
    andrew.46 about 5 years
    @KimberlyW Not required on my system, I have just now tested...
  • Freedo
    Freedo about 5 years
    @andrew.46 I did what you said but I still don't have the hardware acceleration for vp9 :/
  • andrew.46
    andrew.46 about 5 years
    @Freedo Unfortunately nvenc is for H.264 and HEVC encoding...
  • Freedo
    Freedo about 5 years
    @andrew.46 do you know how I can get vp9 encoding? I have a GTX 1080p which supports it according to this developer.nvidia.com/video-encode-decode-gpu-support-matrix
  • andrew.46
    andrew.46 about 5 years
    @Freedo Hmmm... I don't know much about this but I suspect you are after vp9_vaapi. Perhaps set a new question and hope that my colleague llogan steps in...
  • Freedo
    Freedo about 5 years
    @andrew.46 I did askubuntu.com/questions/1143973/… is for CPU I believe
  • andrew.46
    andrew.46 about 5 years
    @Freedo And so my ignorance is confirmed :)
  • andrew.46
    andrew.46 over 4 years
    Thanks! I have added this in to the 'Notes' section...
  • DPS
    DPS over 4 years
    Beware that, at the end of the tutorial over on the ffmpeg site, there is a command to remove the packages that you installed when you followed along with the tutorial. It nearly just wiped out my entire system. It says that a lot of system packages, including ubuntu-standard and things like programs that I have installed are "no longer needed and can be removed". Much much more than what I described here. and of course, I just hit enter thinking that NO was the default response. I hate myself
  • andrew.46
    andrew.46 over 4 years
    @DPS Ouch!! But not the simple uninstall of the nv-codec-headers that I have given in this answer?
  • DPS
    DPS over 4 years
    Haha no, not yours. It was the one at the bottom of the "Compile FFmpeg" page that you linked, with the header "Reverting Changes made by this Guide". I'll never make that mistake again (yeah right). lol
  • andrew.46
    andrew.46 about 4 years
    Always a little sad when an answer such as mine starts to become obsolete :). I have voted up...