There is no denying that FFmpeg is an incredible tool that can be used to change the file format or quality of an AV file in no time. It can also be used to create gifs, extracting audio, and more. Although there might be plenty of open source tools for converting, tweaking, and editing multimedia such as Handbrake and Audacity, FFmpeg is possibly the best open-source option. In fact, it is a collection of various projects involving multimedia files. FFmpeg is normally used in many media-related projects such as VLC (VideoLAN project).
A common misconception that people have about FFmpeg is that it has anything to do with Moving Picture Experts Group (MPEG group). However, it could not be further from the trust. This guide takes a close look at how you can use FFmpeg with FFmpeg, the command-line tool. It is a small piece of the project and can be used on just about every operating system. Otherwise, you can simply head over to the FFmpeg website to download it. What sets it apart is the fact that it can do almost anything you put your mind to. In order for you to understand how to put FFmpeg to good use, you must first better understand media files.
From a high-level view, media files are broken into containers and streams. It is the streams that include actual AV components like the video or audio which are encoded using codec or any other media encoding. Every codec has properties, weaknesses, and strengths. For instance, a high-quality lossless audio that you might have heard about is the
FLAC codec, whereas,
Vorbis is the go-to option for storing MP3 files as it offers better audio quality. It means that
FLAC-formatted files would have a larger size than
Vorbis audio streams. Besides, they would also sound better. But, neither is better as each option has something to offer. It is the container that acts as the wrapper for the streams. It presents an interface which media players can interact with along as well as tools.
There are some containers which are highly advanced and allow streaming of audio streams and videos inside the single containers. Now, the streams in the container do not necessarily have to be just video or audio as there are different containers that allow different streams such as chapter information and subtitles. Hence, it all depends on the container that is set up. It would provide an abstract view of the media files and skip most of the differences between the containers. It is common for most of them to require certain metadata or streams and place restrictions on the contents or codex.
As you would now have a basic understanding of media files, we can cover basic conversion. Selecting the right containers and formats is what most people struggle with when trying to convert audio to video. FFmpeg does a great job with its default settings to help you navigate conversion. The tool normally selects the right container and codex without requiring any complex configuration. You can enter the following command to convert an MP3 file into an OGG file.
ffmpeg -I input.mp3 output.ogg
The above command would convert the MP3 file (
input.mp3) into an OGG file (
output.ogg) in no time. If we consider the perspective of FFmpeg, converting an MP3 audio stream and wrap the stream into an OGG container is the right format. Thus, you would not have to specify the container types or specify stream as FFmpeg would figure everything out just for you. The following command should work with videos.
ffmpeg -I input.mp4 output.webm
Since WebM is a defined format, FFmpeg would know exactly what to do. It will be able to convert the streams to a valid file in the WebM format. However, it might not always work depending on the container you choose. For example, containers such as Matroska are meant to handle every type of stream you put into them regardless of whether they are valid or not with the following command.
FFmpeg-I input.mp4 output.mkv
The above may lead to a file with a similar codex as input.mp4 which means that it might be able to get the job done.
Select Your Codex
The question is what you should do when you need to use a container such as Matroska which is able to handle every stream out there but still have influence over the Codex in the output. FFmpeg is here to help. You can simply use the
–c flag to select the codex as needed. It will enable you to set the different codex for every stream. For instance, you can set the audio to stream as Vorbis with the following command.
ffmpeg -I input.mp3 –c:a libvorbis output.ogg
Similarly, you can use the following command to change the video to audio stream.
ffmpeg -I input.mp4 –c:v vp9 –c:a libvorbis output.mkv
The above should easily make a Matroska container using a VP9 video stream along with a Vorbis audio stream. When you enter the command
ffmpeg -codecs, it would print out every codex it knows. Thus, the output of the command depends on the FFmpeg version you are using.
Change a Single Stream
Typically, the file you have tends to be partially correct as it would only have a single stream in the incorrect format. Re-encoding the correct stream can be rather time-consuming which is why you need to turn to FFmpeg to help by using the command mentioned below.
ffmpeg -I input.webm –c:v Copy –c:a FLAC output.mkv
The above command would copy the video stream from the WebM input into MKV output. It would also encode the Vorbis audio stream into FLAC. It shows just how powerful the
–c flag can be.
After you have fished reading the post, you will know how to use FFmpeg to convert media files. It is about time that you gave it a try to find out everything it can do for you.