ADVERTISEMENT

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 Debian/Ubuntu Linux
Est. reading time 3 minutes
ADVERTISEMENT

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-460
  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/
    ./configure --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64
  9. Compile it, execute:make -j $(nproc)
  10. Verify executable: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. You can now use -hwaccel cuda switch for encoding. For instance: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. It is a ncurses-based GPU status viewer for NVIDIA GPUs:
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
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:


🐧 Get the latest tutorials on Linux, Open Source & DevOps via

RSS feed ➔    Weekly email newsletter ➔
ADVERTISEMENT


CategoryList of Unix and Linux commands
Documentationhelp mandb man pinfo
Disk space analyzersdf duf ncdu pydf
File Managementcat cp less mkdir more tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Linux Desktop AppsSkype Spotify VLC 3
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop iotop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
Shell builtinscompgen echo printf
Text processingcut rev
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

Comments on this entry are closed.

  • Michael Sep 28, 2021 @ 5:51

    A good overview but doesn’t result the installation of the cuda toolkit via apt in very old version? At least on an older Ubuntu, like the wildly used 18.04?

    • 🐧 Vivek Gite Sep 28, 2021 @ 6:43

      For server usage it should be find. But, yes, one can always upgrade to Ubuntu 20.04 LTS.

  • Serge Sep 28, 2021 @ 16:48

    got working `ffmpeg` with the nvidia acceleration without compiling, but with the propietary driver. did i something wrong?

Comments are closed. Still have questions? Post it on our forum