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 |
Installing FFmpeg with NVIDIA GPU hardware acceleration on Linux
- First, make sure Nvidia Driver (Latest Proprietary Driver) installed on Ubuntu or Debian. For example:
sudo apt install nvidia-driver-460
- Reboot the Linux system:
sudo reboot
- 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
- 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 - Install ffnvcodec on Ubuntu or Debian:
cd nv-codec-headers && sudo make install
- Get ffmpeg source code, run:
cd ~/nvidia/
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/ - Install GNU gcc compiler collection and libs, run:
sudo apt install build-essential yasm cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-dev
- 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 - Compile it, execute:
make -j $(nproc)
- Verify executable:
ls -l ffmpeg
./ffmpeg
- 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
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:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
- If your domain is not sending email, set these DNS settings to avoid spoofing and phishing
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:
- A supported GPU hardware. Hence, not all NVIDIA GPU will work.
- Working installation of NVIDIA driver
- The CUDA toolkit
- Also, the NVIDIA Codec SDK
- And finally, ffmpeg configured and complied with --enable-cuda-nvcc option.
RSS feed ➔ Weekly email newsletter ➔
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • iotop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
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?
For server usage it should be find. But, yes, one can always upgrade to Ubuntu 20.04 LTS.
got working `ffmpeg` with the nvidia acceleration without compiling, but with the propietary driver. did i something wrong?