ffmpeg is a CLI (command line based) utility for processing media files. It is a framework with a multitude of features and, because of it’s open source license, it is the base for many widespread apps such as VLC, YouTube, iTunes and many more. A number of Linux video editors use ffmpeg underneath the GUI.
What I love even more about ffmpeg is that it can be used on it’s own to accomplish many processing tasks in a very simple manner (with one or two commands). However, it’s a very powerful program that can be used in more complex ways and even replace an editing workflow.
In this ffmpeg tutorial, I’ll show you how to install ffmpeg and I’ll be covering it’s different uses. I’ll even get into some more complex features.
I’ll be explaining everything in detail so you can understand what is going on even if you are new to the Linux world.
I’ll be using Ubuntu 18.04, but these commands should work for other distributions too.
Note:Although I’ve researched ffmpeg thoroughly and tested everything I’ll mention in this article, I don’t edit videos (or other media) on a daily basis. If you have any suggestions, tips or corrections, let me know in the comments!
Let’s get for it!
Installing ffmpeg on Ubuntu & other Linux
Installing ffmpeg is a straightforward process. It’s a popular application and is available in most of the Linux distribution through the package manager.
Install ffmpeg in Ubuntu
In Ubuntu, ffmpeg is available in the Universe repository so make sure to enable universe repository. and then update the packages list and install ffmpeg. You can do all these using the below commands in a terminal:
That should be it! You can verify the install with simply typing:
This should display some text describing your ffmpeg configuration (including version).
As you can see from the screenshot above, the installed version is 3.4.4. However, the latest version of ffmpeg at the moment of writing this article is 4.1. To install any 4.x version, you’ll have to use the ffmpeg ppa. Click to expand the next section to see the details of ffmpeg installation via PPA.
Install the latest ffmpeeg in Ubuntu-based distros with PPA
Install ffmpeg in Arch-based distributions
To install ffmpeg in Arch-based distributions, use the following command:
You have to keep in mind that all file options apply only to the file that follows them (you’ll have to write them again for the next file).
Any file not preceded by -i is considered an output file. Ffmpeg uses as many inputs and outputs as you provide. You can also use the same name for both an input and an output file, but you’ll have to add the -y tag before the output file name.
You shouldn’t mix inputs and outputs; specify input files first, then specify output files.
1. Get media file information
The first use of ffmpeg is displaying information about a media file. This can be done by not entering any output files. Simply type:
ffmpeg -i file_name
This works for both audio and video files:
ffmpeg -i video_file.mp4
ffmpeg -i audio_file.mp3
Although useful, this command will display way too much information that isn’t relevant to your file (information about ffmpeg). To omit that, add the -hide_banner flag:
As you can see, the command now outputs only details concerning the media file you specified (encoder, streams etc.).
2. Convert Media Files
Another very useful way to use ffmpeg is for hassle-free conversion between different media formats. You only have to specify input and output files, since ffmpeg will get the required format from the file extensions. This works for video-to-video and audio-to-audio conversions.
Additionally, you could specify codecs you want to use, adding -c:a (for audio) and -c:v (for video) followed by the name of the codecs, or copy if you want to use the same codecs as the original file:
Note:this might make the file extension rather deceiving, so be careful.
3. Extract Audio from Video
To extract audio from a video file, you do a simple conversion and add the -vn flag:
ffmpeg -i video.mp4 -vn audio.mp3
Note that this command will utilize the bit rate of the original file. It’s a good idea to set it yourself; you can use -ab (audio bit rate):
ffmpeg -i video.mp4 -vn -ab 128k audio.mp3
Some common bit rates are 96k, 128k, 192k, 256k, 320k (highest bitrate supported by mp3).
Other common options are -ar (audio frequency: 22050, 441000, 48000), -ac (number of audio channels), -f (audio format, although normally automatically detected). -ab can also be replaced by -b:a. For example:
Similar to the last task, to accomplish this we’ll add a simple tag: -an (instead of -vn).
ffmpeg -i video_input.mp4 -an -video_output.mp4
Note:The -an tag will render all audio options for that output file useless, since there will be no audio in the result of the operation.
5. Extract Images from Video
This might come in handy for a lot of people. For example, you might have a series of images (e.g. a slideshow) and you want to get all images out of that. Simply enter:
ffmpeg -i video.mp4 -r 1 -f image2 image-%3d.png
Let’s take that apart for a second.
-r specifies the frame rate (how many frames are extracted into images in one second, default: 25), -f specifies the output format (image2 actually stands for image2 sequence.
The last parameter (the output file) has a somewhat interesting naming: it uses %3d at the end. This simply numbers your images with 3 digits (000, 001, etc.). You can also use %2d (2 digit format) or even %4d (4 digit format) if you want. Feel free to experiment and use whatever works for you!
Note:There is also a way to turn a set of images into a video/slideshow. I’ll cover that in the Advanced Usage section.
6. Change Video Resolution or Aspect Ratio
Another simple task for ffmpeg. All you have to do to resize a video is specify a new resolution after the -s flag:
Note:Once again, another slightly more powerful method will be mentioned in the Advanced Usage section.
7. Add Poster Image to Audio
This is a great way to turn audio into video, using a single photo (such as a album cover) for the audio. This is a very useful feature when you want to upload audio files to sites that don’t allow anything other than video/images (YouTube and Facebook are two examples of such sites).
Note:Video bit rates are different from audio bit rates (much higher values).
You could set the -crf flag (Constant Rate Factor). A lower crf means a higher bit rate. Also, using libx264 as video codec helps. Here’s a very efficient size reduction with very small reduction in quality:
As you can see, this works for both video and audio files. Both of the commands above do the same thing: save the first 5 seconds of the input file to the output file. I have used to different ways of inputting the duration: a single number (number of seconds) and HH:MM:SS (hours, minutes, seconds). The second version acts more like an end time.
You can go even further by specifying a start time with -ss, and even an end time with -to:
The syntax is quite simple. I specified -t 00:00:30 as duration for the first part (the first part will consist of the first 30 seconds of the original video). Next, I specified that I’d like the rest to be part of a second video (starting from the end of the last part, 00:00:30).
This can be done for as many parts as you want! Have fun playing with this feature, since it is very powerful. Keep in mind that it works with audio too.
2. Concatenate Media Files
ffmpeg can also do the opposite process: putting multiple parts together.
To do so, you’ll have to create a new text file and start editing it using your preferred editor.
Since I like using the terminal, I’ll use touch and vim. It doesn’t matter what you call this file. I’ll call mine join.txt and create the file using touch:
Now, I’ll edit it using vim:
You can use whatever tools you want (nano, gedit, etc.), this is just what it’s the easiest for me to use.
Here, enter the full paths to all the files you wish to join (they will be joined in the order you write them here), one per line. Make sure they have the same extension (e.g. mp4). Here’s mine:
Save the file you just edited. This method works for any audio or video files.
Now enter the following:
ffmpeg -f concat -i join.txt output.mp4
Note:My output file is output.mp4, but that’s because all my input files have the mp4 extension.
This should join all the files you wrote down in join.txt (or however you named that file) into one single output file.
3. Join Images into a Video
This is how you can put together a slideshow or anything similar. I’ll show you how to add audio too.
The first thing I recommend doing is making sure the photos you want to put together are located in the same directory. I’ll put mine in a directory called my_photos. The extensions recommended for the pictures are .png and .jpg. Whichever you choose, make sure all images have the same extension (otherwise, using ffmpeg might have unintended effects). You could simply convert the .png files to .jpg or vice-versa (see Basic Usage).
The format (-f) of our conversion should be image2pipe. For the input, you’ll have to provide a hyphen (–). What image2pipe allows you to do is pipe (using | in the terminal)the results of a command such as cat into ffmpeg instead of inputting all names one by one. That’s exactly what I’ll do. For this to work, we’ll also have to mention that we want the video codecs to be copied (-c:v copy) (to properly use the images):
If you play this file, you might think only some pictures were added to the slideshow. Actually, all of your pictures have been added, but ffmpeg cycles through them as fast as possible. This means 1 frame per photo; ffmpeg (by default) runs at around 23 fps.
To change that, you’ll have to specify the frame rate (-framerate) you want:
In my example, I set the frame rate to 1, that means that each frame (that also means each images) appears for 1 second.
To add audio, such as a background song, we’ll have to specify the audio file as input file (-i audo_file) and copy the audio codecs (-c:a copy). For the codecs, you could copy both audio and video codecs at the same time with -c copy. Make sure you set the codecs right before specifying the output file. You could also set the frame rate so that all of your pictures synchronize with the duration of the audio you want to use. To get that, divide the number of pictures by the duration of the audio (in seconds). For my example, I have a 22 seconds long audio file and 9 images. 9 / 22 is approximately 0.40, so that’s what I’ll use for my frame rate:
Screencasting with ffmpeg is not a hard job. The format (-f) you need to use is x11grab. This will capture your XSERVER. As input you’ll have to specify your screen display number (main screen should normally be 0:0). However, this will only capture a top-left portion of the screen. You should add a screen size (-s). Mine is 1920×1080. The screen size should be mentioned before the input:
ffmpeg -f x11grab -s 1920x1080 -i :0.0 output.mp4
Press q or CTRL+C at any time to stop the screencast.
Bonus trick: You can make the size of the output file fullscreen with inputting this after for the size (instead of 1920×1080 or any other set resolution):
Recording input from your webcam (or another device, such an usb camera) is even easier. In Linux, devices are stores in /dev as /dev/video0, /dev/video1, etc.:
ffmpeg -i /dev/video0 output.mkv
Again, q or CTRL+C to stop recording.
6. Record Your Audio
Linux handles audio mainly through ALSA and pulseaudio. ffmpeg can record both, but I’ll cover pulseaudio, since Debian-based distributions include it by default. The syntax is a little different for the two methods.
For pulseaudio, you have to force (-f) alsa and specify default as input (-i default):
ffmpeg -f alsa -i default output.mp3
Note:In your distribution sound settings, make sure the default recording device is the one you want to record.
I play guitar quite a lot and I have an audio interface so I can record it. I had a pleasant surprise when ffmpeg recorded my guitar with no problems.
Bonus Tips on Recording
For any type of recording you can also specify codecs, of course (already covered). You can choose a specific frame rate (-r). You can combine recording audio with webcam/screen recording too!
Note: ffmpeg records in small pieces, so a really small recording might not get saved. I recommend recording just a little longer and cutting it afterwards (if you only want a few seconds), just to make sure the file will actually be written to your disk.
Basic usage of Filters in ffmpeg
Filters are an incredibly powerful feature of ffmpeg. There is a crazy amount of filters available for you to use, making ffmpeg completely capable of handling any editing needs. Since there are so many filters and this article is already comprehensive, I’ll only cover some simple filters.
As you can see, I am specifying video filters (-vf, short for -filter:v) and audio filters (-af, short for -filter:a). The actual filters are written inside double quotes (“) and can be chained by separating them with commas (,). You can specify as many filters as you want (I’ve written etc to note additional filters, it isn’t actually part of the command).
The general form of a filter is:
The different filter settings and their values are separated by colons.
You can also execute mathematical operations as values for the different settings.
Note:More detailed coverage of different constants to be used in expressions and different settings can be found in the official ffmpeg filter documentation.
I’ll cover both video and audio filters. Let’s see some examples!
1. Video Scaling
This is a very simple filter. The only settings are width and height:
As noted, the second cut will cut in the center of the input file (since I haven’t specified x and y coordinates for the top-left corner). The first command will cut from the top-left corner (x=0:y=0).
Here is an example that uses mathematical expressions as values:
The first command amplifies the volume 1.5 times. The second command makes the audio 1/4 (0.25) times quieter.
Bonus Tip: Playback Speed Adjustments
I’ll include both the video (doesn’t affect audio) and audio filters for this task.
The filter for video is setpts (PTS = presentation time stamp). This works in a somewhat interesting manner. Since we actually modify the PTS, a bigger coefficient means a slower result and vice-versa:
The first command slows the audio by 1/4 of the original speed. The second command speeds up the audio 4 times (2*2).
Note:For changing video and audio playback speed with the same command you’ll have to look into filtergraphs.
In this comprehensive guide I covered the installation, basic usage, advanced usage and basics of filters.
I hope that this can be a helpful resource for anyone looking to try outffmpeg, for someone that wants to use ffmpeg for multiple tasks or even just for someone who is willing to get more familiar with the extensive features of this amazing utility.
Let us know if you found this article useful in the comments below and tell us what are you using ffmpeg for! We look forward to any additions of any kind. Thanks for reading our blog and keep an eye out for an article covering more complex filtering. Enjoy!