This guide will show you how to install and use FFmpeg in Ubuntu 20.04.
Installing FFmpeg in Ubuntu
There are two official ways of installing FFmpeg on Ubuntu: from the Ubuntu repo (v7.x) and from the snap (v4.x). Depending on your needs, you should choose the most appropriate method for you.
It is also possible to compile and install FFmpeg from the source. However, this method is a bit complex and it is recommended not to follow this method unless you have a specific reason to do so.
Installing FFmpeg from Ubuntu Repo
This is the default method for installing FFmpeg. All you have to do is tell APT to grab and install the program from the default Ubuntu software repo.
Fire up a terminal, update the APT cache, and install FFmpeg.
$ sudo apt install ffmpeg
Let us verify whether the installation was successful. First, test the FFmpeg version via the following command:
Do not forget to check the available encoders and decoders. Do so by entering the following command:
Installing FFmpeg from snap
FFmpeg is also available as a snap package. If you do not have snap configured, then you can install it right away by issuing the following command:
$ sudo apt update && sudo apt install snapd -y
$ sudo snap install core core20 && sudo systemctl restart snapd
Now, your system should be ready to grab and install snap packages from the Snapcraft store. Check out FFmpeg on Snapcraft.
$ sudo snap install ffmpeg
Test out the installation of FFmpeg by entering the following command:
$ ffmpeg -encoders
$ ffmpeg -decoders
Installing FFmpeg from the Source Code
FFmpeg is an open-source tool. It is therefore possible to manually build this program from the source code. This method is only recommended if you are willing to create a custom build, want to try out the latest version, or wish to test out a bug. For general use, follow the other methods instead to install this program. Check out the official FFmpeg compilation guide for Ubuntu.
Building FFmpeg from the source requires several dependencies. Enter the following commands to download the dependencies:
$ sudo apt install
Now, prepare a dedicated directory for storing the source code and the compiled binary files.
$ mkdir -pv ~/ffmpeg_source ~/bin
It is now time to prepare some third-party libraries. These are the most common ones used with FFmpeg. If you do not need one or more of these libraries, then skip the relevant part and ignore the associated ./configure option.
- NASM: An assembler that some libraries rely on.
- libx264: The H.264 video encoder.
$ sudo apt install -y libx264-dev
- libx265: The H.265 video encoder (also known as HEVC).
$ sudo apt install -y libx265-dev libnuma-dev
- libvpx: The VP8/VP9 video encoder/decoder.
$ sudo apt install -y libvpx-dev
- libfdk-aac: The AAC audio encoder.
$ sudo apt install -y libfdk-aac-dev
- libmp3lame: The MP3 audio encoder.
$ sudo apt install libmp3lame-dev
- libopus: The Opus audio encoder/decoder.
$ sudo apt install libopus-dev
- libaom: The AV1 video encoder/decoder. Note that if you are going to use this one, the compilation may fail. According to the official FFmpeg wiki, it seems like this package does not have a stable API yet. It is recommended to avoid this one.
$ sudo apt install -y libaom-dev
You are now ready to start building FFmpeg from the source. Grab the source code by issuing the following:
$ cd ~/ffmpeg_source
$ wget -O ffmpeg-snapshot.tar.bz2 https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
$ tar -xvf ffmpeg-snapshot.tar.bz2
Update the PATH environment variable and run the configuration script.
$ PATH=“$HOME/bin:$PATH“ PKG_CONFIG_PATH=“$HOME/ffmpeg_build/lib/pkgconfig” ./configure
$ –extra-libs=“-lpthread -lm”
During this process, you may encounter the error “gnutls not found using pkg-config.” To solve this problem, a certain package must be present in the system. You can install the relevant package and fix this error by issuing the following command:
$ sudo apt install -y libunistring-dev
Now, run the configuration script again.
Run the make command to start compiling FFmpeg. Use the “-j” flag to run parallel compilation to speed up the process.
$ PATH=“$HOME/bin:$PATH“ make -j4
You can now install the FFmpeg version that you just built from the source via the following command:
Reload the bash shell to recognize the new FFmpeg binary location.
Test out the FFmpeg installation via the following commands:
$ ffmpeg -encoders
$ ffmpeg -decoders
After following the steps above, you have now successfully installed FFmpeg. It is time to learn how to use it. This section will show you some of the most common uses of this tool.
First, convert a video file from MP4 to WebM format. The beauty of FFmpeg is that you do not have to specify the input and output formats. FFmpeg will automatically detect the source and target format and act accordingly. Here, the “-hide_banner” flag is used to disable the configuration information that FFmpeg reports on each run.
$ ffmpeg -hide_banner -i <input> <output>
Let us now take a look at converting audio files. Convert an MP3 to OGG.
$ ffmpeg -hide_banner -i demo.mp3 demo.ogg
When converting files, it is also possible to specify the codec. Use the “-c” flag, followed by the name of any supported encoder/decoder, or a special value copy. For example, you can convert an MP4 into WebM format using the libvpx video codec and libvorbis audio codec.
$ ffmpeg -hide_banner -i demo.mp4 -c:v libvpx -c:a libvorbis demo.webm
Similarly, it is also possible to convert the audio format using a specified codec. For example, you can convert an MP3 file to OGG using the libopus codec.
$ ffmpeg -hide_banner -i demo.mp3 -c:a libopus demo.ogg
FFmpeg is a powerful tool for working with media files. There are plenty of tutorials on various features of FFmpeg and its uses. Feel free to explore more about this amazing tool. Grab a couple of demo media files and practice with them to master FFmpeg. Check out this interesting tutorial on how to transform multiple images into a video file.