Skip to content

Instantly share code, notes, and snippets.

@gfodor
Last active January 20, 2023 14:55
Show Gist options
  • Save gfodor/16b216b354c656ea1078bbc54c364ca6 to your computer and use it in GitHub Desktop.
Save gfodor/16b216b354c656ea1078bbc54c364ca6 to your computer and use it in GitHub Desktop.
Static FFmpeg build on Ubuntu 16.04 with Nvidia NVENC enabled.

Minimalist static FFmpeg build on Ubuntu 18.10 with Nvidia NVENC enabled.

Original guide with a standard build is here.

With this guide, I'm adding more instructions to enable support for NVIDIA CUVID and NVIDIA NPP for enhanced encode and decode performance.

First, prepare for the build and create the work space directory:

cd ~/
mkdir ~/ffmpeg_sources
sudo apt-get -y update
sudo apt-get upgrade -y
sudo apt-get -y install autoconf automake build-essential libass-dev libtool pkg-config texinfo zlib1g-dev

Install dependencies for NVENC:

sudo apt-get -y install glew-utils libglew-dev libglew2.0 freeglut3 freeglut3-dev libghc-glut-dev libghc-glut-doc libghc-glut-prof libalut-dev libxmu-dev libxmu-headers libxmu6 libxmu6-dbg libxmuu-dev libxmuu1 libxmuu1-dbg git-core

Build and deploy Yasm: Yasm is an assembler for x86 optimizations used by x264 and FFmpeg. Highly recommended or your resulting build may be very slow.

cd ~/ffmpeg_sources
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make -j$(nproc)
sudo make -j$(nproc) install
make -j$(nproc) distclean

Build and deploy libx264 statically: This library provides a H.264 video encoder. See the H.264 Encoding Guide for more information and usage examples. This requires ffmpeg to be configured with --enable-gpl --enable-libx264.

cd ~/ffmpeg_sources
wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar xjvf last_x264.tar.bz2
cd x264-snapshot*
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static --disable-opencl
PATH="$HOME/bin:$PATH" make -j88
sudo make -j$(nproc) install
make -j$(nproc) distclean

Build and configure libx265: This library provides a H.265/HEVC video encoder. See the H.265 Encoding Guide for more information and usage examples.

sudo apt-get install cmake mercurial -y
cd ~/ffmpeg_sources
hg clone https://bitbucket.org/multicoreware/x265
cd ~/ffmpeg_sources/x265/build/linux
PATH="$HOME/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make -j$(nproc)
sudo make -j$(nproc) install
make -j$(nproc) clean

Build and deploy the libfdk-aac library: This provides an AAC audio encoder. See the AAC Audio Encoding Guide for more information and usage examples. This requires ffmpeg to be configured with --enable-libfdk-aac (and --enable-nonfree if you also included --enable-gpl).

cd ~/ffmpeg_sources
wget -O fdk-aac.tar.gz https://github.com/mstorsjo/fdk-aac/tarball/master
tar xzvf fdk-aac.tar.gz
cd mstorsjo-fdk-aac*
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make -j$(nproc)
sudo make -j$(nproc) install
make -j$(nproc) distclean

Deploy NVENC SDK:

First, install Nvidia's drivers:

Activate the proper repo:

sudo add-apt-repository ppa:graphics-drivers/ppa -y
sudo apt-get update

Then install nvidia-367:

apt-get install nvidia-378 nvidia-378-dev -y
apt-get install nvidia-cuda-toolkit --install-recommends -y

At this stage, please reboot the node.

sudo systemctl reboot

Then proceed to download the Nvidia NVENC 7.1 SDK from the Nvidia Developer portal when the host is booted up:

We are using the NVENC 7.1 SDK from here. Sign up with the developer program to access the download page below.

Ensure that the SDK is downloaded to your ~/ffmpeg_sources directory (cd ~/ffmpeg_sources to be sure) so as to maintain the needed directory structure.

Extract and copy the NVENC SDK headers as needed:

Then navigate to the extracted directory:

cd ~/ffmpeg_sources
wget -c https://s3-us-west-1.amazonaws.com/backups.reticulum-dev-7f8d39c45878ee2e/streaming-deps/Video_Codec_SDK_8.2.16.zip
unzip Video_Codec_SDK_8.2.16.zip
sudo cp -vr Video_Codec_SDK_8.2.16/Samples/External/* /usr/include/
mv Video_Codec_SDK_8.2.16 nv_sdk

Building a static ffmpeg binary with the required options:

cd ~/ffmpeg_sources
git clone https://github.com/FFmpeg/FFmpeg -b master
cd FFmpeg
PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
  --prefix="$HOME/ffmpeg_build" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I$HOME/ffmpeg_build/include" \
  --extra-ldflags="-L$HOME/ffmpeg_build/lib" \
  --bindir="$HOME/bin" \
  --enable-cuda \
  --enable-cuvid \
  --enable-libnpp \
  --extra-cflags=-I../nv_sdk \
  --extra-ldflags=-L../nv_sdk \
  --enable-gpl \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nvenc \
  --enable-libxcb \
  --enable-xlib \
  --enable-nonfree
PATH="$HOME/bin:$PATH" make -j$(nproc)
sudo make -j$(nproc) install
make -j$(nproc) distclean
hash -r

If ~/bin is already in your path, you can call up ffmpeg directly.

Hint: Use this guide to learn how to launch ffmpeg in multiple instances for faster NVENC based encoding on capable hardware.

Recorder Screen CLI FFMPEG

Capture without audio

ffmpeg -f x11grab -framerate 60 -s 1920x1080 -i :0.0 -c:v h264_nvenc -b:v 16000k \
-pix_fmt nv12 -rc cbr -profile:v high -preset lossless my-screen-only.mp4

Capturing with audio

ffmpeg -f alsa -i pulse -f x11grab -framerate 60 -s 1920x1080 -i :0.0 -c:a aac -b:a 320k -c:v h264_nvenc \
-b:v 13000k -pix_fmt nv12 -qmin 15 -qmax 40 -profile:v high -preset llhq \
-cq 10 -bf 2 -g 150 my-audio-screen.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment