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!"
@ransagy
Copy link

ransagy commented Feb 1, 2020

As the note says, you have to edit the configure script in the ffmpeg sources dir. I tried it on a clean Ubuntu VM in Azure and had no issues.
I imagine the snap version wouldn't have nvidia support, though.

@theonlycure
Copy link

theonlycure commented Feb 8, 2020

@ransagy
Copy link

ransagy commented Feb 8, 2020

Huh, That's good to know. Weird, considering that non-free shouldn't be legally allowed redistribution but..
Thanks :)

@ajgentile
Copy link

ajgentile commented Mar 9, 2020

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

Awesome.

@Brainiarc7
Copy link
Author

Brainiarc7 commented Mar 9, 2020

@mercster
Copy link

mercster commented Mar 27, 2020

Hey, thanks for this... if not already brought up, the script calls 'git', which the script does not install with apt-get. Might wanna add that.

Also getting "ERROR: failed checking for nvcc." and script fails, but I'll see if I can't figure it out...

@theonlycure
Copy link

theonlycure commented Apr 16, 2020

@sotiris-bos
Copy link

sotiris-bos commented May 15, 2020

You have to change the videolan x264 source to use git. The wget link just gives a 404 error, resulting in ffmpeg being compiled without x264.

@DwarfFighterCleric
Copy link

DwarfFighterCleric commented May 29, 2020

I'm still being restricted to use a FFmpeg binary that uses outdated Libavcodec and Libavformat from back in Feb-2017 because these are what the Xenial 16.04 repositories have to install. This means that I'm potentially relying on outdated aac codec (less audio quality) and outdated x264 (possibly less performance or compression rate when encoding video)
It seems like this difficulty to build from source happens across the board.
Do you guys know a place where updated Linux builds are made available? I could only find one Windows EXE build and it was from 2018, it came with HE-AAC included with it. (and even harder: any place to find Xenial 16.04 builds?)
It seems like some people from the Linux community really have a source of satisfaction from gatekeeping these things so other people can't benefit from it.
Even an Appimage release once a year would provide solution for this.
Is there a way to manually update the binary and the libs? Will that work if I simply copy the files manually? How can I manually update the AAC and the x264 codecs?

@mercster
Copy link

mercster commented May 29, 2020

Debian is usually quite out of date... I can only suggest you install a more up-to-date distro (Ubuntu? Fedora?) ; I don't know the answer for Debian.

@Anan5a
Copy link

Anan5a commented Oct 27, 2020

Debian is usually quite out of date... I can only suggest you install a more up-to-date distro (Ubuntu? Fedora?) ; I don't know the answer for Debian.

@mercster What do mean Debian is outdated? It us very uptodate

@katsar0v
Copy link

katsar0v commented Nov 24, 2020

I don't think the script works atm:

Cloning into 'FFmpeg'...
remote: Enumerating objects: 51, done.
remote: Counting objects: 100% (51/51), done.
remote: Compressing objects: 100% (42/42), done.
remote: Total 610331 (delta 16), reused 20 (delta 9), pack-reused 610280
Receiving objects: 100% (610331/610331), 245.36 MiB | 26.88 MiB/s, done.
Resolving deltas: 100% (478297/478297), 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.
Makefile:177: /tests/Makefile: No such file or directory
make: *** No rule to make target '/tests/Makefile'.  Stop.
Makefile:177: /tests/Makefile: No such file or directory
make: *** No rule to make target '/tests/Makefile'.  Stop.
Makefile:177: /tests/Makefile: No such file or directory
make: *** No rule to make target '/tests/Makefile'.  Stop.
Complete!

@Brainiarc7
Copy link
Author

Brainiarc7 commented Nov 24, 2020

@katsar0v
Copy link

katsar0v commented Nov 24, 2020

@Brainiarc7 I thought the script does this, will try again

@katsar0v
Copy link

katsar0v commented Nov 24, 2020

nvcc -V :

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Oct_12_20:09:46_PDT_2020
Cuda compilation tools, release 11.1, V11.1.105
Build cuda_11.1.TC455_06.29190527_0

I confirm that the script does not work on Ubuntu 20.04 with 9750H and nvidia GTX 1650 (same error as above appears)

@jqueguiner
Copy link

jqueguiner commented Jan 14, 2021

On my side u20.04
missing
apt-get install libopus-dev libx264-dev

@Brainiarc7
Copy link
Author

Brainiarc7 commented Jan 15, 2021

@ProTechEx
Copy link

ProTechEx commented Feb 24, 2021

Could you please update, many people need your config

@theonlycure
Copy link

theonlycure commented Mar 1, 2021

@mcdonc
Copy link

mcdonc commented Mar 14, 2021

The snap ffmpeg package indeed works! However, one nit (about snaps in general, not about the ffmpeg snap in particular) is that it is a modern confined snap, and thus cannot access files outside of your home directory or outside of removable media. I even tried installing via snap install --devmode but it had no impact. I can't imagine many people keep their enormous video libraries in their home directory, but there ya go.

@theonlycure
Copy link

theonlycure commented Mar 16, 2021

@mcdonc
Copy link

mcdonc commented Mar 16, 2021

I run the snap package on my scratch drive and the script(s) send the finished encode to the media drive(s) Just call up ffmpeg with /snap/bin/ffmpeg should work without it but I have different ffmpeg versions as default

That's what I'm doing too, it works fine, just had to know to admit defeat :)

@Yuvod-Dev
Copy link

Yuvod-Dev commented May 29, 2021

I have used this:

sudo snap install ffmpeg --channel=4.3.1/stable

but I get the following error:

5/29/2021 13:49:49 164041 [FFDEBUG] FFMPEG:[AVHWDeviceContext @ 0x558f2aa03880] Cannot load libcuda.so.1
[AVHWDeviceContext @ 0x558f2aa03880] Could not dynamically load CUDA
Device creation failed: -1313558101.
[h264 @ 0x558f2aa4f3c0] No device available for decoder: device type cuda needed for codec h264.
Stream mapping:
  Stream #0:2 -> #0:0 (aac (native) -> aac (native))
  Stream #0:1 -> #0:1 (h264 (native) -> h264 (h264_nvenc))
Device setup failed for decoder on input stream #0:1 : Unknown error occurred

any ideas ?

@theonlycure
Copy link

theonlycure commented May 29, 2021

@Brainiarc7
Copy link
Author

Brainiarc7 commented May 29, 2021

@sgjava
Copy link

sgjava commented Aug 24, 2021

--pkg-config-flags="--static" required for ffmpeg build on Ubuntu 20.04.

To fix "ERROR: opus not found using pkg-config"

@theonlycure
Copy link

theonlycure commented Aug 25, 2021

@caio-vinicius
Copy link

caio-vinicius commented Sep 7, 2021

export PATH=$PATH:/usr/local/cuda/bin solved the problem for me.
Seems like nvcc is installed, if the CUDA toolkit was properly installed, but just not in the path.

@mageddo
Copy link

mageddo commented Nov 24, 2021

Got ERROR: nvenc requested, but not all dependencies are satisfied: ffnvcodec

@Pedulla
Copy link

Pedulla commented Nov 30, 2021

Unfortunately, the snap version of ffmpeg does not install in an lxc (sharing a host gtx6000) so I need to compile.
Host is proxmox7, lxc is U20.04. nvidia-smi runs on both host and container.

What are the updates for UB20.04?
Are all the "normal for ubuntu distro" includes in this script?

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