Skip to content

Instantly share code, notes, and snippets.

@Ghostbird
Last active November 5, 2024 17:03
Show Gist options
  • Save Ghostbird/0445ebe0d9f6d945c3978702ad455103 to your computer and use it in GitHub Desktop.
Save Ghostbird/0445ebe0d9f6d945c3978702ad455103 to your computer and use it in GitHub Desktop.
Build FFMPEG with NVIDIA hardware accelleration libraries on Debian 12. Includes non-free libnpp!

Obsolete

It is no longer necessary to use this script to have hardware acceleration in ffmpeg on Debian. The default Debian ffmpeg and dependencies now support this out of the box.

See: #gistcomment-5095112

#!/bin/bash
# Automatically compile and install FFMPEG with NVIDIA hardware acceleration on Debian 12
# Includes cuvid, cuda, nvenc, nvdec, and non-free libnpp
# Based on:
# https://www.tal.org/tutorials/ffmpeg_nvidia_encode
# https://developer.nvidia.com/blog/nvidia-ffmpeg-transcoding-guide/
# Abort on error
set -e
suite=$(source /etc/os-release && echo $VERSION_CODENAME)*
# Install libavcodec-extra manually so the build-deps step doesn't pull the problematic libavcodec59
# libjs-bootstrap is a dependency of ffmpeg-doc
# devscripts contains the dch command
sudo apt-get install libavcodec-extra libjs-bootstrap devscripts
sudo apt-mark auto libavcodec-extra libjs-bootstrap devscripts
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-cuvid --enable-nvdec --enable-nvenc --enable-libnpp --enable-nonfree/' debian/rules
DEBEMAIL="root@local" DEBFULLNAME="script" dch --local "+nvidiasupport" "Compiled with support for nvidia hardware acceleration"
DEB_BUILD_OPTIONS="nocheck notest" dpkg-buildpackage -r -nc --jobs=auto --no-sign
cd ..
# Install all built packages, except the non-extra variants of libavfilter, libavcodec and libavformat
sudo dpkg -i $(ls *.deb | grep -Ev "(libavfilter|libavcodec|libavformat)[0-9]+_")
echo "Verification:"
ffmpeg -codecs 2> /dev/null | grep nvenc
# Script ends here, lines below are to restore your system to Debian default binaries
exit 0
# Undo package changes by this script, and restore Debian default binaries:
# Make temporary directory and switch to it.
cd $(mktemp -d)
# First forcibly remove all the custom packages, regardless of what depends on them.
# After this step your system is partially broken.
sudo dpkg --remove --force-all ffmpeg ffmpeg-dbgsym ffmpeg-doc libavcodec59 libavcodec59-dbgsym libavcodec-dev libavcodec-extra libavcodec-extra59 libavcodec-extra59-dbgsym libavdevice59 libavdevice59-dbgsym libavdevice-dev libavfilter8 libavfilter8-dbgsym libavfilter-dev libavfilter-extra libavfilter-extra8 libavfilter-extra8-dbgsym libavformat59 libavformat59-dbgsym libavformat-dev libavformat-extra libavformat-extra59 libavformat-extra59-dbgsym libavutil57 libavutil57-dbgsym libavutil-dev libpostproc56 libpostproc56-dbgsym libpostproc-dev libswresample4 libswresample4-dbgsym libswresample-dev libswscale6 libswscale6-dbgsym libswscale-dev
# Now download the default debian binary versions of the just removed packages.
# This ensures we don't accidentally install the cached self-built packages again.
apt-get download ffmpeg ffmpeg-doc libavdevice59 libavcodec-dev libavcodec-extra libavcodec-extra59 libavdevice-dev libavfilter-dev libavfilter-extra libavfilter-extra8 libavformat-dev libavformat-extra libavformat-extra59 libavutil57 libavutil-dev libpostproc56 libpostproc-dev libswresample4 libswresample-dev libswscale6 libswscale-dev
# Finally install those packages as replacements for the just removed ones. This should fix the system again.
sudo dpkg -i *.deb
@dariogriffo
Copy link

An ironic statement, since you're going out of your way to install a bleeding edge package.

Indeed it is, but as said is the only package I have installed this way :)

But I think you meant you don't need other bleeding edge packages. You might want to reflect that in your blog, so users don't think you need to do that.

You are correct I'll update the blog. Thanks for the heads-up

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