Comprehensive Guide To Using FFmpeg To Convert Media Files
Comprehensive Guide To Using FFmpeg To Convert Media Files
L — LINUX
Comprehensive Guide to
Using FFmpeg to Convert
Media Files
by EdXD · Updated May 18, 2022 · 1 share · No comments · 12 minute read
FFmpeg is one of those modern marvels of open source software. It is a suite of libraries and x
smaller programs to handle video and audio files primarily.
Free Video Editor Tool
It works with images and other multimedia files such as video streaming formats. It has lots of x
uses like video transcoding, video editing, video scaling, video cropping or other video
manipulation work.
At its heart FFmpeg is a command line tool used with the ffmpeg command.
It has a basic simple video player and ability to probe video media information for analysis.
FFmpeg is also included in the workflow of other software like the popular video player VLC. Enterprise
companies like YouTube use it in their core processing when ingesting video uploads.
Overall FFmpeg can play, record, convert, and stream audio and video. It includes libavcodec –
the leading audio/video codec library.
In this tutorial we’ll install FFmpeg and learn how to use some its most popular
features through practical examples and detailed explanations.
TABLE OF CONTENTS
1 Install FFmpeg
Install FFmpeg on Linux
Installing FFmpeg on Windows
Installing FFmpeg on MacOS
Check if FFmpeg is Installed
## Install FFmpeg
FFmpeg officially supports Linux, Windows and MacOS.
While it’s not the absolute latest version of FFmpeg that one can install, it is a version not that
far behind.
FFmpeg itself is not big and it will install quickly like in the demo.
If you don’t have all the additional libraries and dependencies that FFmpeg requires, it will take
a little bit longer than our demonstration to finish installing everything. Depending on your
internet connection speed and processing power of your computer it can take up to two
minutes or more.
Don’t worry if it takes a few minutes, the amount of additional libraries FFmpeg uses are a lot.
x
Free Video Editor Tool
x
The way FFmpeg works is that it doesn’t try to reinvent everything, it uses already established
libraries and it applies them into its workflow. That way the developers are focused on the core
of the product instead of redeveloping everything themselves.
$ ffmpeg -version
x
Free Video Editor Tool
x
This tells us information about our installation. What version, in our example it is the 4.4-6
Ubuntu maintained version. We see all the libraries that are enabled under configuration.
Finally we can see the codecs that come integrated with it.
A codec is a computer program that compresses or decompresses digital video or audio data. Codecs
are necessary for encoding and decoding (playing) audio and video. Multiple codecs exist, and each
codec has its own advantages and disadvantages. The reason there are so many codecs is that no single
codec can do everything.
Now that we installed FFmpeg on our system there’s a myriad of work we can do with
multimedia files. We will learn of a few of them below.
Videos are usually saved in a container format. A container format is a file format that allows
multiple data streams to be embedded on them.
x
Free Video Editor Tool
Ad
Animated Video Templates x
Create stunning animation videos in minutes without
learning curve.
Doratoon
OPEN
To make an illustration of what this means let’s go over what videos are.
Videos are still frame pictures grouped together successively to create the illusion of motion.
In a one minute video that is 1440 frames, or pictures to easily illustrate. So the container keeps
these picture data streams and displays it at a set frequency. In a short explanation that is how
digital videos work.
The extension of a video file usually corresponds to a container format, although not always.
Popular video container formats used today are:
These are some of the most popular container formats used on the web today.
It’s important to note that while any file can have any of these extensions used, it doesn’t
necessarily mean that the video container of the file is of that format.
For example, I can change the extension of a party.mov video file to party.mp4 but that
doesn’t mean that the container got changed. The container of this party.mp4 will still be the
QuickTime File Format.
In order to change the container of the file it needs a process called transmuxing, it’s something
that FFmpeg can do. x
Free Video Editor Tool
## Encoding, Transcoding, and Transmuxing x
Before we proceed to examples of how to use FFmpeg we need to understand the difference
between encoding, transcoding and transmuxing. All three of them are formatting a data
stream into a container so it can be a playable video but they differ slightly.
The important thing to remember about encoding is that it’s being compressed from a raw
source.
Since usually raw video data contains so much information the process of encoding takes
longer than transcoding or transmuxing and it’s a CPU intensive task.
Transcoding will also compress data into a video container file ready to be playable. The source,
though, was a video that was already encoded. Since transcoding is changing the format
completely, it is also a time consuming process.
The process is a lot quicker and it’s not CPU intensive. What it does is just reassigns the video
data into the container file’s format, but the video data stays the same.
To transmux a file with FFmpeg we can run a command like the following:
This is one of the most simple tasks we can do with FFmpeg. Let’s explain the above command.
First we ran the command ffmpeg with the -i option followed by indicating the video file to
use.
x
Free Video Editor Tool
x
After inputting the video file name that we are going to use, with the -c option we told it to
copy into a file name bunny.mkv.
The -c option is an important one on FFmpeg because it corresponds to the codec the video
should use.
In this case we didn’t want to transcode into another video format with different video settings,
so instead of inputting our encoding configuration we just simply told it to copy.
All that did was exchange the container wrappers while keeping the same video encode.
We will save quality and a lot of time by just changing the containers if there is no need to
transcode into different settings.
One such format is the M3U8 file format. M3U8 is a playlist format that aggregates thousands
of smaller chunks of MPEG Transport Stream .ts files.
.ts is a popular format for streaming because it’s a long video file cut into small chunks of a few
seconds, while the video is playing the browser is downloading these chunks of small video files
and buffering them in the memory.
This technique to stream video saves video providers over the web a lot of bandwidth as just
the played portion gets downloaded to the user and not the whole video file. x
Free Video Editor Tool
For example, a 2 hour movie does not need to download the whole file so the user can play, it
will only download the 5 minutes the user watched before closing the web page.
But because this format has literally thousands of chunks of a few seconds of time span, it’s x
hard to download. FFmpeg can help us here.
$ ffmpeg -i https://multiplatform-
f.akamaihd.net/i/multi/will/bunny/big_buck_bunny_,640x360_400,640x360_7
-c copy downloadbunny.mp4
As our aim here is to simply download the file, we again are only going to remux the video.
We run the ffmpeg command this time with the -i option indicating the web URL of the
.m3u8 playlist file. For the codec portion -c we will again copy as to not transcode, then we
give it a file name to save to, in our example downloadbunny.mp4.
In less than 2 minutes we were able to download the complete 10 minute that was hosted on
the .m3u8 playlist file.
This type of download and remux combination can be done with other streaming formats also
like MPEG-DASH and others.
Ad
Animated Video Templates
Create stunning animation videos in minutes without
learning curve.
Doratoon
OPEN x
Free Video Editor Tool
x
This will not work when the HLS stream is encrypted on the server side because we wouldn’t
have the decryption key.
$ ffmpeg -i "https://multiplatform-
f.akamaihd.net/i/multi/will/bunny/big_buck_bunny_,640x360_400,640x360_7
-c:s srt output_subs.srt
Here we are running ffmpeg with the same m3u8 playlist file URL as the example before that
we indicated with the -i option.
We will use another of FFmpeg’s not so well known options, the -c:s option. That option will
tell it to download any subtitles that it finds and to write it on our computer using the srt
format of subtitles.
Then we give it a name to the subtitle file it will create, in our example output_subs.srt.
The role of ffprobe is to probe for information about a video file. It’s used to extract
information from multimedia streams and print it in human- and machine-readable fashion.
x
Free Video Editor Tool
x
Doratoon Open
One such useful piece of information is the amount of frames a video has. We can do that easily
with the following command.
This option omits more information and gives us only the frame count we are looking for.
The -select_streams v:0 tells it to start with the first video stream in the container. While
most video files usually only have one video stream in it, there could be more than one.
x
Free Video Editor Tool
-count_frames is telling it to count the number of frames per stream.
-show_entries stream=nb_read_frames is to tell it to show us only entries for frames. x
Finally -of csv=p=0 tells it to hide descriptions and only output the value.
From the previous HLS stream video we had downloaded, now we know that it contains 17876
frames.
Depending on the CPU power of your computer this probe can take a little while to complete as
it counts the frames.
ffprobe is an excellent utility and we should always keep it in mind when working with video
files.
In this example we are getting our big buck bunny video which is 480 x 270 (width x height).
That is a very small resolution for our enjoyment so we use ffmpeg with the -i option to pick
our video.
This time we will use the -vf option to indicate that we want to scale the video to a resolution
of =1920x1080 and we are flagging it to use the lanczos algorithm.
Animated Video
Animated Video
x
Templates Free VideoTemplates
Editor Tool
Doratoon Doratoon
x
FFmpeg has three algorithms to choose from when scaling:
1. Bilinear
2. Bicubic
3. Lanczos
While Lanczos won’t be the sharpest, it provides a combination of sharpness and smoothness to
make the footage look better.
After a few minutes of transcoding it will output us a new file, upscaled_1080p.mp4 in our
example.
Here we take the 1920 x 1080 (width x height) video that we just upscaled and are downscaling
it to a 1280 x 720 resolution using the same settings.
Ad
Animated Video Templates
Create stunning animation videos in minutes without
learning curve.
Doratoon
OPEN
x
Free Video Editor Tool
x
In the previous video we transcoded a video and we didn’t even know it.
In that example we didn’t indicate any codec settings but because it was an up and down scale
we had to transcode, it couldn’t simply copy like we had been doing before. In the previous
example FFmpeg simply used the default transcoding settings.
FFmpeg has a plethora of codecs it can use. To see a list of them you can run the command.
$ ffmpeg -codecs
Not all of them can encode, so to get a list of all the encoders on our system we can run the
command.
$ ffmpeg -encoders
One of the most popular encoders for videos is the h264 encoder .
The h264 encoder is compatible with web browsers, meaning videos encoded with it can be
played embedded on a website.
There are some encoders that are not compatible with web browsers like the h265 encoder.
Some phones use the h265 encoder when using the smartphone camera for example,
h265 is more modern and has better compression than h264, so smartphone providers
use h265 so their users save on storage space. But h265 is not web compatible so if we
x
upload a video that was encoded with h265 it won’t play on a website.
Free Video Editor Tool
So what do sites like YouTube and Facebook do when users upload a h265 encoded x
video to a website? They transcode the video on their servers and then serve a h264
encoded version.
YouTube and Facebook, Instagram and TikTok, practically everybody on the internet use
FFmpeg on their servers to process the videos.
Here we are using ffmpeg with the -i option, as we have learned by now, to indicate the file
we are going to be working with.
-map 0 options tells it to perform the transcode on all available video streams in the container.
The default is only 1 so that’s why we had the need to configure this. 0 means “all streams” in
this case.
As we know, the -c option indicates the codec and :v portion indicates that we are only
talking about the video codec and not the audio.
Remember when we were just copying transmuxing and not transcoding, we would just tell it a
single -c option as we were merely copying the audio and video, but this time we are
indicating the video only with -c:v libx264 , the libx264 tells it to use the h264 encoder.
The other options -crf 18 consist of the quality of the output we want, this is a huge topic on
itself out of the scope of this tutorial. The quality settings options of FFmpeg are immense.
The -vf format=yuv420p refers to the pixel format and yuv420p is one of the most web
compatible pixel formats and safe to use. Lastly again we are using the -c option but this time
with the :a to indicate the audio codec that in this example we are indicating it to just copy.
At the end of the command we have the title of the output video, downscaled_720p.mp4.
Running this command will make a totally new transcoded version of bigbuckbunny.mp4, that
in this example is supposedly a h265 encoded video that we are reencoding to h264 format to
make it web compatible.
x
Free Video Editor Tool
x
When working with codecs we can use -c:v for video, -c:a for audio, and -c:s for subtitles.
This is just one simple example of transcoding using FFmpeg. Transcoding goes in hand with
changing the audio and video settings of which there are literally thousands of options.
x
Free Video Editor Tool
It’s pretty simple to extract audio from a video. Notice that we are only copying the audio x
portion with -c:a copy. The -vn tells it to disable the processing of the video stream.
So here FFmpeg is just copying the audio stream, omitting the video stream and saving it to an
audio only file format, the aac container with the .aac extension.
This time we are running ffmpeg but before telling which file to use with the -i option, we will
indicate the start time and duration of our output video.
The -ss 00:00:00 portion indicates at what point in the video our cut will start in the format of
hours, minutes and seconds.
The -t 00:00:10 portion indicates the duration our video will have. x
Free Video Editor Tool
In our example we are cutting from the start of the video and lasting for only 10 seconds.
The rest of the options are as we know them, -i indicates which video and -c copy indicates x
that we are using the copy codec with buncute.mp4 being the final output file.
## Conclusion
FFmpeg is a monster of an open source free software. Not only its capabilities are huge in
quantity but the quality of its job is par with none.
Websites like YouTube and Facebook wouldn’t exist without it. It’s one of those rare pieces of
software that command respect at all levels, and for Linux users it’s a great tool to learn to use.
TAGS: Ffmpeg
1 SHARE 1 TWEET
EdXD
Subscribe Login
Name*
POST COMMENT
Email
Website
x
0 COMMENTS
Free Video Editor Tool