How to install FFmpeg with NVIDIA GPU acceleration on Linux

See all GNU/Linux related FAQ
The ffmpeg is free and open-source video converter software for Linux and Unix-like systems. However, on Ubuntu/Debian Linux and other distros, NVIDIA hardware-based encoding is disabled at compile time. So, naturally, you need supporting NVIDIA GPU. Apart from that, it would be best if you had CUDA support installed with GNU compilers. Let us see how to install everything one by one on a server or desktop powered by Ubuntu or Debian Linux.

I tested the following instructions on Ubuntu Linux 20.04 LTS. I installed NVIDIA driver version 460 and CUDA version 10.1. My GPU is GeForce GTX 1650. Let us see how to install FFmpeg with NVIDIA GPU hardware acceleration support on Linux.
Tutorial details
Difficulty level Intermediate
Root privileges Yes
Requirements Linux terminal
Category Driver Management
OS compatibility Debian Linux Mint Pop!_OS Ubuntu
Est. reading time 4 minutes

Installing FFmpeg with NVIDIA GPU hardware acceleration on Linux

  1. First, make sure Nvidia Driver (Latest Proprietary Driver) installed on Ubuntu or Debian. For example:
    $ sudo apt install nvidia-driver-525
  2. Reboot the Linux system:
    $ sudo reboot
  3. Next you need to install CUDA tool kit on Debian or Ubuntu Linux using the apt command or apt-get command:
    $ sudo apt install nvidia-cuda-toolkit
    Using FFmpeg with NVIDIA GPU Hardware Acceleration by installing CUDA

    Installing CUDA toolkit (Click to enlarge)

  4. To compile ffmpeg with NVIDIA we need ffnvcodec too. Clone git repo:
    $ mkdir ~/nvidia/ && cd ~/nvidia/
    $ git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
  5. Install ffnvcodec on Ubuntu or Debian:
    $ cd nv-codec-headers && sudo make install
  6. Get ffmpeg source code, run:
    $ cd ~/nvidia/
    $ git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/
  7. Install GNU gcc compiler collection and libs, run:
    $ sudo apt install build-essential yasm cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-dev
  8. Configure ffmpeg with NVIDIA GPU support:
    $ cd ~/nvidia/ffmpeg/
    Before, you start adding encoders and feature read the help page to see which features you need to compile ffmpeg:
    $ ./configure --help | more
    $ ./configure --help | grep -E '(libvpx|ibx264)'

    Now do the minimum compile:
    $ ./configure --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64

    How to add missing encoders

    The following section requires a good understanding of audio/video encoders and their names, such as libx264, libx265, libvpx, etc. I added this note so you can hunt down those libs and compile them per your needs. Typically you search for missing encoders lib using the apt-cache command:
    ## see which features needs to be enable or disable at compile time ##
    $ ./configure --help | grep enable
    $ ./configure --help | grep disable
    $ ./configure --help | less ## Now search for that lib ##
    $ apt-cache search lib_name_here
    $ apt-cache search libx265
    $ apt-cache search libx265 | grep dev

    Then install that package name:
    $ sudo apt install libx265-dev
    Finally, add it ./configure as follows:
    $ ./configure --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 \
    --enable-libx265

    Here is a complete example of adding and compiling encoders per my project’s needs. Feel free to add and remove those libs and compile-time options. The above will build minimum support but certain encoder will be missing such as H.264 video encoder, H.265/HEVC video encoder and so on. If you need them install required tools and dev libs. For example:
    $ sudo apt-get install nasm libx264-dev libx265-dev libnuma-dev libvpx-dev libfdk-aac-dev libopus-dev libghc-gnutls-dev libaom-dev libass-dev libmp3lame libvorbis-dev libvpx-dev libx265-dev libx264-dev
    Now, reconfigure it:
    $ ./configure --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --enable-gpl \
    --enable-gnutls \
    --enable-libaom \
    --enable-libass \
    --enable-libfdk-aac \
    --enable-libfreetype \
    --enable-libmp3lame \
    --enable-libopus \
    --enable-libvorbis \
    --enable-libvpx \
    --enable-libx264 \
    --enable-libx265 \
    --enable-nonfree

  9. Compile it, execute:
    $ make -j $(nproc)
  10. Verify executable, type:
    $ ls -l ffmpeg
    $ ./ffmpeg

    Howto install FFmpeg with NVIDIA GPU acceleration on Linux

    Installing ffmpeg with NVIDIA support and verifying installation (Click to enlarge)

  11. Install newly build ffmpeg into /usr/local/ directory:
    $ sudo make install
  12. Verify ffmpeg path using the ls command and type/command command as follows:
    $ ls -l /usr/local/bin/ffmpeg
    $ type -a ffmpeg
  13. Add /usr/local/bin/ to $PATH using the export command (see adding path permanently on Linux with bash for more info):
    $ echo "$PATH"
    $ export PATH=$PATH:/usr/local/bin
    $ echo "$PATH"

  14. You can now use -hwaccel cuda switch for encoding. For instance:
    $ ffmpeg -y -hwaccel cuda -i input.file output.file
    $ /usr/local/bin/ffmpeg -y -hwaccel cuda -i input.file output.file

How to view NVIDIA gpu stats and load while using the ffmpeg

Use any one of the following command:
$ nvidia-smi
Make sure you try the nvtop command. It is a ncurses-based GPU status viewer for NVIDIA/Intel/AMD GPUs. For example:
$ nvtop

GPU-accelerated video processing with ffmpeg monitoring with nvtop

Click to enlarge

Related: Top 7 Linux GPU Monitoring and Diagnostic Commands Line Tools

Result

In this example, I am converting input.mkv to out.mp4 as follows without using GPU acceleration:
$ time /bin/ffmpeg -y -i /tmp/input.mkv /tmp/out.mp4
Time command outputs with CPU used:

real	1m40.678s
user	16m52.159s
sys	0m7.821s

Also note down the speed:

frame= 7320 fps= 73 q=-1.0 Lsize=   58306kB time=00:02:02.03 bitrate=3914.0kbits/s speed=1.22x    

And now same file with NVIDIA GPU acceleration (note the path, I am calling the compiled version):
$ time ~/ffmpeg/ffmpeg -hwaccel cuda -y -i /tmp/input.mkv /tmp/out.mp4
OR
$ time /usr/local/bin/ffmpeg -hwaccel cuda -y -i /tmp/input.mkv /tmp/out.mp4
Time command outputs with GPU used:

real	0m28.494s
user	1m58.659s
sys	0m3.670s

ffmpeg speed up by 4.32x:

frame= 7320 fps=259 q=31.0 Lsize=   50294kB time=00:02:02.03 bitrate=3376.2kbits/s speed=4.32x  

Mine is a consumer-grade GPU, but no doubt data center or professional-grade NVIDIA GPU gives even better performance. Make sure you read the following man pages using the man command:
$ man ffmpeg
$ man nvtop
$ man nvdia-smi

Summing up

You learned how to install FFmpeg with NVIDIA GPU acceleration hardware support on Debian or Ubuntu Linux to speed up encoding with the ffmpeg command. Please see NVIDIA documentations and ffmpeg wiki page for further information. In short, to enable support for GPU-assisted encoding with an NVIDIA GPU for ffmpeg, you need:

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

29 comments… add one
  • Boris Aug 5, 2023 @ 23:38

    After following this, now my ffmpeg does not work with mp3 or m4a files. Any ideas why?

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Aug 6, 2023 @ 8:43

      Try passing --enable-libmp3lame when compiling. I added a note called “How to add missing encoders”. Please read it carefully. I hope this helps.

  • Tony Diep Sep 4, 2023 @ 22:50

    Thanks for posting this!

    Here are the errors and solutions I got when compiling on Ubuntu 22.04

    ERROR: failed checking for nvcc.
    add /usr/local/cuda/bin/ to path
    

    AND:

    ERROR: gnutls not found using pkg-config
    sudo apt install libgnutls28-dev
    
    AND:
    
    ERROR: libmp3lame >= 3.98.3 not found
    sudo apt install libmp3lame-dev
    
  • H Dec 25, 2023 @ 1:42

    Is it possible to post colab version? It would especially extremely helpful for those who have gpu resources, Error problem above will also decrease dramatically,

  • Mohit Dubey Apr 8, 2024 @ 10:35

    Unable to read input as stream or save file using writestream (pipe) it is giving me an error “Error: Output format mp4 is not available”

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Apr 8, 2024 @ 16:00

      While running ./configure add the following:

      --enable-decoder=aac*,ac3*,opus,vorbis
      --enable-demuxer=mov,m4v,matroska
      --enable-muxer=mp3,mp4
      

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.