Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This gist will show you how to build a minimalist, statically-linked ffmpeg binary under the ~/bin subdirectory on your home on Ubuntu 16.04LTS. Comes with NPP, CUDA and NVENC capabilities.

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 && apt-get dist-upgrade -y
sudo apt-get -y install autoconf automake build-essential libass-dev \
  libtool \
  pkg-config texinfo zlib1g-dev

Install CUDA 8 SDK from Nvidia's repository:

Note that this phase will prompt you to install the device driver. Skip it, and skip the samples too.We will install the driver later. Fetch the installers first:

mkdir ~/cuda && cd ~/cuda
 wget -c -v -nc https://developer.nvidia.com/compute/cuda/8.0/Prod2/local_installers/cuda_8.0.61_375.26_linux-run

wget -c -v https://developer.nvidia.com/compute/cuda/8.0/Prod2/patches/2/cuda_8.0.61.2_linux-run

chmod +x cuda_*

Now, install the SDK, and skip the device driver setup:

./cuda_8.0.61_375.26_linux-run

When you're done, proceed to deploy the patch, accept the license and proceed:

./cuda_8.0.61.2_linux-run

Now, set up the environment variables for CUDA:

Edit the /etc/environment file and append the following:

CUDA_HOME=/usr/local/cuda-8.0

Now, append the PATH variable with the following:

/usr/local/cuda/bin

When done, remember to source the file:

source /etc/environment

Now, create a custom conf file for the CUDA library path under /etc/ld.so.conf.d/cuda.conf, with the following entries:

nano /etc/ld.so.conf.d/cuda.conf

Content:

/usr/local/cuda-8.0/lib64

And also run:

ldconfig -vvvv

(This phase assumes that you're logged in as root).

Now, deploy the device driver:

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-384

(This phase assumes that you're logged in as root).

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 

Build and deploy nasm: Nasm is an assembler for x86 optimizations used by x264 and FFmpeg. Highly recommended or your resulting build may be very slow. Note that we're using the latest release candidate, and not the stable version as of the time of writing.

cd ~/ffmpeg_sources
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
./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
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: At this phase, we will assume that the end user has rebooted the node prior to proceeding with this phase. If not, proceed via:

sudo systemctl reboot

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

We are using the latest NVENC SDK for this.

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:

unzip Video_Codec_SDK_*.zip
cd Video_Codec_SDK_*/Samples

From within the SDK directory, do:

sudo cp -vr Samples/common/inc/GL/* /usr/include/GL/
sudo cp -vr Samples/common/inc/*.h /usr/include/

When done,do:

cd ~/ffmpeg_sources
mv Video_Codec_SDK_* nv_sdk

That will allow us to statically link to the SDK with ease, below.

Note that there may be a newer version of the SDK available at the time, please adjust as appropriate.

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-sdk \
  --enable-cuvid \
  --enable-libnpp \
  --extra-cflags=-I../nv_sdk \
  --extra-ldflags=-L../nv_sdk \
  --extra-cflags="-I/usr/local/cuda/include/" \
  --extra-ldflags=-L/usr/local/cuda/lib64/ \
  --enable-gpl \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nvenc \
  --enable-nonfree
PATH="$HOME/bin:$PATH" make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) distclean
hash -r

You may also want to tune your build further by calling uon NVCC to generate a build optimized for your GPU's CUDA architecture only.

The example below shows the build options to pass for Pascal's GM10x-series GPUs, with an SM version of 6.1:

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-sdk \
  --enable-cuvid \
  --enable-libnpp \
  --extra-cflags=-I../nv_sdk \
  --extra-ldflags=-L../nv_sdk \
  --extra-cflags="-I/usr/local/cuda/include/" \
  --extra-ldflags=-L/usr/local/cuda/lib64/ \
  --nvccflags="-gencode arch=compute_61,code=sm_61 -O2" \
  --enable-gpl \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libx264 \
  --extra-libs=-lpthread \
  --enable-libx265 \
  --enable-nvenc \
  --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. Note that the build instructions assume that the NVIDIA CUDA toolkit is on the system path, as is recommended during setup.

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

llogan commented Dec 10, 2016 edited

16.04 provides yasm 1.3.0 so you can just install it from the repo instead of compiling. The Ubuntu compile guide on the FFmpeg Wiki that you adapted (an attribution link would be appreciated as it is covered by CC BY-SA 3.0) has users compile it because that guide is intended to be usable with all supported Ubuntu versions. Once you use the repo yasm you can, IIRC, avoid using the PATH="$HOME/bin:$PATH" instances.

Secondly, are you sure you need to download the NVENC SDK? I could be mistaken, but I think it and CUDA stuff are included in the FFmpeg source in the compat directory.

Owner

Brainiarc7 commented Dec 18, 2016

Yes, the NVENC SDK is needed, in full.

unrelatedlabs commented Dec 28, 2016 edited

Had to include lib path to cuda, otherwise libnpp was not found.

Adding --extra-cflags="-I/usr/local/cuda/include/" --extra-ldflags=-L/usr/local/cuda/lib64/ to configure

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-nonfree --extra-cflags="-I/usr/local/cuda/include/" --extra-ldflags=-L/usr/local/cuda/lib64/

freezy commented Mar 23, 2017

I had to add --enable-shared to the x264 configuration, otherwise I had linker errors.

Owner

Brainiarc7 commented Mar 28, 2017

Hello @unrelatedlabs,

Thanks for the correction above.

I also ran into the same problem with the current NVENC SDK version (v7.1.9), your fix corrects the problem.

Updating the documentation now.

vidiben commented Apr 7, 2017

Hi, on lubuntu 16.04, I am running into the following issues

  1. NPP: the configure command keeps complaining, saying it cannot find libnpp.

I don't have any cuda folder in /usr/local (using a fresh lubuntu 16.04 setup), but it looks to be properly installed.

# find /usr/lib/x86_64-linux-gnu/ -name "*npp*"
/usr/lib/x86_64-linux-gnu/libnpps.so
/usr/lib/x86_64-linux-gnu/libnppi.so.7.5.18
/usr/lib/x86_64-linux-gnu/libnpps_static.a
/usr/lib/x86_64-linux-gnu/libnppc_static.a
/usr/lib/x86_64-linux-gnu/libnppc.so.7.5
/usr/lib/x86_64-linux-gnu/libnppi.so
/usr/lib/x86_64-linux-gnu/libnpps.so.7.5.18
/usr/lib/x86_64-linux-gnu/libnppi_static.a
/usr/lib/x86_64-linux-gnu/libnppi.so.7.5
/usr/lib/x86_64-linux-gnu/libnpps.so.7.5
/usr/lib/x86_64-linux-gnu/stubs/libnpps.so
/usr/lib/x86_64-linux-gnu/stubs/libnppi.so
/usr/lib/x86_64-linux-gnu/stubs/libnppc.so
/usr/lib/x86_64-linux-gnu/libnppc.so
/usr/lib/x86_64-linux-gnu/libnppc.so.7.5.18

@unrelatedlabs, @Brainiarc7: Where and how can I get a local build of cuda to link to?

  1. When I disable libnpp, I can compile but each time I try to use nvenc, I am getting a Segmentation fault. Same result if cuda and cuvid are disabled :\
ffmpeg -i tmp.0.h264 -c:v hevc_nvenc test.mkv                                            Moviez/Ghost in the Shell (1995).tmp.mkv grospc
ffmpeg version N-85328-g8d94d97 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
  configuration: --prefix=/tmp/ffmpeg/build --pkg-config-flags=--static --extra-cflags='-I/tmp/ffmpeg/build/include -static' --extra-ldflags='-L/tmp/ffmpeg/build/lib -static' --bindir=/usr/local/bin --extra-cflags=-I/tmp/ffmpeg/sources/nv_sdk --extra-ldflags=-L/tmp/ffmpeg/sources/nv_sdk --enable-gpl --extra-ldflags=-L/usr/lib/x86_64-linux-gnu/ --enable-libass --enable-libfdk-aac --enable-vaapi --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --enable-nvenc --enable-static --disable-ffplay --disable-ffserver --disable-shared --enable-debug --enable-cuda --enable-cuvid
  libavutil      55. 60.100 / 55. 60.100
  libavcodec     57. 92.100 / 57. 92.100
  libavformat    57. 72.100 / 57. 72.100
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 84.100 /  6. 84.100
  libswscale      4.  7.100 /  4.  7.100
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
Input #0, h264, from 'tmp.0.h264':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: h264 (High), yuv420p(progressive), 1920x1040, 23.98 fps, 23.98 tbr, 1200k tbn, 47.95 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> hevc (hevc_nvenc))
Press [q] to stop, [?] for help
[1]    12775 segmentation fault (core dumped)  ffmpeg -i tmp.0.h264 -c:v hevc_nvenc test.mkv
Owner

Brainiarc7 commented Apr 16, 2017

Hello @vidiben,

It would be best to install CUDA SDK 8 from Nvidia's repository.

I'll update this gist with these instructions soon.

Owner

Brainiarc7 commented Apr 19, 2017

Hello @vidiben,

It's done.

Owner

Brainiarc7 commented May 8, 2017

Hello @vibiden,

How did you install CUDA when you got the segmentation faults?

croepha commented Jun 21, 2017

i had to install nasm from source, libx264 kept complaining about my yasm source

Owner

Brainiarc7 commented Jun 25, 2017

Hello @croepha,

Your observation is correct.

x264 and others are now favoring nasm over yasm, will correct the gist with this information. Thanks for the feedback!

Owner

Brainiarc7 commented Jun 26, 2017

And now updated with new build options and flags that need to be passed so as to generate optimized kernels for the specific GPU architecture.

libero08 commented Jul 3, 2017

Hi B7,
as you knew i'm a follower of youre tips and tutorials.
Unfortunately, the new "minimalistic buld for nvenc" cause me lot of problem during the instalation process.
first step:
i install a fresh & clean ubuntu 16.04
i install all step by step and all goes fine untill here:
"sudo systemctl reboot"
After reboot
a lot of probblems begin!
i loose environment for the sistem, even inside the file /etc/environement this line was unmodified:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
i was forced to modiffied on this way to make system work again:
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
and inside of environmet i have also included this 2 lines like you say before:
export CUDA_HOME=/usr/local/cuda-8.0
export PATH=/usr/local/cuda/bin:$PATH
The problems do not stop here.
The system stuckt here durinfg the compilation of ffmpeg -here : make -j$(nproc), because it was unable to link to the "nvcc" on /usr/local/cuda/bin" or /usr/local/cuda-8.0/bin"!
To make proper system work and i install also " libproc-processtable-perl"
and for revealing nvcc ( i was unable to reveal using which and nvcc -V return nothing) i was forced to install: apt install nvidia-cuda-toolkit
after that i was able to compile ffmpeg.
Sincerly don't knew yet if cuda works, but calling during encoder processo of libx264 ffmpeg do his job.
I enable also on my binary libfreetype.
So, please moddify when you can with corect arguments the instalation steps.

Owner

Brainiarc7 commented Jul 5, 2017

Hello @libero08 ,

I'm sorry to hear that you're having problems with these instructions.

However, note that before I publish any of these gists, great effort is made to ensure that:

(a). The testbed is always running the latest, greatest version of Ubuntu LTS, confirmed by running apt-get upgrade and apt-get dist-upgrade.
(b). Unless otherwise specified, the NVIDIA CUDA SDK's installation process detailed above also installs the appropriate Nvidia driver, and the CUDA toolkit's binaries, which, by default, are not in the system path. This is why we modify these environment variables to set both CUDA_HOME and prepend the PATH variable with the location for the CUDA-8 SDK's bin subdirectory, as demonstrated above.

From the symptoms observed, it can be concluded that you did not use an up to date Ubuntu installation.
Even after installing a new installation from an Ubuntu installation image (boot media), you must run:

sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get dist-upgrade -y
To ensure that you're running the latest kernel (see the last dist-upgrade part).

You may want to retry the build with a clean slate. A verification of these build steps produces a working ffmpeg binary.

libero08 commented Jul 5, 2017 edited

Shure i do!
Beacause i read careffuly all instruction from cuda (PDF) and i found a lot of thing verry interesting.
I supose it' was my fault 100%.
BTW i found some new instruction of cuda incompatible with my device.
Can you permit me to send you an e-mail from gist or git ?

Owner

Brainiarc7 commented Jul 5, 2017

Sure, navigate to my profile and send me an email to that address.

Thank you for this good information.
I am attempting to use rdp's cross-compile script in ububtu 17.04 with gcc7.1.0 to produce a static windows ffmpeg executable.
I'm still hazy on what to download from nvidia and cross-compile and what switches to use, to enable these.
scale_cuda
scale_npp
For example this "--extra-ldflags=-L/usr/local/cuda/lib64/" seems to imply that libnpp has to be cross-compiled from source too.
I see the latest ffmpeg git has a "--enable-cuda-sdk" build switch, but am not sure what that implies in regard to downloading things and cross-compiling.
I'm unclear though what that means in order to build filter scale_cuda and what relationship scale_cuda has to scale_npp (the scale_cuda source is in the ffmpeg git).
Any advice would be greatly appreciated.

Owner

Brainiarc7 commented Aug 12, 2017

Here is what you will need:

  1. The whole CUDA 8 SDK. Some people use the repository method to install it, but I personally recommend using the runfile installer as it allows you to skip the device driver. However, this method requires a few manual steps, such as setting up a custom ld.so.conf.d/*.conf entry for the NVIDIA CUDA SDK libraries prior to usage.

  2. The scale_cuda filter is somewhat analogous to the options offered by the scale_npp filter, with a few differences: The first one is automatically built when the --enable-cuda-sdk option is used, whereas the latter can only be enabled and built after the --enable-libnpp option is passed, combined with the --enable-cuda-sdk option as a dependency.

You can learn more about Nvidia's Performance Primitives libraries here and from the documentation.

Follow this gist for what's available with NPP and NVENC for FFmpeg, when these options are enabled.

Thank you.

The whole CUDA 8 SDK. Some people use the repository method to install it, but I personally recommend using the runfile installer as it allows you to skip the device driver. However, this method requires a few manual steps, such as setting up a custom ld.so.conf.d/*.conf entry for the NVIDIA CUDA SDK libraries prior to usage.

I'm not sure how to do a "few manual steps, such as setting up a custom ld.so.conf.d/*.conf entry for the NVIDIA CUDA SDK libraries prior to usage", for the static build cross-compilation.

Owner

Brainiarc7 commented Aug 14, 2017

I'll update the gist with instructions for the same.

Owner

Brainiarc7 commented Aug 16, 2017

Done.

Hello @Brainiarc7

First of all thanks for your guide,
I'd like to know if this gist is still up to date?

Regards

Unless I'm missing something, there's no such ffmpeg build flag as "--enable-cuda-sdk", there's just "--enable-cuda". Maybe they dropped it/changed it recently?

Owner

Brainiarc7 commented Oct 5, 2017

Yes, @PMLavigne,

The build flag was dropped in place of the current one.

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