Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This bash script will compile a static Ffmpeg build with NVENC and VAAPI hardware-accelerated support on Ubuntu in your home directory. You can modify the script to customize the build options as you see fit.
#!/bin/bash
#This script will compile and install a static ffmpeg build with support for nvenc un 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
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
cd ~/ffmpeg_sources
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
make
sudo make install
}
#Compile nasm
compileNasm(){
echo "Compiling nasm"
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
}
#Compile libx264
compileLibX264(){
echo "Compiling 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
PATH="$HOME/bin:$PATH" make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) distclean
}
#Compile libfdk-acc
compileLibfdkcc(){
echo "Compiling libfdk-cc"
sudo apt-get install unzip
cd ~/ffmpeg_sources
wget -O fdk-aac.zip https://github.com/mstorsjo/fdk-aac/zipball/master
unzip fdk-aac.zip
cd mstorsjo-fdk-aac*
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) distclean
}
#Compile libmp3lame
compileLibMP3Lame(){
echo "Compiling libmp3lame"
sudo apt-get install nasm
cd ~/ffmpeg_sources
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
./configure --prefix="$HOME/ffmpeg_build" --enable-nasm --disable-shared
make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) distclean
}
#Compile libopus
compileLibOpus(){
echo "Compiling libopus"
cd ~/ffmpeg_sources
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
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) distclean
}
#Compile libvpx
compileLibPvx(){
echo "Compiling libvpx"
cd ~/ffmpeg_sources
git clone https://chromium.googlesource.com/webm/libvpx
cd libvpx
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/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="$HOME/bin:$PATH" make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) clean
}
#Compile ffmpeg
compileFfmpeg(){
echo "Compiling ffmpeg"
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" \
--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/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="$HOME/bin:$PATH" make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) distclean
hash -r
}
#The process
cd ~
mkdir ffmpeg_sources
installLibs
InstallCUDASDK
installSDK
compileNasm
compileLibX264
compileLibfdkcc
compileLibMP3Lame
compileLibOpus
compileLibPvx
compileFfmpeg
echo "Complete!"
@RicardoCapurro

This comment has been minimized.

Copy link

commented Apr 12, 2017

Note there is a bug while compiling vpx, the tar file and directory name is libvpx-1.6.0.tar.bz2 and libvpx-1.6.0 respectively without a v.

#Compile libvpx
...
wget storage.googleapis.com/downloads.webmproject.org/releases/webm/libvpx-1.6.0.tar.bz2
tar xjvf libvpx-v1.6.0.tar.bz2
cd libvpx-v1.6.0
...

@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented May 25, 2017

Okay, thanks for the correction.

@trahibidadido

This comment has been minimized.

Copy link

commented May 31, 2017

For 16.04 change:

sudo apt-get -y --force-yes install

to

sudo apt-get -y --allow install

@aptalca

This comment has been minimized.

Copy link

commented Jun 1, 2017

Just tried it and it seems libx264 requires nasm 2.13 https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu#nasm

and there are typos in the libvpx file and folder names

@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented Jul 18, 2017

I'm planning to deprecate this script, as you should be using a newer version of NVENC SDK and NASM to handle the build.

@sven1011

This comment has been minimized.

Copy link

commented Aug 26, 2017

that would be sad. I just used the script (step by step I admit) and was successful. I made a few changes:

  • use a prior downloaded Video_Codec_SDK_7.0.1 (the max version that is supported by my driver)
  • I used the usual 'sudo apt-get install' (to keep a list of newly installed stuff and be able to ok by hand)
  • added a section for compiling nasm-2.13.01 (looks very much like everything else)
  • fixed up compileLibPvx to compile libvpx-1.6.1.tar.bz2
    all pretty easy. Thus this script is still a big help to all of us who like to use nvenc. It just needs a bit of tlc.
@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented Nov 23, 2017

Noted, @sven1011

@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented Jun 8, 2018

Script updated.

@JezausTevas

This comment has been minimized.

Copy link

commented Jun 27, 2018

Does not work on Ubuntu 14.04, cuda installation failed as of missing (unsupported) packages.

@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented Jun 29, 2018

Hmm, but why (and how) are you still on Ubuntu 14.04LTS?

Perhaps you could try installing CUDA with the binary installer, straight from NVIDIA?

@padams4

This comment has been minimized.

Copy link

commented Jul 3, 2018

I'm planning to deprecate this script, as you should be using a newer version of NVENC SDK and NASM to handle the build

Nooooooo! I struggled using the latest ffmpeg and NVIDIA documentation. This script works out of the box once "--enable-nonfree" is moved before -"-enable-cuda-sdk"

Dell with Xeon X5690, 16.04 LTS, GTX-1080.

@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented Jul 31, 2018

The script should work out of the box (as at the present).

I'll commit resources to keep it up to date.

@pecheuy

This comment has been minimized.

Copy link

commented Aug 31, 2018

thanks for this

@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented Sep 17, 2018

You're welcome, @pecheuy.

Feel free to fork, make improvements, etc.

@kr105

This comment has been minimized.

Copy link

commented Oct 9, 2018

To make it work with Ubuntu 18.04 I had to modify the repository version on the script (plus file name and version of the .deb package too) and additionally install the nvidia-cuda-toolkit and git packages (This was a brand new machine which didn't have it installed). I had to remove the libopus option from the configure script as it also gave me problems. But at the end I got the compiled ffmpeg I was looking for. Thanks!

@ilyaevseev

This comment has been minimized.

Copy link

commented Oct 15, 2018

I adopted your script to Ubuntu-18.04 and CentOS 7.
My script is here: https://github.com/ilyaevseev/ffmpeg-build

@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented Dec 17, 2018

Thank you so much, @ilyaevseev

@nbd712

This comment has been minimized.

Copy link

commented Jan 30, 2019

Not sure if this is due to the older version (14.04 LTS) that I'm using, but I had to add "--pkg-config-flags="--static" into the ffmpeg config options. Great script, thanks!

@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented Feb 14, 2019

Thanks for the note, @nbd712.
I'll test that out and report back.

@eddiecsilva

This comment has been minimized.

Copy link

commented Jul 26, 2019

Is this script compatible with Disco Dingo 19.04? My first run did not complete the installation, but I had already made many system changes. I will install a clean system and share the results.

Update 01:
Did a complete clean install and changed the release of Ubuntu to 18.10, but still not work. :-)

Update 02:
Here's a link to the output of the script: http://pasted.co/7d04d777
Machine: Ryzen 5 2600x, motherboard Asus Prime B450M, 8GB DDR4 3000Mhz, SSD sandisk 240GB, Ubuntu 19.04 full updated

@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented Jul 26, 2019

@lucaszanella

This comment has been minimized.

Copy link

commented Jul 28, 2019

I'm gonna dockerize this soon :)

@Brainiarc7

This comment has been minimized.

Copy link
Owner Author

commented Jul 28, 2019

@deisi

This comment has been minimized.

Copy link

commented Sep 2, 2019

on a quite fresh ubu 19.04 it ended with this:

rm -f y4minput.c.o vpx/src/vpx_image.c.o vpx_mem/vpx_mem.c.o tools/tiny_ssim.c.o y4minput.c.d vpx/src/vpx_image.c.d vpx_mem/vpx_mem.c.d tools/tiny_ssim.c.d y4minput.c.o vpx/src/vpx_image.c.o vpx_mem/vpx_mem.c.o tools/tiny_ssim.c.o
rm -f tools.doxy tools.dox tools/tiny_ssim.dox
rm -f .docs .install-docs 
rm -f .bins .install-bins tools/tiny_ssim
rm -f .libs .install-libs 
rm -f .install-srcs
rm -f   
rm -f doxyfile 
rm -f .docs .install-docs 
rm -f .bins .install-bins 
rm -f .libs .install-libs 
rm -f .install-srcs
rm -f   
rm -f 
rm -f .docs .install-docs 
rm -f .bins .install-bins 
rm -f .libs .install-libs 
rm -f .install-srcs
Compiling ffmpeg
fatal: destination path 'FFmpeg' already exists and is not an empty directory.
ERROR: failed checking for nvcc.

If you think configure made a mistake, make sure you are using the latest
version from Git.  If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
Makefile:163: /tests/Makefile: No such file or directory
make: *** No rule to make target '/tests/Makefile'.  Stop.
Makefile:163: /tests/Makefile: No such file or directory
make: *** No rule to make target '/tests/Makefile'.  Stop.
Makefile:163: /tests/Makefile: No such file or directory
make: *** No rule to make target '/tests/Makefile'.  Stop.
Complete!

@bootrino

This comment has been minimized.

Copy link

commented Sep 11, 2019

On Ubuntu 18.04 I get:

remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 572802 (delta 5), reused 8 (delta 3), pack-reused 572781
Receiving objects: 100% (572802/572802), 224.59 MiB | 32.71 MiB/s, done.
Resolving deltas: 100% (447059/447059), done.
ERROR: failed checking for nvcc.

If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.

@RileyMShea

This comment has been minimized.

Copy link

commented Sep 15, 2019

I was getting opus errors on 19.04. I needed to add the --pkg-config-flags="--static". I also changed the ffmpeg source to the official release which may have done something.

#Compile ffmpeg
compileFfmpeg(){
cd ~/ffmpeg_sources &&
wget -O ffmpeg-snapshot.tar.bz2 https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2 &&
tar xjvf ffmpeg-snapshot.tar.bz2 &&
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/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="$HOME/bin:$PATH" make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) distclean
hash -r
}

@Hulkaveli

This comment has been minimized.

Copy link

commented Oct 8, 2019

You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
 cuda-drivers : Depends: nvidia-418 (>= 418.87.01) but it is not installed
 libcuda1-418 : Depends: nvidia-418 (>= 418.87.01) but it is not installed
 nvidia-418-dev : Depends: nvidia-418 (>= 418.87.01) but it is not installed
 nvidia-opencl-icd-418 : Depends: nvidia-418 (>= 418.87.01) but it is not installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

the --fix-broken install doesnt work & i cant figure out how to just undo everything this script did.

please help. spent 3 days setting ubuntu server up with everything configured and the only thing left was to get handbrake-cli or ffpmeg nvenc encoding going.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.