Skip to content

Instantly share code, notes, and snippets.

@raydouglass
Last active February 21, 2024 00:06
Show Gist options
  • Save raydouglass/31966ba6a6474370d6514879dfc510ab to your computer and use it in GitHub Desktop.
Save raydouglass/31966ba6a6474370d6514879dfc510ab to your computer and use it in GitHub Desktop.
Compile ffmpeg 6.0 with NVIDIA hardware acceleration
#!/usr/bin/env bash
set -euxo pipefail
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
if ! command nvcc --version >/dev/null 2>&1; then
echo "Please install CUDA Toolkit or adjust PATH to include CUDA binaries"
echo "See https://developer.nvidia.com/cuda-downloads"
exit 2
fi
nv_codec_headers_dir="$(mktemp -d)"
ffmpeg_dir="$(mktemp -d)"
trap 'rm -rf "$nv_codec_headers_dir" "$ffmpeg_dir"' EXIT
# Install dependencies
apt update && apt install -y \
autoconf \
automake \
build-essential \
cmake \
dialog \
git \
libass-dev \
libfdk-aac-dev \
libfreetype6-dev \
libgnutls28-dev \
liblzma-dev \
libmp3lame-dev \
libnuma-dev \
libopenjp2-7-dev \
libopus-dev \
libsnappy-dev \
libsrt-gnutls-dev \
libtool \
libva-dev \
libvorbis-dev \
libvpx-dev \
libwebp-dev \
libx264-dev \
libx265-dev \
libxvidcore-dev \
libzimg-dev \
libzmq3-dev \
meson \
ninja-build \
pkg-config \
texinfo \
wget \
yasm \
zlib1g-dev
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git "$nv_codec_headers_dir"
pushd "$nv_codec_headers_dir"
make install
popd
git clone -b release/6.0 --depth 1 https://git.ffmpeg.org/ffmpeg.git "$ffmpeg_dir"
pushd "$ffmpeg_dir"
# Configure ffmpeg w/ nvidia support & common codecs
./configure \
--disable-static \
--enable-cuda-nvcc \
--enable-gnutls \
--enable-gpl \
--enable-libass \
--enable-libfdk-aac \
--enable-libfdk-aac \
--enable-libfontconfig \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libnpp \
--enable-libopenjpeg \
--enable-libopus \
--enable-libsnappy \
--enable-libsrt \
--enable-libvorbis \
--enable-libvpx \
--enable-libwebp \
--enable-libx264 \
--enable-libx265 \
--enable-libxml2 \
--enable-libxvid \
--enable-libzimg \
--enable-libzmq \
--enable-lzma \
--enable-nonfree \
--enable-pthreads \
--enable-shared \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64
make -j"$(nproc)"
make install
popd
# Ensure the installed libraries are on LD_LIBRARY_PATH
# This may not be necessary if /usr/local/lib already configured
# You can test by running ffmpeg and if you see an error like ffmpeg: error while loading shared libraries: libavdevice.so.60: cannot open shared object file: No such file or directory
grep -q "^/usr/local/lib$" /etc/ld.so.conf.d/* || (echo "/usr/local/lib" | tee /etc/ld.so.conf.d/usr-local-lib.conf)
ldconfig
# Check if the nvidia encoders are available
echo "Checking for nvidia encoders..."
if ! command ffmpeg -encoders 2>&1 | grep -i nvidia >/dev/null; then
echo "Nvidia encoders not found in ffmpeg"
exit 4
fi
echo "ffmpeg with nvidia support installed successfully!"
echo "Example usage:"
echo " /usr/local/bin/ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:v h264_nvenc -preset fast -c:a copy -c:s copy -map 0 output.mp4"
echo "See also: https://github.com/raydouglass/media_management_scripts#hardware-acceleration"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment