Skip to content

Instantly share code, notes, and snippets.

@denji
Forked from lucaspar/nvenc-install.sh
Last active August 24, 2025 10:53
Show Gist options
  • Save denji/fc963b79498328a2145944fc5029cddc to your computer and use it in GitHub Desktop.
Save denji/fc963b79498328a2145944fc5029cddc to your computer and use it in GitHub Desktop.
Installation script of CUDA-accelerated `ffmpeg` with NVIDIA Encoder ( docker NVIDIA Encoder https://github.com/markus-perl/ffmpeg-build-script )
#!/bin/bash
# This script will compile and install a static ffmpeg build with support for nvenc in Ubuntu.
# See the prefix path and compile options if edits are needed to suit your needs.
# Install required things from apt
installLibs() {
echo "Installing prerequisites"
sudo apt-get update
sudo apt-get -y --force-yes install autoconf automake build-essential libass-dev libfreetype6-dev libgpac-dev \
libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev
}
# # Install CUDA SDK
# InstallCUDASDK() {
# echo "Installing CUDA and the latest driver repositories from repositories"
# cd "$FFMPEG_SOURCES_DIR" || exit 1
# wget -c -v -nc "https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.2.88-1_amd64.deb"
# sudo dpkg -i cuda-repo-ubuntu1604_9.2.88-1_amd64.deb
# sudo apt-key adv --fetch-keys "http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub"
# sudo apt-get -y update
# sudo apt-get -y install cuda
# sudo add-apt-repository ppa:graphics-drivers/ppa
# sudo apt-get update && sudo apt-get -y upgrade
# }
# Install NVIDIA SDK
installSDK() {
echo "Installing the NVIDIA NVENC SDK."
cd "$FFMPEG_SOURCES_DIR" || exit 1
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers || exit 1
make
sudo make install
}
# Compile nasm
compileNasm() {
echo "Compiling nasm"
cd "$FFMPEG_SOURCES_DIR" || exit 1
wget "http://www.nasm.us/pub/nasm/releasebuilds/2.14rc0/nasm-2.14rc0.tar.gz"
tar xzvf nasm-2.14rc0.tar.gz
cd nasm-2.14rc0 || exit 1
./configure --prefix="$DIR_FFMPEG_BUILD" --bindir="$DIR_USR_BIN"
# use nproc - 2
make -j"${NUM_CORES}"
make -j"${NUM_CORES}" install
make -j"${NUM_CORES}" distclean
}
# Compile libx264
compileLibX264() {
echo "Compiling libx264"
cd "$FFMPEG_SOURCES_DIR" || exit 1
# --- older code with broken URL:
# wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
# tar xjvf last_x264.tar.bz2
# cd x264-snapshot*
# --- newer block:
wget "https://download.videolan.org/pub/x264/snapshots/x264-snapshot-20191217-2245.tar.bz2"
tar xjvf x264-snapshot-20191217-2245.tar.bz2
cd x264-snapshot-20191217-2245 || exit 1
# ---
PATH="$DIR_USR_BIN:$PATH" ./configure --prefix="$DIR_FFMPEG_BUILD" --bindir="$DIR_USR_BIN" --enable-static
PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
make -j"${NUM_CORES}" install
make -j"${NUM_CORES}" distclean
}
# Compile libfdk-acc
compileLibfdkcc() {
echo "Compiling libfdk-cc"
sudo apt-get install unzip
cd "$FFMPEG_SOURCES_DIR" || exit 1
wget -O fdk-aac.zip https://github.com/mstorsjo/fdk-aac/zipball/master
unzip fdk-aac.zip
cd mstorsjo-fdk-aac* || exit 1
autoreconf -fiv
./configure --prefix="$DIR_FFMPEG_BUILD" --disable-shared
make -j"${NUM_CORES}"
make -j"${NUM_CORES}" install
make -j"${NUM_CORES}" distclean
}
# Compile libmp3lame
compileLibMP3Lame() {
echo "Compiling libmp3lame"
sudo apt-get install nasm
cd "$FFMPEG_SOURCES_DIR" || exit 1
wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar xzvf lame-3.99.5.tar.gz
cd lame-3.99.5 || exit 1
./configure --prefix="$DIR_FFMPEG_BUILD" --enable-nasm --disable-shared
make -j"${NUM_CORES}"
make -j"${NUM_CORES}" install
make -j"${NUM_CORES}" distclean
}
# Compile libopus
compileLibOpus() {
echo "Compiling libopus"
cd "$FFMPEG_SOURCES_DIR" || exit 1
wget http://downloads.xiph.org/releases/opus/opus-1.2.1.tar.gz
tar xzvf opus-1.2.1.tar.gz
cd opus-1.2.1 || exit 1
./configure --prefix="$DIR_FFMPEG_BUILD" --disable-shared
make -j"${NUM_CORES}"
make -j"${NUM_CORES}" install
make -j"${NUM_CORES}" distclean
}
# Compile libvpx
compileLibPvx() {
echo "Compiling libvpx"
cd "$FFMPEG_SOURCES_DIR" || exit 1
git clone https://chromium.googlesource.com/webm/libvpx
cd libvpx || exit 1
PATH="$DIR_USR_BIN:$PATH" ./configure --prefix="$DIR_FFMPEG_BUILD" --disable-examples \
--enable-runtime-cpu-detect --enable-vp9 --enable-vp8 \
--enable-postproc --enable-vp9-postproc --enable-multi-res-encoding \
--enable-webm-io --enable-better-hw-compatibility --enable-vp9-highbitdepth \
--enable-onthefly-bitpacking --enable-realtime-only \
--cpu=native --as=nasm
PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
make -j"${NUM_CORES}" install
make -j"${NUM_CORES}" clean
}
#Compile ffmpeg
compileFfmpeg() {
echo "Compiling ffmpeg"
cd "$FFMPEG_SOURCES_DIR" || exit 1
git clone https://github.com/FFmpeg/FFmpeg -b master
cd FFmpeg || exit 1
PATH="$DIR_USR_BIN:$PATH" PKG_CONFIG_PATH="$DIR_FFMPEG_BUILD/lib/pkgconfig" ./configure \
--prefix="$DIR_FFMPEG_BUILD" \
--extra-cflags="-I$DIR_FFMPEG_BUILD/include" \
--extra-ldflags="-L$DIR_FFMPEG_BUILD/lib" \
--bindir="$DIR_USR_BIN" \
--enable-cuda-sdk \
--enable-cuvid \
--enable-libnpp \
--extra-cflags="-I/usr/local/cuda/include/" \
--extra-ldflags=-L/usr/local/cuda/lib64/ \
--enable-gpl \
--enable-libass \
--enable-libfdk-aac \
--enable-vaapi \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libtheora \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-nonfree \
--enable-nvenc
PATH="$DIR_USR_BIN:$PATH" make -j"${NUM_CORES}"
make -j"${NUM_CORES}" install
make -j"${NUM_CORES}" distclean
hash -r
}
# set number of cores to use
num_proc="$(nproc)"
NUM_CORES=$((num_proc - 2))
echo "Using $NUM_CORES cores"
# directories
DIR_INSTALLATION="$HOME/demos/"
DIR_FFMPEG_BUILD="$DIR_INSTALLATION/ffmpeg-build"
DIR_USR_BIN="$HOME/.local/bin"
cd "$DIR_INSTALLATION" || exit 1
FFMPEG_SOURCES_DIR="$DIR_INSTALLATION/ffmpeg-sources"
mkdir -p "$FFMPEG_SOURCES_DIR"
# main things
installLibs
installSDK
compileNasm
compileLibX264
compileLibfdkcc
compileLibMP3Lame
compileLibOpus
compileLibPvx
compileFfmpeg
echo "Complete!"
@whittinghamj
Copy link

this errors out

Using up to 158 cores
Installing prerequisites
Get:1 http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease [1,581 B]
Hit:2 http://us.archive.ubuntu.com/ubuntu jammy InRelease
Get:3 http://us.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu jammy-backports InRelease [109 kB]
Err:1 http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY A4B469963BF863CC
Get:5 http://us.archive.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Hit:6 https://ppa.launchpadcontent.net/graphics-drivers/ppa/ubuntu jammy InRelease
Ign:7 https://ppa.launchpadcontent.net/nilarimogard/webupd8/ubuntu jammy InRelease
Err:8 https://ppa.launchpadcontent.net/nilarimogard/webupd8/ubuntu jammy Release
404 Not Found [IP: 185.125.190.52 443]
Reading package lists... Done
W: GPG error: http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY A4B469963BF863CC
E: The repository 'http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: The repository 'https://ppa.launchpadcontent.net/nilarimogard/webupd8/ubuntu jammy Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

also I had to add the following to auto create your staging folders

Create folders

mkdir -p $DIR_USR_BIN
mkdir -p $DIR_INSTALL_ROOT
mkdir -p $DIR_FFMPEG_BUILD
mkdir -p $DIR_FFMPEG_SOURCES

@DOCIS-007
Copy link

Update

# set number of cores to use
num_proc="$(nproc)"
NUM_CORES=$((num_proc <= 2 ? num_proc : num_proc - 1))
echo "Using up to $NUM_CORES cores"

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