Skip to content

Instantly share code, notes, and snippets.

@gpmnsp
Forked from teocci/compile_ffmpeg.md
Last active October 22, 2017 07:43
Show Gist options
  • Save gpmnsp/adbca236f1dc9d35c38cc1c1cd4b86d2 to your computer and use it in GitHub Desktop.
Save gpmnsp/adbca236f1dc9d35c38cc1c1cd4b86d2 to your computer and use it in GitHub Desktop.
Compile FFmpeg on Ubuntu 16.04

Compile FFmpeg on Ubuntu

This basic guide supports Ubuntu Xenial Xerus 16.04 and will enable several external encoding and decoding libraries: libfaac (AAC encoder), libfdk-aac (AAC encoder), libmp3lame (MP3 encoder), libopencore-amr (AMR encoder/decoder), librtmp (for additional RTMP protocols), libtheora (Theora encoder), libvorbis (Vorbis encoder), libvpx (VP8 encoder/decoder), and libx264 (H.264 encoder). These are optional and may be omitted if desired. This guide will also install many filters (see the filter list in the Filtering Guide.

Note: Copy and paste the whole code box for each step.

Preparation

Remove any existing packages:

sudo apt -y  remove ffmpeg x264 libav-tools libvpx-dev libx264-dev

Get the dependencies (Ubuntu Desktop users):

sudo apt-get update
sudo apt-get -y install build-essential checkinstall git libfaac-dev libgpac-dev \
  libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev \
  librtmp-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev \
  libx11-dev libxfixes-dev pkg-config texi2html yasm zlib1g-dev

Get the dependencies (Ubuntu Server or headless users):

sudo apt-get update
sudo apt-get -y install build-essential checkinstall git libfaac-dev libgpac-dev \
  libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev librtmp-dev libtheora-dev \
  libvorbis-dev pkg-config texi2html yasm zlib1g-dev

Now make a directory for the source files that will be downloaded later in this guide:

mkdir ~/ffmpeg_sources

Installation

Install Yasm:

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
make install

Install nasm:

NASM assembler. Required for compilation of x264 and other tools.

cd ~/ffmpeg_sources
wget http://www.nasm.us/pub/nasm/releasebuilds/2.13.01/nasm-2.13.01.tar.bz2
tar xjvf nasm-2.13.01.tar.bz2
cd nasm-2.13.01
./autogen.sh
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
PATH="$HOME/bin:$PATH" make
make install

libx264 | H.264 video encoder

Requires ffmpeg to be configured with --enable-gpl --enable-libx264. See the H.264 Encoding Guide for more information and usage examples.

If your repository provides libx264-dev version ≥ 118 then you can install that instead of compiling:

sudo apt-get install libx264-dev

Otherwise you can compile:

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
make install

Note: You can download the nightly x264 source snapshot as an alternative to using git.

libx265 | H.265/HEVC video encoder.

See the H.265 Encoding Guide for more information and usage examples.

If your repository provides libx265-dev version ≥ 68 then you can install that instead of compiling:

sudo apt-get install libx265-dev

Otherwise you can compile:

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
make install

libfdk-aac | AAC audio encoder.

See the AAC Audio Encoding Guide for more information and usage examples.

Requires ffmpeg to be configured with --enable-libfdk-aac (and --enable-nonfree if you also included --enable-gpl).

If your repository provides libfdk-aac-dev then you can install that instead of compiling:

sudo apt-get install libfdk-aac-dev

Otherwise you can compile:

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
make install

libmp3lame | MP3 audio encoder.

Requires ffmpeg to be configured with --enable-libmp3lame.

If your repository provides libmp3lame-dev version ≥ 3.98.3 then you can install that instead of compiling:

sudo apt-get install libmp3lame-dev

Otherwise you can compile:

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
make install

libopus | Opus audio decoder and encoder.

Requires ffmpeg to be configured with --enable-libopus.

If your repository provides libopus-dev version ≥ 1.1 then you can install that instead of compiling:

sudo apt-get install libopus-dev

Otherwise you can compile:

cd ~/ffmpeg_sources
wget https://archive.mozilla.org/pub/opus/opus-1.1.5.tar.gz
tar xzvf opus-1.1.5.tar.gz
cd opus-1.1.5
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install

libvpx | VP8/VP9 video encoder and decoder. See the VP8 Video Encoding Guide for more information and usage examples.

Requires ffmpeg to be configured with --enable-libvpx.

If your repository provides libvpx-dev version ≥ 0.9.7 then you can install that instead of compiling:

sudo apt-get install libvpx-dev

Otherwise you can compile:

sudo apt-get install git
cd ~/ffmpeg_sources
git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git
cd libvpx
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests
PATH="$HOME/bin:$PATH" make
make install

Note: You can download a libvpx source snapshot as an alternative to using git.

FFmpeg

cd ~/ffmpeg_sources
git clone --depth 1 https://git.ffmpeg.org/ffmpeg.git
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-gpl \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopencore-amrnb \
  --enable-libopencore-amrwb \
  --enable-librtmp \
  --enable-libopus \
  --enable-libtheora \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree \
  --enable-version3 \
  --enable-x11grab \
PATH="$HOME/bin:$PATH" make
make install
hash -r

Note: You can download the nightly FFmpeg source snapshot as an alternative to using git.

cd ~/ffmpeg_sources
wget http://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-gpl \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopencore-amrnb \
  --enable-libopencore-amrwb \
  --enable-librtmp \
  --enable-libopus \
  --enable-libtheora \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree
PATH="$HOME/bin:$PATH" make
make install
hash -r

Note: Ubuntu Server users should omit --enable-x11grab.

Finish

Installation is now complete and FFmpeg is now ready for use. You can keep the x264, libvpx, and ffmpeg directories in your home directory if you plan on updating later. See Updating FFmpeg below for more details. Some optional steps are next followed by instructions on updating FFmpeg and finally instructions on reverting all changes made by this guide.

Optional Installation

qt-faststart

This is a useful tool if you're showing your H.264 in MP4 videos on the web. It relocates some data in the video to allow playback to begin before the file is completely downloaded. Usage: qt-faststart input.mp4 output.mp4.

cd ~/ffmpeg
make tools/qt-faststart
make install

Updating FFmpeg

Development of FFmpeg is active and an occasional update can give you new features and bug fixes. First you need to delete (or move) the old files:

sudo apt -y remove ffmpeg x264 libx264-dev libvpx-dev
sudo apt update
sudo apt -y install build-essential checkinstall git libfaac-dev libgpac-dev \
  libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev \
  librtmp-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev \
  libx11-dev libxfixes-dev texi2html yasm zlib1g-dev

Or just removing the source and build directories.

rm -rf ~/ffmpeg_build ~/ffmpeg_sources ~/bin/{ffmpeg,ffprobe,ffplay,ffserver,x264,x265}

Now just follow the guide from the beginning.

x264

cd ~/ffmpeg_sources
cd x264-snapshot*
make distclean
git pull

Now run ./configure, make, and make install as shown in the install x264 section.

libvpx

cd ~/ffmpeg_sources
cd ~/libvpx
make clean
git pull

Now run ./configure, make, and make install as shown in the install libvpx section.

FFmpeg

cd ~/ffmpeg_sources
cd ~/ffmpeg
make distclean
git pull

Now run ./configure, make, and make install as shown in the install FFmpeg section.

Reverting Changes Made by This Guide

To remove FFmpeg/x264 and other packages added for this guide:

rm -rf ~/ffmpeg_build ~/ffmpeg_sources ~/bin/{ffmpeg,ffprobe,ffplay,ffserver,x264,x265,nasm}
sudo apt autoremove autoconf automake build-essential cmake libass-dev libfreetype6-dev \
  libmp3lame-dev libopus-dev libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev \
  libvorbis-dev libvpx-dev libx264-dev libxcb1-dev libxcb-shm0-dev ibxcb-xfixes0-dev mercurial texinfo zlib1g-dev
sed -i '/ffmpeg_build/c\' ~/.manpath
hash -r

Or remove the packages and manually remove the directories created by this guide:

sudo apt -y autoremove build-essential checkinstall fdk-aac ffmpeg git libfaac-dev libgpac-dev \
  libjack-jackd2-dev libmp3lame-dev librtmp-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev \
  libvorbis-dev libvpx libx11-dev libxfixes-dev pkg-config qt-faststart texi2html x264 yasm zlib1g-dev
hash -r

Lastly, delete the x264, fdk-aac, libvpx, and ffmpeg directories in your home folder.

#!/bin/bash
###############################
# Compilation & Installation #
###############################
# You can compile ffmpeg to your liking. If you do not require certain encoders you may skip the relevant section and then remove the appropriate ./configure option in FFmpeg. For example, if libopus is not needed, then skip that section and then remove --enable-libopus from the Install FFmpeg section.
# This guide is designed to be non-intrusive and will create several directories in your home directory:
# ffmpeg_sources – Where the source files will be downloaded. This can be deleted if desired when finished with the guide.
# ffmpeg_build – Where the files will be built and libraries installed. This can be deleted if desired when finished with the guide.
# bin – Where the resulting binaries (ffmpeg, ffplay, ffserver, x264, x265) will be installed.
# You can easily undo any of this as shown in Reverting Changes Made by This Guide. (https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu#RevertingChangesMadebyThisGuide)
### First install the dependencies: ###
sudo apt-get update
sudo apt-get -y install autoconf automake build-essential libass-dev libfreetype6-dev \
libtheora-dev libtool libvorbis-dev pkg-config texinfo wget zlib1g-dev
# Note: Server users can omit the ffplay and x11grab dependencies:
# sudo apt-get -y install libsdl2-dev libva-dev libvdpau-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev
### Now make a directory for the source files that will be downloaded later in this guide: ###
mkdir ~/ffmpeg_sources
### Yasm ###
# An assembler for x86 optimizations used by x264 and FFmpeg. Highly recommended or your resulting build may be very slow.
# If your repository provides yasm version ≥ 1.2.0 then you can install that instead of compiling:
sudo apt-get -y install yasm
## Otherwise you can compile:
# 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
# make install
### nasm ###
# NASM assembler. Required for compilation of x264 and other tools.
cd ~/ffmpeg_sources
wget http://www.nasm.us/pub/nasm/releasebuilds/2.13.01/nasm-2.13.01.tar.bz2
tar xjvf nasm-2.13.01.tar.bz2
cd nasm-2.13.01
./autogen.sh
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
PATH="$HOME/bin:$PATH" make
make install
### libx264 ###
# H.264 video encoder. See the H.264 Encoding Guide for more information and usage examples.
# Requires ffmpeg to be configured with --enable-gpl --enable-libx264.
# If your repository provides libx264-dev version ≥ 118 then you can install that instead of compiling:
sudo apt-get -y install libx264-dev
# Otherwise you can compile:
# 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
# make install
### libx265 ###
# H.265/HEVC video encoder. See the H.265 Encoding Guide for more information and usage examples.
# If your repository provides libx265-dev version ≥ 68 then you can install that instead of compiling:
# sudo apt-get -y install libx265-dev
# Otherwise you can compile:
sudo apt-get -y 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
make install
### libfdk-aac ###
# AAC audio encoder. See the AAC Audio Encoding Guide for more information and usage examples.
# Requires ffmpeg to be configured with --enable-libfdk-aac (and --enable-nonfree if you also included --enable-gpl).
# If your repository provides libfdk-aac-dev then you can install that instead of compiling:
sudo apt-get -y install libfdk-aac-dev
# Otherwise you can compile:
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
make install
### libmp3lame ###
# MP3 audio encoder.
# Requires ffmpeg to be configured with --enable-libmp3lame.
# If your repository provides libmp3lame-dev version ≥ 3.98.3 then you can install that instead of compiling:
sudo apt-get -y install libmp3lame-dev
# Otherwise you can compile:
# 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
# make install
### libopus ###
# Opus audio decoder and encoder.
# Requires ffmpeg to be configured with --enable-libopus.
# If your repository provides libopus-dev version ≥ 1.1 then you can install that instead of compiling:
sudo apt-get -y install libopus-dev
# Otherwise you can compile:
# cd ~/ffmpeg_sources
# wget https://archive.mozilla.org/pub/opus/opus-1.1.5.tar.gz
# tar xzvf opus-1.1.5.tar.gz
# cd opus-1.1.5
# ./configure --prefix="$HOME/ffmpeg_build" --disable-shared
# make
# make install
### libvpx ###
# VP8/VP9 video encoder and decoder. See the VP9 Video Encoding Guide for more information and usage examples.
# Requires ffmpeg to be configured with --enable-libvpx.
# If your repository provides libvpx-dev version ≥ 0.9.7 then you can install that instead of compiling:
sudo apt-get -y install libvpx-dev
# Otherwise you can compile:
# sudo apt-get install git
# cd ~/ffmpeg_sources
# git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git
# cd libvpx
# PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth
# PATH="$HOME/bin:$PATH" make
# make install
### Optional: install qt-faststart ###
# This is a useful tool if you're showing your H.264 in MP4 videos on the web. It relocates some data in the video to allow playback to begin before the file is completely downloaded. Usage: qt-faststart input.mp4 output.mp4.
cd ~/ffmpeg
make tools/qt-faststart
sudo checkinstall --pkgname=qt-faststart --pkgversion="$(date +%Y%m%d%H%M)-git" --backup=no \
--deldoc=yes --fstrans=no --default install -Dm755 tools/qt-faststart \
/usr/local/bin/qt-faststart
### ffmpeg ###
cd ~/ffmpeg_sources
wget http://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" \
--extra-libs=-lpthread \
--bindir="$HOME/bin" \
--enable-gpl \
--enable-libass \
--enable-libfdk-aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libtheora \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-nonfree
PATH="$HOME/bin:$PATH" make
make install
hash -r
### Conclusion ###
# Installation is now complete and ffmpeg is now ready for use. Your newly compiled FFmpeg programs are in ~/bin.
# Usage
# There are several methods to use your new ffmpeg.
# Navigate to ~/bin and execute the binary: cd ~/bin && ./ffmpeg -i ~/input.mp4 ~/videos/output.mkv (notice the ./)
# Or use the full path to the binary: /home/yourusername/bin/ffmpeg -i ../input.mp4 ../videos/output.mkv
# If you want the ffmpeg command to just work from anywhere:
# Log in and log out
# Or run source ~/.profile
# Note: ~/bin is included in the standard Ubuntu $PATH by default (via the ~/.profile file), but only when the ~/bin directory actually exists. This is why you must log out then log in or run source ~/.profile if you just created ~/bin. See â€Ubuntu Wiki: Persistent Environment Variables for more info.
# Additional Notes
# See the H.264 Encoding Guide for some encoding examples.
# If you do not see FFmpeg developers in your ffmpeg console output then something went wrong and you're probably using the â€fake "ffmpeg" from the repository (the counterfeit "ffmpeg" was eventually removed and the real ffmpeg returned in 15.04).
# You can delete the ffmpeg_sources directory if you want to.
### Updating FFmpeg ###
# Development of FFmpeg is active and an occasional update can give you new features and bug fixes. First you need to delete (or move) the old files:
# rm -rf ~/ffmpeg_build ~/ffmpeg_sources ~/bin/{ffmpeg,ffprobe,ffplay,ffserver,x264,x265}
# Now just follow the guide from the beginning.
### Reverting Changes Made by This Guide ###
# rm -rf ~/ffmpeg_build ~/ffmpeg_sources ~/bin/{ffmpeg,ffprobe,ffplay,ffserver,x264,x265,nasm}
# sudo apt-get autoremove autoconf automake build-essential cmake libass-dev libfreetype6-dev \
# libmp3lame-dev libopus-dev libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev \
# libvorbis-dev libvpx-dev libx264-dev libxcb1-dev libxcb-shm0-dev ibxcb-xfixes0-dev mercurial texinfo zlib1g-dev
# sed -i '/ffmpeg_build/c\' ~/.manpath
# hash -r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment