The best way to compile FFmpeg is, of course, to consult the official documentation, which provides the most authoritative and feasible guidance. But it takes a little trickery.

Compilation environment: Ubuntu 20.04

The official documentation states that the installation is non-invasive and will create several directories in the home directory:

Ffmpeg_sources — The source files will be downloaded here. If desired, it can be removed at compile time.

Ffmpeg_build — This is where the files are built and the library is installed. If desired, it can be removed at compile time.

Bin — Install binaries (FFMpeg, ffPlay, ffProbe, X264, x265) here.

Get the dependencies before compilation

If you use the method of changing the server address, automatically select the best server, the dependency package can not be downloaded, the first step is jammed.

1. Go to Software and Updates (Applications -> Software & Updates);

2.Ubuntu Software -> Download from (Download from), click Download from list box to select other sites… (Others…) ;

3. Click on the right to Select the Best Server (S).

4. Go to the Testing Download Servers page.

5. After the Server download test is complete, click S (Choose Server) in the lower right corner.

6. You need to enter the password to replace the server.

7. Click on the lower right corner (Close);

8. Click Reload to pop up a list of possible apps that are out of date.

Just manually select aliyun Server address (China -> mirrors.aliyun.com -> Choose Server), if you do not successfully update all packages the first time, continue to run the following command until all dependencies are downloaded, I executed the command twice before downloading.

sudo apt-get update -qq && sudo apt-get -y install \
  autoconf \
  automake \
  build-essential \
  cmake \
  git-core \
  libass-dev \
  libfreetype6-dev \
  libgnutls28-dev \
  libsdl2-dev \
  libtool \
  libva-dev \
  libvdpau-dev \
  libvorbis-dev \
  libxcb1-dev \
  libxcb-shm0-dev \
  libxcb-xfixes0-dev \
  meson \
  ninja-build \
  pkg-config \
  texinfo \
  wget \
  yasm \
  zlib1g-dev
Copy the code

Some of the packages didn’t download.

Continue executing the above command until all dependencies have been downloaded.

Note: Server users can ignore ffPlay and x11Grab dependencies: libsdl2-dev libva-dev libvdpau-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev

Create a new directory in your home directory to hold all of your source and binaries:

mkdir -p ~/ffmpeg_sources ~/bin
Copy the code

Second, install FFmpeg compilation requires a third-party library

If you want to install some of the most common third-party libraries. Each section provides the commands needed to install the library. For each section, copy and paste the entire code block into the shell.

If certain features are not needed, you can skip the relevant section (if not) and then remove the appropriate./configure option from FFmpeg. For example, if you don’t need libvpx, skip that section and remove –enable-libvpx from the install FFmpeg section.

To experience all the FFmpeg functionality, install all of these libraries.

NASM

An assembler used by some libraries.

The version must be at least 2.13. The version I installed here is 2.14.02-1.

sudo apt-get install nasm
Copy the code

If you are installing a version later than 2.13, you will need to manually download the source code to compile and install it.

CD ~ / ffmpeg_sources && && \ \ wget https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.bz2 tar XJVF Nasm-2.15.05.tar.bz2 && \ CD nasm-2.15.05 && \./autogen.sh && \ PATH="$HOME/bin:$PATH"./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" && \ make && \ make installCopy the code

Other third-party library installations are similar.

libx264

H.264 video encoder. The version must be ≥ 118.

The FFMPEG compilation options are configured –enable-gpl and –enable-libx264.

sudo apt-get install libx264-dev
Copy the code

libx265

H.265/HEVC video encoder. Version ≥ 68.

The FFMPEG compilation options are configured –enable-gpl and –enable-libx265.

sudo apt-get install libx265-dev libnuma-dev
Copy the code

libvpx

VP8/VP9 video encoding and decoder. Version ≥ 1.4.0.

Requires the FFMPEG compilation option configuration –enable-libvpx.

sudo apt-get install libvpx-dev
Copy the code

libfdk-aac

AAC audio coder.

Requires the FFMPEG compilation option configuration –enable-libfdk-aac (plus –enable-nonfree if the configuration includes –enable-gpl).

sudo apt-get install libfdk-aac-dev
Copy the code

libmp3lame

Mp3 audio encoder. Version ≥ 3.98.3.

Requires the FFMPEG compilation option configuration –enable-libmp3lame.

sudo apt-get install libmp3lame-dev
Copy the code

libopus

Opus audio decoder and encoder. Version ≥ 1.1.

Requires the FFMPEG compilation option configuration –enable-libopus.

sudo apt-get install libopus-dev
Copy the code

libsvtav1

AV1 video decoder and encoder. FFmpeg only supports encoders, so compiling does not support decoders.

Requires the FFMPEG compilation option configuration –enable-libsvtav1.

cd ~/ffmpeg_sources && \
git -C SVT-AV1 pull 2> /dev/null || git clone https://gitlab.com/AOMediaCodec/SVT-AV1.git && \
mkdir -p SVT-AV1/build && \
cd SVT-AV1/build && \
PATH="$HOME/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DCMAKE_BUILD_TYPE=Release -DBUILD_DEC=OFF -DBUILD_SHARED_LIBS=OFF .. && \
PATH="$HOME/bin:$PATH" make && \
make install
Copy the code

libdav1d

AV1 decoder, much faster than Libaom.

Requires the FFMPEG compilation option configuration –enable-libdav1d.

sudo apt-get install libdav1d-dev
Copy the code

Without this installation package, you can only manually download the source code, compile and install. If the Linux distribution does not provide enough of the latest meson version (0.49.0 or later), a more recent version needs to be installed. This is easily done with Python package indexing:

sudo apt-get install python3-pip && \
pip3 install --user meson
Copy the code

If you look at MESon, you can see that the version is 0.53.2 and there is no need to update it.

Avx-512 support requires NASM version at least 2.14. I installed version 2.14.02-1, which meets the conditions. Alternatively, use -denable_avX512 =false to disable AVX-512 in the MESon setting.

cd ~/ffmpeg_sources && \
git -C dav1d pull 2> /dev/null || git clone --depth 1 https://code.videolan.org/videolan/dav1d.git && \
mkdir -p dav1d/build && \
cd dav1d/build && \
meson setup -Denable_tools=false -Denable_tests=false --default-library=static .. --prefix "$HOME/ffmpeg_build" --libdir="$HOME/ffmpeg_build/lib" && \
ninja && \
ninja install
Copy the code

Compile FFmpeg

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" \
  --extra-libs="-lpthread -lm" \
  --ld="g++" \
  --bindir="$HOME/bin" \
  --enable-gpl \
  --enable-gnutls \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libsvtav1 \
  --enable-libdav1d \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree && \
PATH="$HOME/bin:$PATH" make && \
make install && \
hash -r
Copy the code

ERROR: gnutls not found using PKg-config ERROR: gnutls not found using PKg-config ERROR: gnutls not found using PKg-config

Libgnutls28-dev is installed in version 3.6.13-2. A quick search on the Internet revealed that this error was reported when ffmpeg was compiled from Ubuntu16.04.

The solution is to install libunistring-dev.

sudo apt-get install libunistring-dev
Copy the code

Compile FFmpeg again and this time it works!

Now re-log in or run the following command for the current shell session to identify the new FFmPEG location (for the environment variable to take effect) :

source ~/.profile
Copy the code

Compilation and installation are now complete, and FFMPEG (along with FFPlay, FFProbe, LAME, X264, and X265) should now be ready to use.

Four, usage,

You can now open a terminal and enter the ffmpeg command, which should execute the new FFMPEG.

If you need more than one user on the same system to access the new FFmpeg, not just the one who compiled it, then move or copy the FFmpeg binary from ~/bin to /usr/local/bin.

Update FFmpeg

Development of FFmpeg is active, with occasional updates providing new features and bug fixes. First, you need to delete (or move) the old file:

rm -rf ~/ffmpeg_build ~/bin/{ffmpeg,ffprobe,ffplay,x264,x265}
Copy the code

Just follow the compile process again.

Restore the changes made when compiling FFmpeg

Delete compile files, source files, and binaries:

rm -rf ~/ffmpeg_build ~/ffmpeg_sources ~/bin/{ffmpeg,ffprobe,ffplay,x264,x265,nasm}
sed -i '/ffmpeg_build/d' ~/.manpath
hash -r
Copy the code

You can also remove packages already installed in compiling FFmpeg:

sudo apt-get autoremove autoconf automake build-essential cmake git-core libass-dev libfreetype6-dev libgnutls28-dev libmp3lame-dev libnuma-dev libopus-dev libsdl2-dev libtool libva-dev libvdpau-dev libvorbis-dev libvpx-dev libx264-dev libx265-dev libxcb1-dev libxcb-shm0-dev ibxcb-xfixes0-dev texinfo wget yasm zlib1g-dev
Copy the code

References:

1. trac.ffmpeg.org/wiki/compil…

2. blog.csdn.net/amu1550/art…