Skip to content

Instantly share code, notes, and snippets.

@Ghostbird
Last active December 22, 2023 02:05
Show Gist options
  • Save Ghostbird/11b62ae6370a4f42babc7c8164f44d9d to your computer and use it in GitHub Desktop.
Save Ghostbird/11b62ae6370a4f42babc7c8164f44d9d to your computer and use it in GitHub Desktop.
Automatically compile and install FFMPEG with NVIDIA hardware acceleration on Debian 10+
#!/bin/bash
# Automatically compile and install FFMPEG with NVIDIA hardware acceleration on Debian
# Based on https://www.tal.org/tutorials/ffmpeg_nvidia_encode
# Verified working on Debian 10 and 11
# Abort on error
set -e
suite=stable
sudo apt-get build-dep ffmpeg -t $suite
sudo apt-get install nvidia-cuda-toolkit -t $suite
mkdir -p ffmpeg-deb/src
cd ffmpeg-deb
if [[ -d nv-codec-headers ]]
then
cd nv-codec-headers
git fetch --tags
else
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
fi
# Checkout latest release, intead of HEAD. The Debian driver in stable may not yet support the pre-release API.
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
make
sudo make install
cd ../src
rm -rf ./*
apt-get source ffmpeg -t $suite
cd ffmpeg-*
sed -i 's/--enable-sdl2/--enable-sdl2 --enable-cuda --enable-nvenc/' debian/rules
DEB_BUILD_OPTIONS="nocheck nodoc notest" dpkg-buildpackage -r -nc -j4 --no-sign
cd ..
# Install all built packages, except the non-extra variants of libavfilter and libavcodec
sudo dpkg -i $(ls *.deb | grep -Ev "(libavfilter|libavcodec)[0-9]+_")
echo "Verification:"
ffmpeg -codecs 2> /dev/null | grep nvenc
@Ghostbird
Copy link
Author

Ghostbird commented May 20, 2022

I updated this with a suite setting. I tried it recently and I got some errors. It turned out that I had added the testing suite which in certain cases would be preferred. This way it's locked to stable, but you can change it in one place if desired.

Also updated:

  • Build without pre-clean, the script standard creates a new clean directory, but this makes it easier to quickly rerun the build line if a build fails.
  • Add options nodoc since I got errors in the doc build.

@eliaspizarro
Copy link

eliaspizarro commented Nov 27, 2022

i got dependency problems with deb11 and this script, after install, run apt update:

develop@zoneminder:~/ffmpeg-deb/src$ sudo apt upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
 libavcodec-extra58 : Conflicts: libavcodec58
 libavfilter-extra7 : Conflicts: libavfilter7

@Ghostbird
Copy link
Author

Ghostbird commented Nov 30, 2022

I don't have this issue on my Debian 11 machine, I rebuilt it to be sure. However, I'll check whether I changed the script to avoid it, and forgot to update the gist. Otherwise it might be because you had a different set of packages pre-installed than I had.

@Ghostbird
Copy link
Author

Ghostbird commented Dec 2, 2022

I've checked the script. The only difference was that I had locally added the notest option. I've added it here too.

Note, this buils and installs all ffmpeg dependencies however these include two pairs of mutually exclusive ones, libavcodec58 vs libavcodec58-extra and libavfilter7 vs libavfilter-extra7.

I've updated the install line in the script to exclude the non-extra variants. Maybe that'll fix your issue. In my case dpkg automatically made the right choice to not install the normal variants in favour of the -extra variants.

The changed line is 32:

sudo dpkg -i $(ls *.deb | grep -Ev "(libavfilter|libavcodec)[0-9]+_")

Runs a subshell that lists all files ending in .deb and feeds that list to grep using extended regex mode (-E) and inverse output (-v outputs only non-matching lines). Finally dpkg -i installs the resulting list of deb files from the subshell output.

@Ghostbird
Copy link
Author

Ghostbird commented Dec 3, 2022

Found an interesting issue just now. nv-codec-headers HEAD is already at API 12 but the Debian stable driver only supports up to API 11. That makes sense, API 12 is not officially released yet AFAIK.
I've changed the script to checkout and build the latest tag from nv-codec-headers , instead of HEAD. This solves the issue where you try to use an NVENC codec in ffmpeg and receive:

[h264_nvenc @ 0x55639ee42180] Driver does not support the required nvenc API version. Required: 12.0 Found: 11.1
[h264_nvenc @ 0x55639ee42180] The minimum required Nvidia driver for nvenc is (unknown) or newer

@Ghostbird
Copy link
Author

This script is now superseded by build-ffmpeg-nvidia.sh.

It compiles FFMPEG with cuvid, cuvid, nvdec, nvenc, and non-free libnpp.

I've decided to make that a separate script, instead of an upgrade to this one, because it includes the --enable-nonfree flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment