How to install ffmpeg on Ubuntu 20.04 LTS
sudo apt updatesudo apt install ffmpeg
which ffmpeg
/usr/bin/ffmpeg
ffmpeg
Install ffmpeg from source
mkdir -p ~/ffmpeg_sources ~/bin ~/ffmpeg_build
Install the packages required for compiling.
sudo apt install -y
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 \
yasm \
zlib1g-dev
Install the assembler used by some libraries.
sudo apt install nasm
Install the most common third-party libraries.
sudo apt install -y libx264-dev libx265-dev libnuma-dev libvpx-dev libfdk-aac-dev libmp3lame-dev libopus-dev
There are three common libraries which we need to compile and install directly from their source code.
libdav1d
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" --bindir="$HOME/ffmpeg_build/bin" --libdir="$HOME/ffmpeg_build/lib" && \
ninja && \
ninja install
libsvtav1
cd ~/ffmpeg_sources && \
git -C SVT-AV1 pull 2> /dev/null || git clone https://github.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 instal
libaom
cd ~/ffmpeg_sources && \
git -C aom pull 2> /dev/null || git clone --depth 1 https://aomedia.googlesource.com/aom && \
mkdir -p aom_build && \
cd aom_build && \
PATH="$HOME/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED=off -DENABLE_NASM=on ../aom && \
PATH="$HOME/bin:$PATH" make && \
make install
- libx264 free software library for encoding video streams into the H.264/MPEG-4 AVC compression format.
- libx265 encodes video streams with around 25-50% bitrate savings compared to H.264.
- libvpx free software video codec library from Google for the VP8 and VP9 video coding formats.
- libfdk-aac-dev codec library for Android, supports an encoder implementation of the Advanced Audio Coding (AAC).
- libmp3lame-dev MP3 encoding library.
- libopus-dev codec for interactive speech and audio transmission over the internet.
Download and extract the source code for ffmpeg.
cd ~/ffmpeg_sources
wget -O ffmpeg-snapshot.tar.bz2 https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
tar xjvf ffmpeg-snapshot.tar.bz2
rm ffmpeg-snapshot.tar.bz2
The wget utility, installed by default on Ubuntu 20.04 LTS, helps to download files from the web. After retrieving the ffmpeg source code inside the ffmpeg_sources directory, we unpack it with the help of the tar utility.
Then we remove the archive from the directory.
Compile and install ffmpeg with the command shown below.
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" \
--bindir="$HOME/bin" \
--enable-gpl \
--enable-gnutls \
--enable-libaom \
--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
The --enable option helps to link a library to the ffmpeg builds. As for the --bindir option, it specifies the location of the binaries.
It may take a while to compile and install; so be patient.
On success you should have the following binaries inside the $HOME/bin directory.
Reload your current shell for the changes to take place.
source ~/.profile
Verify the installation
The which command locates a binary.
which ffmpeg
The result is shown below.
/home/janoroot/bin/ffmpeg
The ffmpeg installed with the help of apt-get is also available under the /usr/bin directory.
Information about the tools in the framework
ffprobe is a standalone executable which is useful to probe a media file for information such as the format and streams. Usually a video has an audio stream and a video one.
A practical example of ffprobe in action is shown below.
ffprobe -show_format -show_streams Downloads/vrak.mp4
The result of the above command is shown below.
[STREAM]
index=0
codec_name=h264
codec_long_name=H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
profile=Main
codec_type=video
codec_time_base=1/60
codec_tag_string=avc1
codec_tag=0x31637661
width=256
height=144
coded_width=256
coded_height=144
closed_captions=0
has_b_frames=1
sample_aspect_ratio=1:1
display_aspect_ratio=16:9
pix_fmt=yuv420p
level=12
color_range=tv
color_space=bt709
color_transfer=bt709
color_primaries=bt709
chroma_location=left
field_order=unknown
timecode=N/A
refs=1
is_avc=true
nal_length_size=4
id=N/A
r_frame_rate=30/1
avg_frame_rate=30/1
time_base=1/15360
start_pts=0
start_time=0.000000
duration_ts=3503104
duration=228.066667
bit_rate=72521
max_bit_rate=N/A
bits_per_raw_sample=8
nb_frames=6842
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=1
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
DISPOSITION:timed_thumbnails=0
TAG:language=und
TAG:handler_name=ISO Media file produced by Google Inc. Created on: 02/23/2019.
TAG:vendor_id=[0][0][0][0]
[/STREAM]
[STREAM]
index=1
codec_name=aac
codec_long_name=AAC (Advanced Audio Coding)
profile=LC
codec_type=audio
codec_time_base=1/44100
codec_tag_string=mp4a
codec_tag=0x6134706d
sample_fmt=fltp
sample_rate=44100
channels=2
channel_layout=stereo
bits_per_sample=0
id=N/A
r_frame_rate=0/0
avg_frame_rate=0/0
time_base=1/44100
start_pts=0
start_time=0.000000
duration_ts=10060800
duration=228.136054
bit_rate=127999
max_bit_rate=127999
bits_per_raw_sample=N/A
nb_frames=9825
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=1
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
DISPOSITION:timed_thumbnails=0
TAG:language=eng
TAG:handler_name=ISO Media file produced by Google Inc. Created on: 02/23/2019.
TAG:vendor_id=[0][0][0][0]
[/STREAM]
[FORMAT]
filename=Downloads/vrak.mp4
nb_streams=2
nb_programs=0
format_name=mov,mp4,m4a,3gp,3g2,mj2
format_long_name=QuickTime / MOV
start_time=0.000000
duration=228.137000
size=5947375
bit_rate=208554
probe_score=100
TAG:major_brand=isom
TAG:minor_version=512
TAG:compatible_brands=isomiso2avc1mp41
TAG:encoder=Lavf58.19.102
[/FORMAT]
The information is structured based on tags so it can be easily filtered by text processing tools, or your custom scripts. As you can see, each stream has an index, starting from 0.
FFplay is a fast media player. It's simplest usage is shown below.
ffplay Downloads/vrak.mp4
ffmpeg is complex. To put it in simple words, it is an open source solution for converting, recording and streaming audio and video.
The following command illustrates how to convert a video from mp4 to avi.
ffmpeg -i Downloads/vrak.mp4 Downloads/vrak.avi
The -i option specifies the input media.
Test the libraries
First let's test the mp3 encoder. FFmpeg does not have a native encoder for mp3, so we have to use the libmp3lame library.
The following command converts a wav audio file to an mp3 one.
ffmpeg -i Downloads/bratjaga.wav -codec:a libmp3lame Downloads/bratjaga.mp3
The -codec:a option specifies the encoding library, which in this case is the libmp3lame one.
Let's test the libfdk-aac-dev library. Being the successor format to mp3, for the music the .m4a extension is used.
The following command converts the audio file to AAC in an M4A container.
ffmpeg -i Downloads/bratjaga.wav -c:a libfdk_aac -b:a 128k Downloads/bratjaga.m4a
As for libvpx, it is the VP8 video encoder for WebM. For the purpose of demonstration, I am going to encode an mp4 video to webm.
ffmpeg -i Downloads/rap.mp4 -c:v libvpx -b:v 1M -c:a libvorbis Downloads/rap.webm
Depending on the size of the video, the encoding takes some time.
Play the encoded webm media with the help of FFplay.
ffplay Downloads/rap.webm
The command shown below encodes a video with good quality by using the libx264 library.
ffmpeg -i Downloads/rap.mp4 -c:v libx264 -preset slow -crf 22 -c:a copy Downloads/rap.mkv
To test the libx265 library, just change the name in the above command.
Final thoughts
If you have any question, feel free to leave a comment below.
Feel free to follow my work on Instagram.
Leave a Comment