Skip to content

Instantly share code, notes, and snippets.

@jaybarnes
Last active September 1, 2018 04:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaybarnes/a9c9fbe432b6fd4ce27478fe98bc7221 to your computer and use it in GitHub Desktop.
Save jaybarnes/a9c9fbe432b6fd4ce27478fe98bc7221 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 16.04 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-dbg libglew-dev libglew1.13 \
libglewmx-dev libglewmx-dbg freeglut3 freeglut3-dev freeglut3-dbg 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 nasm:

cd ~/ffmpeg_sources
wget https://www.nasm.us/pub/nasm/releasebuilds/2.13.03/nasm-2.13.03.tar.gz
tar xzvf nasm-2.13.03.tar.gz
cd nasm-2.13.03
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make -j$(nproc)
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
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)
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)
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://github.com/jniltinho/oficinadotux/raw/master/ffmpeg_nvenc/Video_Codec_SDK_7.1.9.zip
unzip Video_Codec_SDK_7.1.9.zip
cp -vr Video_Codec_SDK_7.1.9/Samples/common/inc/GL/* /usr/include/GL/
cp -vr Video_Codec_SDK_7.1.9/Samples/common/inc/*.h /usr/include/
mv Video_Codec_SDK_7.1.9 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-x11grab \
  --enable-xlib \
  --enable-nonfree
PATH="$HOME/bin:$PATH" make -j$(nproc)
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