Quick guide into FFmpeg

Joel Oliveira
Joel Oliveira
Jan 15 2021
Posted in Engineering & Technology

Using FFmpeg to convert media from the command line

Quick guide into FFmpeg

There are many tools out there to help you convert, edit and manipulate video and audio files. In 2021, a quick lookup online will unveil several free open source projects, websites or cloud based solutions. Depending on what you want to achieve, you will probably find your answer without ever reading or hearing about FFmpeg. But you would be surprised how many of these projects, media companies and services are actually using FFmpeg. It is still the best option, when you just want to quickly manipulate media files from the command line.

What is FFmpeg

FFmpeg (aka Fast Forward mpeg) is a free and open source multimedia framework that ships with several libraries and it is divided in 3 main tools:

ffmpeg

The command line tool to convert media files between formats.

ffplay

A multimedia player using the FFmpeg libraries.

ffprobe

A multimedia stream analyser tool to extract meta data from media files.

It has been around for 2 decades and its libraries are a core part of products like VLC and it is known to have been included in core processing workflows for YouTube and iTunes.

In this post, we will focus on how to use FFmpeg, in macOS, from the command line. You will learn how to convert video and audio files into different formats, extract audio from video files, transform images into videos or generate GIFs, among other things.

Install FFmpeg

The easiest way to install FFmpeg on macOS and use it from the command line, would be to use Homebrew. If you plan to use it in a real world application, there also many other packages that can help you include in your Java, Python or Node.js project. In this post, we will focus on running FFmpeg in the terminal. If you have Homebrew, go ahead and run the following command:

brew install ffmpeg

If everything went OK, running the following command should display some information:

ffmpeg -version

You can also run the following command to get a list of all available formats:

ffmpeg -formats

And similarly, you can run this command if you want to get a list of all available codecs:

ffmpeg -codecs

There are many other options in FFmpeg and if you feel curious you can always take a look at their documentation. In this post we will simply scratch the surface of what FFmpeg can do and focus on some quick examples that many developers and end-users alike, will eventually need at some point.

Get information from a file

Now that you have FFmpeg installed, it's time to start playing around. To get information about a specific file saved in your computer, you can run the following command:

ffmpeg -i my_input.mov

This will output some useful information about your file. Note the option -i, it stands for input and you will use it to provide one or more input files that you want to convert.

Convert WAV to MP3

Now let's actually convert one audio format to another. WAV files are lossless and uncompressed and will preserve the quality of the original recording. For these reasons, they are usually considerably big files. However, if you're not an audiophile and you are concerned about storage space, you will want to use a lossy compression format like MP3. FFmpeg is capable of detecting audio streams and codecs automatically and generate the perfect conversion by using a simple command:

ffmpeg -i my_input.wav my_output.mp3

Changing Audio Channel and Bit & Sample Rate

FFmpeg is really powerful and you can manipulate your output's codecs, bit rate, sampling rate, etc. This is where things can get complex and it will be useful if you have some understanding of multimedia containers and streams. Let's look at an example where we convert the same file, but this time, we will use only one audio channel (mono), set its bit rate to 64Kbps and sampling rate to 22050Hz:

ffmpeg -i my_input.wav -ac 1 -ab 64000 -ar 22050 my_output.mp3

As you can see, by passing a variety of options in your commands, you can manipulate even further your output file.

Extract an Audio Fragment

FFmpeg also comes in handy when you want to extract a certain fragment from your input file into another file. This can be easily done by using the following command:

ffmpeg -i my_input.mp3 -t 30 my_output.mp3

With the command above you can easily extract 30 seconds of audio from your original file. Pretty much the same way, you can also extract a sample of your audio file with an offset (using HH:MM:SS format):

ffmpeg -i my_input.mp3 -ss 0:01:00 -t 0:00:30 my_output.mp3

This example would generate a 30 seconds audio file but this time only after the first minute.

Extract Audio from Video

Here's another great example of what FFmpeg can quickly accomplish. Let's imagine you want to extract the audio stream from a video file. In the process, we will tell FFmpeg that the output should be MP3 (-f mp3), that it should encode it at 192Kbps (-ab 192000) and that it should remove the video stream (-vn), as follows:

ffmpeg -i my_input.mp4 -f mp3 -ab 192000 -vn my_output.mp3

Add Audio to Video

Let's now look into another useful example. Let's generate a video file and include an audio stream from a different file:

ffmpeg -i my_video_input.mov -i my_audio_input.wav -map 0:v -map 1:a -c copy my_output.mov

As you can see, we have 2 input files in this example. We also map the first one to the video stream (-map 0:v) and the second one to the audio stream (-map 1:a) while maintaining the same codecs from both files (-c copy).

Crop Video File

FFmpeg is also great when you want to crop a video. This can be achieved using the following command:

ffmpeg -i my_input.mp4 -vf "crop=out_w-20:out_h-20" my_output.mp4

In this example we are applying filter to crop our video stream. In this case, we removed 20 pixels from the width and height. The crop filter supports the following format:

  • out_w is the width of the output
  • out_h is the height of the output
  • x and y specify the top left corner axis

Extract a Video Fragment

Just like the example for audio files, FFmpeg is also capable of extracting fragments from video files. The following command would generate a 30 seconds video file from the original source:

ffmpeg -i my_input.mp4 -t 0:00:30 my_output.mp4

Convert Video into Images

Another great example of how useful FFmpeg can be. In the following example, we will basically generate images from a video file:

ffmpeg -i my_input.mp4 image%d.jpg

This would create several images (named image1.jpg, image2.jpg, etc…) from a given video.

Convert Images into Video

You can also create a video from multiple images. In the following example, all the images in the current directory (named image1.jpg, image2.jpg, etc…) would be converted into a video sequence:

ffmpeg -f image2 -i image%d.jpg my_output.mp4

Convert Video into GIF

FFmpeg can also easily create a GIF from a video file, as follows:

ffmpeg -i my_input.mp4 my_output.gif

Add Subtitles to a Video

If you have subtitles for a movie, it is possible to use FFmpeg to insert them into your video file:

ffmpeg -i my_input.mp4 -i subtitles.srt -c copy -c:s mov_text my_output.mp4

Add Image Overlay to Video

Finally, we will leave you with yet another great example of what FFmpeg can do. In this example, we will generate a video file with a transparent PNG overlay. By using a filter, we will position it 25px from the top left corner and use yuv420p as the pixel format, while also copying the same audio codec (-c:a copy):

ffmpeg -i my_video_input.mp4 -i my_image_input.png -filter_complex "[0:v][1:v] overlay=25:25" -pix_fmt yuv420p -c:a copy my_output.mp4

Cool, Right?

This is just the some of the things FFmpeg is capable of. From the command line, it can be a very useful utility to quickly convert video and audio files. In real world applications, it is powering some of the world's biggest media companies.

In Notificare, we are also using it to perform a myriad of operations. As always, if you have any questions, suggestions or corrections to this post, feel free to drop a message in our Support Channel.

Keep up-to-date with the latest news