How to Install and Use FFmpeg on Ubuntu 18.04 / 16.04

FFmpeg is a very popular, cross-platform, free and open-source multimedia framework, used for transcoding multimedia files. FFmpeg is actually a collection of different projects for manipulating multimedia files. It’s not only used by users to handle media files, but it’s quite often used by many softwares, behind the scenes. You may have used it before without knowing.

In this tutorial we’ll focus on installing the stable version (3.x), and the latest version (4.x) of FFmpeg on Ubuntu 18.04 or Ubuntu 16.04 (The same steps apply for both).

Furthermore, we’ll get into using the FFmpeg command-line tool, which is only a single part of the entire FFmpeg project.

Prerequisites

  • This tutorial works for when you’re root, but we recommend you use a non-root sudo userwhen making changes to your system. To create a sudo user, and read why it’s recommended that you use one, you can follow our tutorial: How to Create a Sudo User on Ubuntu

Currently, the stable version is FFmpeg 3.x, but the latest version is FFmpeg 4.x.

We’ll see how to install both of them, and you can choose which one fits your needs.

Install FFmpeg 3.x on Ubuntu

At the time of writing, the latest stable version is FFmpeg 3.4.4.

Step 1 – Install FFmpeg 3.x

To install it, update your package index then install FFmpeg by using the apt package manager:

$ sudo apt-get update
$ sudo apt-get install ffmpeg

Step 2 – Check FFmpeg Version

Let’s check that FFmpeg is properly installed by checking its’ version:

$ ffmpeg -version

Here’s my output:

ffmpeg version 3.4.4-0ubuntu0.18.04.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)

That’s it. Installing the stable version is that simple, and you start using it.

Install FFmpeg 4.x on Ubuntu

FFmpeg 4.x is the newest major release, and it’s packed with new features. You can check them out and the latest changes to FFmpeg on their website in the News subsection.

Step 1 – Add the FFmpeg Unofficial PPA

The latest version of FFmpeg is 4.x. It’s currently not in the Official Ubuntu Repositories, so to install it you’ll have to configure PPA on your system. To do this, add Jonathon F’s PPA repository by running the following command:

$ sudo add-apt-repository ppa:jonathonf/ffmpeg-4

If you get you get the error sudo: add-apt-repository: command not found when running the above command, then just install software-properties-common and try adding the PPA again:
$ sudo apt-get install software-properties-common

Step 2 – Install FFmpeg 4.x

With the unofficial PPA added, we can proceed with the installation. Update your package index and install FFmpeg via the apt package manager:

$ sudo apt-get update
$ sudo apt-get install ffmpeg

Step 3 – Check the FFmpeg Version

To make sure that the installation was successful run the following command to check our FFmpeg version:

$ ffmpeg -version

Here’s my output:

ffmpeg version 4.0.3-1~18.04.york0 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-27ubuntu1~18.04)

Well done. The installation was successful and now you can start using the latest version of FFmpeg.

FFmpeg Examples & Basic Commands

If you’re just starting out, these examples will hopefully give you a feel of how FFmpeg works.

For the purposes of this tutorial, we’ll quickly download a sample video file to play with, from here https://sample-videos.com. You’ll find a list of videos, of different sizes, formats and resolutions.

$ wget https://sample-videos.com/video123/mp4/480/big_buck_bunny_480p_20mb.mp4

NOTE: There are many command options we could talk about, but it is beyond the scope of this tutorial. For a thorough understanding of FFmpeg we suggest you read the FFmpeg documentation on their website or their wiki ( which is very well organized and we strongly recommend ).

01. Help

You can see a list of command options by using:

$ ffmpeg -h

02. Get file info

With the file downloaded, use the -i command option will display the to display information on the video file:

$ ffmpeg -i big_buck_bunny_480p_20mb.mp4

03. Convert files

Most people need FFmpeg to easily convert files. This process is typically complex when done manually, but FFmpeg makes this easy by automatically selecting the correct codecs and container.

Convert Video Files

Say you have an mp4 video file and you want to convert it to webm. To do this we simply run:

$ ffmpeg -i big_buck_bunny_480p_20mb.mp4 big_buck_bunny_480p_20mb.webm

The syntax of this command basically looks like this:

$ ffmpeg -i [input_file.current_format] [output_file.desired_format] 

Convert Audio Files

The basic conversion of audio files looks exactly the same. We don’t have an audio file available so we’ll just leave the example. Say we have Despacito.mp3 ʰᵃʰᵃ and we need to convert it to ogg

$ ffmpeg -i Despacito.mp3 Despacito.ogg

Convert Video to Audio Files

In the same fashion, you can convert a video file to an audio file. Some would call this extracting audio.

Say you’ve downloaded the Despacito video, in flv format, from somewhere, and you want to convert it to an mp3. To do that you’d simply run:

$ ffmpeg -i Despacito.flv -vn Despacito.mp3

-vn – means that we’ve disabled video in the output file

To see a list of supported formats by FFmpeg, run:

$ ffmpeg -formats

The following examples don’t cover all of the cases you’ll encounter, and you may not understand some options we’re adding and it may be confusing to go through the all the docs.

04. Compress Media Files

As you probably know, compressing files means to reduce their size.

Compressing Video Files

One of the reasons for the docs being confusing is that many encoders have their own separate “private options”.

A good thing is that we find that the codec most used when dealing with video files is libx264, and we believe you can get a good understanding of it from the following FFmpeg Wiki Page, without having to go through all of the documentation – H.264 Video Encoding Guide.

For other codecs, the FFmpeg Wiki has a nice Encoding Subsection where you’ll find the guides for different codecs.

In this example, we’ll compress an mp4using the libx264 codec (this may take a few minutes):

$ ffmpeg -i big_buck_bunny_480p_20mb.mp4 -vf scale=360:-1 -c:v libx264 -preset veryslow -crf 24 compressed_big_buck_bunny_480p_20mb.mp4

Let’s see what all that means:

ffmpeg – we used FFmpeg

-i– the input file

big_buck_bunny_480p_20mb.mp4 – the path to our input file

-vf scale=360:-1– this is the scale filter ( FFmpeg Wiki – Scaling ). We scaled the video to 360px width, and the -1 mean that we want to preserve the aspect ratio. So FFmpeg will set the height accordingly, without us having to calculate, so the video keeps it’s aspect ratio. For example, if we wanted we could set the height ourselves by using -vf scale=360:240 or any other values.

-c:v libx264– encodes all video streams with libx264 and copies all audio streams

-preset veryslow – to quote the FFmpeg Docs – Preset Subsection, “A preset is a collection of options that will provide a certain encoding speed to compression ratio. A slower preset will provide better compression (compression is quality per filesize). This means that, for example, if you target a certain file size or constant bit rate, you will achieve better quality with a slower preset. Similarly, for constant quality encoding, you will simply save bitrate by choosing a slower preset.”

-crf 24 – this is the Constant Rate Factor. To put it simply, you can set it from 0 to 51, and the lower it is, the higher quality the output video will be. It is generally accepted to set it between 18-24, so that there isn’t much visible loss of quality. You can find the detailed explanation here FFmpeg Docs – Constant Rate Factor (CRF) Subsection

compressed_big_buck_bunny_480p_20mb.mp4 – our output file

Compressing Audio Files

Let’s say you have a 320kbps bitrate Despacito.mp3. You can compress it by lowering the bitrate, as follows:

$ ffmpeg -i Despacito.mp3 -ab 128 compressed_Despacito.mp3

ffmpeg – we used FFmpeg

-i– the input file

Despacito.mp3 – the path to our input file

-ab – audio bitrate, and another form of -b:a ( Docs – Encoding MP3 )

compressed_Despacito.mp3 – our output file

05. Removing Video/Audio Streams

Remove Audio From Media File

If you want to remove audio from a video, then you can remove it using the -an option. We’ll use our big_buck_bunny_480p_20mb.mp4 file for our example:

$ ffmpeg -i big_buck_bunny_480p_20mb.mp4 -an nosound_big_buck_bunny_480p_20mb.mp4

-an – using this option disables the audio recording in the output file

Remove Video from Media File

We’ve mentioned this before, but mentioning this again to keep our article somewhat organized. To remove video from a media file, and leave only the audio stream, we use the -vn option. Again, we’ll use our big_buck_bunny_480p_20mb.mp4 example:

$ ffmpeg -i big_buck_bunny_480p_20mb.mp4 -vn big_buck_bunny_480p_20mb.mp3

-vn – we use this option to disable video recording in the output file

Conclusion

There are many more ways you can manipulate media files using FFmpeg, but doing so is beyond the scope of this tutorial.

Some other common things that you can do include:

  • Converting videos to GIFs
  • Creating videos/gifs from multiple images
  • Extracting frames (images) from video files
  • Cropping videos
  • Adding watermark to videos
  • Adding an album cover ( the image that shows in the media player ) to media files
  • Stream videos ( like streaming to Youtube, Facebook etc)
  • Download live streaming videos ( by using Gstreamer )

Those are just a few other things that you can do with FFmpeg, and it’s capable of doing a whole lot more. No wonder it’s so popular solution, and used by other software.

Hopefully you were able to install FFmpeg 3.x or FFmpeg 4.x on Ubuntu 18.04 or 16.04 and this tutorial has given you a little bit of insight to be able to navigate through the FFmpeg documentation and other FFmpeg guides.

If you have any questions or you believe there is anything incorrect in this article, please let us know and we’ll get back to you ASAP.

If you’re in the market for affordable VPS that are high on resources, then do check out our KVM or Windows VPS plans for some sweet server deals. To give you a sense of our pricing/performance ration, our smallest plan is 1GB RAM at only $9.99/mo.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments