Ubuntu 16.04: Install FFmpeg 3.0

This article will describe installing FFmpeg 3.0 which is able to encode H264 and running C++ code.

 

1 Build and install OpenH264

FFmpeg 3.0 needs OpenH264 v1.5.0. OpenH264 v1.5.0 will be installed to /usr/local.

$ sudo apt-get install -y nasm
$ git clone https://github.com/cisco/openh264
$ cd openh264
$ git checkout v1.5.0 -b v1.5.0
$ make && sudo make install

2 Build and install FFmpeg

Build and install FFmpeg 3.0, this will be installed to /usr/local.

$ sudo apt-get build-dep -y ffmpeg
$ git clone https://github.com/FFmpeg/FFmpeg
$ cd FFmpeg
$ git checkout n3.0.1 -b n3.0.1
$ ./configure \
  --toolchain=hardened \
  --enable-libopenh264 \
  --enable-gpl \
  --enable-shared \
  --disable-stripping \
  --disable-decoder=libopenjpeg \
  --disable-decoder=libschroedinger \
  --enable-avresample \
  --enable-avisynth \
  --enable-gnutls \
  --enable-ladspa \
  --enable-libass \
  --enable-libbluray \
  --enable-libbs2b \
  --enable-libcaca \
  --enable-libcdio \
  --enable-libflite \
  --enable-libfontconfig \
  --enable-libfreetype \
  --enable-libfribidi \
  --enable-libgme \
  --enable-libgsm \
  --enable-libmodplug \
  --enable-libmp3lame \
  --enable-libopenjpeg \
  --enable-libopus \
  --enable-libpulse \
  --enable-librtmp \
  --enable-libschroedinger \
  --enable-libshine \
  --enable-libsnappy \
  --enable-libsoxr \
  --enable-libspeex \
  --enable-libssh \
  --enable-libtheora \
  --enable-libtwolame \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libwavpack \
  --enable-libwebp \
  --enable-libx265 \
  --enable-libxvid \
  --enable-libzvbi \
  --enable-openal \
  --enable-opengl \
  --enable-x11grab
$ make && sudo make install

Run ffmpeg command after export LD_LIBRARY_PATH=/usr/local/lib.

$ export LD_LIBRARY_PATH=/usr/local/lib # Please write into ${HOME}/.bashrc
$ ffmpeg
ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 5.3.1 (Ubuntu 5.3.1-14ubuntu2.1) 20160413
  configuration: --prefix=/usr/local --toolchain=hardened
  --enable-libopenh264 --enable-gpl --enable-shared --disable-stripping
  --disable-decoder=libopenjpeg --disable-decoder=libschroedinger
  --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa
  --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca
  --enable-libcdio --enable-libflite --enable-libfontconfig
  --enable-libfreetype --enable-libfribidi --enable-libgme
  --enable-libgsm --enable-libmodplug --enable-libmp3lame
  --enable-libopenjpeg --enable-libopus --enable-libpulse
  --enable-librtmp --enable-libschroedinger --enable-libshine
  --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh
  --enable-libtheora --enable-libtwolame --enable-libvorbis
  --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265
  --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl
  --enable-x11grab


  libavutil      55. 17.103 / 55. 17.103
  libavcodec     57. 24.102 / 57. 24.102
  libavformat    57. 25.100 / 57. 25.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 31.100 /  6. 31.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

3 Running C++ code

A pkg-config is useful for getting CXXFLAGS and LDLIBS. Below Makefile will make *.cc file in current directory.

FFMPEG_LIB = libavformat libavutil
LDLIBS := $(shell pkg-config $(FFMPEG_LIB) --libs)
CXXFLAGS = $(shell pkg-config $(FFMPEG_LIB) --cflags)
CXXFLAGS += -Wno-deprecated-declarations $(EXTRA_CXXFLAGS)
PROG := $(patsubst %.cc,%,$(wildcard *.cc))

all: $(PROG)

debug:
        EXTRA_CXXFLAGS=-g $(MAKE) all

clean:
        @$(RM) -f $(PROG)

Running below test.cc. C++ code needs extern "C" around include FFmpeg header because FFmpeg does not used it inside header.

#include <iostream>

extern "C" {
#include <libavformat/avformat.h>
}

int main()
{
  av_register_all();
  AVFrame *dummy = av_frame_alloc();
  av_frame_free(&dummy);
  return 0;
}

Build is completed and run after export LD_LIBRARY_PATH=/usr/local/lib.

$ ls
Makefile  test.cc
$ make
g++ -I/usr/local/include -Wno-deprecated-declarations     test.cc
-L/usr/local/lib -lavformat -lavutil -o test
$ ls
Makefile  test  test.cc