A Quick Guide To Using FFmpeg To Convert Media Files
A Quick Guide To Using FFmpeg To Convert Media Files
By Tim Nugent
There are many open source tools out there for editing, tweaking, and converting
multimedia into exactly what you need. Tools like Audacity or Handbrake are
fantastic, but sometimes you just want to change a file from one format into
another quickly. Enter FFmpeg.
FFmpeg is a collection of different projects for handling multimedia files. It's often
used behind the scenes in many other media-related projects. Despite its name, it
has nothing to do with the Moving Picture Experts Group or the myriad multimedia
formats it has created.
In this article I'll be using FFmpeg through the command-line tool ffmpeg, which is
only a single, small piece of the FFmpeg project. It's available on many different
operating systems and is included in some operating systems by default. It can be
downloaded from the FFmpeg website or through most package managers.
FFmpeg is a powerful tool that can do almost anything you can imagine with
multimedia files. In this article, we are interested in using it to convert files, so we
won't be taking a deep dive into its entire feature set. Before we look at using
FFmpeg, first we need to take a quick look at what a media file exactly is.
Media files
At a very high-level view, a media file is broken up into a container and its streams.
The streams include the actual AV components, such as a movie's audio or video,
and are encoded using a particular media encoding, or codec. Each codec has its
own properties, strengths, and weaknesses. For example, the FLAC codec is good
for high-quality lossless audio, whereas Vorbis is designed to compete with MP3 in
file size while offering better audio quality. This means a FLAC-formatted file will be
much larger than a Vorbis audio stream but should sound better. Neither is
inherently better than the other, as each is trying to do different things.
The container is the wrapper for the streams. It presents a single interface that
media players and tools can interact with. Some containers are highly advanced and
allow for any sort of stream, including multiple video and audio streams inside a
single container. The streams in a container don't have to be just audio or video
though. Different containers will allow for different streams, e.g., subtitles, chapter
information, or other metadata. It all depends on what the container is set to allow.
This is an abstract representation of media files and skips over a lot of the
differences between containers. Many require certain streams and metadata or put
restrictions on the codecs or contents allowed. This explanation is enough to get
you through this article. To learn more, click on the links above.
Be aware that video and audio encoding can take a very long time to run. You
should be prepared to settle in for a while when you use FFmpeg.
Basic conversion
The thing that trips up most people when it comes to converting audio and video is
selecting the correct formats and containers. Luckily, FFmpeg is pretty clever with
its default settings. Usually it automatically selects the correct codecs and
container without any complex configuration.
For example, say you have an MP3 file and want it converted into an OGG file:
This command takes an MP3 file called input.mp3 and converts it into an OGG file
called output.ogg. From FFmpeg's point of view, this means converting the MP3
audio stream into a Vorbis audio stream and wrapping this stream into an OGG
container. You didn't have to specify stream or container types, because FFmpeg
figured it out for you.
Depending on your container of choice, this won't always work. For instance,
containers like Matroska are designed to handle almost any stream you care to put
in them, whether they're valid or not. This means the command:
This flag lets you set the different codec to use for each stream. For example, to set
the audio stream to be Vorbis, you would use the following command:
The same can be done to change the video as well as the audio stream:
This will make a Matroska container with a VP9 video stream and a Vorbis audio
stream, essentially the same as the WebM we made earlier.
The command ffmpeg -codecs will print every codec FFmpeg knows about. The
output of this command will change depending on the version of FFmpeg you have
installed.
More often than you'd like, the file you have is partially correct with only a single
stream in the wrong format. It can be very time consuming to re-encode the correct
stream. FFmpeg can help with this situation:
This command copies the video stream from input.webm into output.mkv and
encodes the Vorbis audio stream into a FLAC. The -c flag is really powerful.
Changing a container
The prior example can be applied to both the audio and video streams, allowing you
to convert from one container format to another without having to do any
additional stream encoding:
Now that we have a handle on the codecs, the next question is: How do we set the
quality of each stream?
The simplest method is to change the bitrate, which may or may not result in a
different quality. Humans' ability to see and hear isn't as clean and clear cut as we'd
like to think. Sometimes changing bitrates makes a huge difference to the
subjective quality. Other times it might do nothing but change the file size.
Sometimes it's very difficult to tell what will happen without trying it out.
To set the bitrate of each stream, you use the -b flag, which works in a similar
fashion to the -c flag, except instead of codec options you set a bitrate.
For example, to change the bitrate of the video, you would use it like this:
This will copy the audio (-c:a copy) from input.webm and convert the video to a
VP9 codec (-c:v vp9) with a bit rate of 1M/s (-b:v), all bundled up in a Matroska
container (output.mkv).
Another way we can impact quality is to adjust the frame rate of the video using the
-r option:
You can also adjust the dimensions of your video using FFmpeg. The simplest way is
to use a predetermined video size:
This modifies the video to 1280x720 in the output, but you can set the width and
height manually if you want:
This produces the exact same output as the earlier command. If you want to set
custom sizes in FFmpeg, please remember that the width parameter (1280) comes
before height (720).
Adjusting frame rate and bitrate are two crude but effective techniques for
affecting media quality. Setting these values very high cannot improve the quality
of an existing source if its quality is already low.
Changing these settings is most effective for quickly reducing a high-quality stream
to make a smaller file size. Adjusting the size of your video can't improve the quality,
but can make it fit better onto a tablet instead of your TV. Changing the size of a
640x480 video to 4K will not improve it.
Changing the quality of your files is a very subjective matter, which means there is
no one way that will work every time. The best method is to make some changes
and test whether it looks or sounds better to you.
This will copy the video and audio streams (-c:av copy) but will trim the video. The
-t option sets the cut duration to be 10 seconds and the -ss option sets the start
point of the video for trimming, in this case at one minute (00:01:00). You can be
more precise than just hours, minutes, and seconds, going down to milliseconds if
needed.
Sometimes you don't really care about the video, you just want the audio. Luckily
this is very straightforward in FFmpeg with the -vn flag:
This command extracts only the audio from the input, encodes it as Vorbis, and
saves it into audio_only.ogg. Now you have an isolated audio stream. You can also
use the -an and -sn flags in the same manner to strip out audio and subtitle
streams.
Recently, animated GIFs (with a hard g because I am not a monster) have made a
comeback. Personally I think GIF is the worst format you could choose for video. It
has terrible compression quality and size; has very hard limits around colors, frame
rates, and container metadata; and can't support audio. Still, it's quite popular. So,
how can you make a video clip into an animated GIF?
Using the -an flag, similar to what we did above, is better than creating an animated
GIF if you'd like to make a video without audio, but there are plenty of places that
support GIFs that won't support a different video format. For all of those:
Other tools
While FFmpeg is the go-to tool for most AV tasks, it isn't perfect for everything.
There are some tools that, used in conjunction with FFmpeg, can make everything a
little bit easier.
It can be very easy to upload something to YouTube, lose the original source video,
and have only the YouTube version remaining. What is the easiest way to retrieve a
copy of your YouTube videos?
Youtube-dl is a nifty little tool you can use to grab videos from YouTube and several
other video-streaming services. It's super straightforward to use:
youtube-dl https://www.youtube.com/watch?v=2m5nW9CQLJ0
This command downloads the video at the URL indicated and saves it locally.
Youtube-dl has several options for controlling the quality and format of the
downloaded video, but I find it easier to use the command above. It downloads the
highest quality audio and video into a single file, then I use FFmpeg to convert them
into the format I want.
Sometimes all you need to know is what's inside the media container. While there
are several tools that can do this, my preferred one is MediaInfo. MediaInfo shows
all the information inside a media container, for each of the different streams, as
well as the metadata for the input file. This gives you everything you might want to
know—and probably a bunch of stuff you didn't.
Running the command mediainfo inputFile.mkv spits out a list of information
about the input file in a human-readable form.
And more...
This is just scratching the surface of what FFmpeg can do. Luckily the
documentation for FFmpeg and the project's other tools is very good and worth
checking out. It'll teach you all about the many different tricks this dog can do.
If you are after a tool with a graphical interface for converting multimedia,
Handbrake is an exceptionally good one available on Linux, Mac OS X, and
Windows. Handbrake uses FFmpeg (among other tools) under the hood.
Tim Nugent
Tim Nugent pretends to be a mobile app developer, game
designer, and PhD student, and now he’s recently branched into
pretending to be an author.
More about me
6 Comments
These comments are closed, however you can Register or Login to post a
comment on another article.
I was reading an article that suggested a .wav file converted to .flac and then
back to .wav did not sound the same as the original. Huh, I thought - that
seems pretty weird.
Strangely enough, using flac(1) to convert the 96/24 .wav to .flac and then
ffmpeg to convert the .flac back to .wav truncated the sample size to 16 bits...
Not sure why that was. Made me also think that if replay gain was stored in
the tags (not sure if this happens but I think it might) but lost in the
conversion, the result could sound different due to loudness level.
It works to display the list of available formats even though it is not a youtube
site.
Problem:
I do not know the directory where the video is located. It is not clearly shown
on source code. Trying to recall a tool to find it in the source code.
Also, what replaces [youtube] on command line for non youtube site Is it
gonna be [sitename?
Cheers.
The -F flag may not be what you are after however as that lists the
available formats, not videos.
If you know the URL of the video you are after then just going
"youtube-dl http://sitename/video" will work nine times out of ten.
Tim
Related Content