ffmpeg is a CLI (command line primarily based) utility for processing media recordsdata. It’s a framework with a mess of options and, due to it’s open sourcee license, it’s the base for a lot of widespread apps akin to VLC, YouTube, iTunes and plenty of extra. Various Linux video editors use ffmpeg beneath the GUI.
What I really like much more about ffmpeg is that it may be used on it’s personal to perform many processing duties in a quite simple method (with one or two instructions). Nevertheless, it’s a really highly effective program that can be utilized in additional complicated methods and even exchange an enhancing workflow.
On this ffmpeg tutorial, I’ll present you set up ffmpeg and I’ll be masking it’s completely different makes use of. I’ll even get into some extra complicated options.
Listed here are the sections of this information:
I’ll be explaining every thing intimately so you’ll be able to perceive what’s going on even if you’re new to the Linux world.
I’ll be utilizing Ubuntu 18.04, however these instructions ought to work for different distributions too.
Notice: Though I’ve researched ffmpeg totally and examined every thing I’ll point out on this article, I don’t edit movies (or different media) every day. In case you have any solutions, suggestions or corrections, let me know within the feedback!
Let’s get for it!
Putting in ffmpeg on Ubuntu & different Linux
Putting in ffmpeg is an easy course of. It’s a preferred software and is obtainable in many of the Linux distribution by means of the bundle supervisor.
Set up ffmpeg in Ubuntu
In Ubuntu, ffmpeg is obtainable within the Universe repository so be certain that to allow universe repository. after which replace the packages record and set up ffmpeg. You are able to do all these utilizing the beneath instructions in a terminal:
sudo add-apt-repository universe
sudo apt replace
sudo apt set up ffmpeg
That must be it! You possibly can confirm the set up with merely typing:
This could show some textual content describing your ffmpeg configuration (together with model).
As you’ll be able to see from the screenshot above, the put in model is three.Four.Four. Nevertheless, the newest model of ffmpeg in the meanwhile of writing this text is Four.1. To put in any Four.x model, you’ll have to make use of the ffmpeg ppa. Click on to develop the subsequent part to see the small print of ffmpeg set up by way of PPA.
Set up the most recent ffmpeeg in Ubuntu-based distros with PPA
There’s an unofficial PPA that you need to use to put in the most recent ffmpeg launch. Merely use these instructions:
sudo add-apt-repository ppa:jonathonf/ffmpeg-Four
sudo apt replace
sudo apt set up ffmpeg -y
Set up ffmpeg in Arch-based distributions
To put in ffmpeg in Arch-based distributions, use the next command:
sudo pacman -S ffmpeg
Set up ffmpeg in Fedora-based distributions
To put in ffmpeg in Fedora-based Linux distributions, you need to use this command:
sudo dnf set up ffmpeg
Learn how to use ffmpeg: The Fundamentals
With ffmpeg put in, I’ll go over fundamental instructions to get you began utilizing this highly effective device.
Zero. ffmpeg Instructions
The fundamental kind a ffmpeg command is:
ffmpeg [global_options] … [output_file_options] output_url …
You must understand that all file choices apply solely to the file that follows them (you’ll have to write down them once more for the subsequent file).
Any file not preceded by -i is taken into account an output file. Ffmpeg makes use of as many inputs and outputs as you present. You may also use the identical title for each an enter and an output file, however you’ll have so as to add the -y tag earlier than the output file title.
You shouldn’t combine inputs and outputs; specify enter recordsdata first, then specify output recordsdata.
1. Get media file data
The primary use of ffmpeg is displaying data a couple of media file. This may be carried out by not getting into any output recordsdata. Merely sort:
ffmpeg -i file_name
This works for each audio and video recordsdata:
ffmpeg -i video_file.mp4
ffmpeg -i audio_file.mp3
Show details about a media file with ffmpeg
Though helpful, this command will show approach an excessive amount of data that isn’t related to your file (details about ffmpeg). To omit that, add the -hide_banner flag:
ffmpeg -i video_file.mp4 -hide_banner
ffmpeg -i audio_file.mp3 -hide_banner
Show information of a media file utilizing ffmpeg
As you’ll be able to see, the command now outputs solely particulars regarding the media file you specified (encoder, streams and so forth.).
2. Convert Media Information
One other very helpful approach to make use of ffmpeg is for hassle-free conversion between completely different media codecs. You solely need to specify enter and output recordsdata, since ffmpeg will get the required format from the file extensions. This works for video-to-video and audio-to-audio conversions.
Listed here are some examples:
ffmpeg -i video_input.mp4 video_output.avi
ffmpeg -i video_input.webm video_output.flv
ffmpeg -i audio_input.mp3 audio_output.ogg
ffmpeg -i audio_input.wav audio_output.flac
You possibly can even specify extra output recordsdata:
ffmpeg -i audio_input.wav audio_output_1.mp3 audio_output_2.ogg
This can convert the enter recordsdata to all specified codecs.
To see an inventory of all supported codecs, use:
Once more, you’ll be able to add -hide_banner to omit details about the app.
You possibly can specify the -qscale Zero parameter earlier than the output file to protect the standard of a video file:
ffmpeg -i video_input.wav -qscale Zero video_output.mp4
Moreover, you might specify codecs you need to use, including -c:a (for audio) and -c:v (for video) adopted by the title of the codecs, or copy if you wish to use the identical codecs as the unique file:
ffmpeg -i video_input.mp4 -c:v copy -c:a libvorbis video_output.avi
Notice: this may make the file extension relatively deceiving, so watch out.
three. Extract Audio from Video
To extract audio from a video file, you do a easy conversion and add the -vn flag:
ffmpeg -i video.mp4 -vn audio.mp3
Notice that this command will make the most of the bit price of the unique file. It’s a good suggestion to set it your self; you need to use -ab (audio bit price):
ffmpeg -i video.mp4 -vn -ab 128okay audio.mp3
Some frequent bit charges are 96okay, 128okay, 192okay, 256okay, 320okay (highest bitrate supported by mp3).
Different frequent choices are -ar (audio frequency: 22050, 441000, 48000), -ac (variety of audio channels), -f (audio format, though usually mechanically detected). -ab can be changed by -b:a. For instance:
ffmpeg -i video.mov -vn -ar 44100 -ac 2 -b:a 128okay -f mp3 audio.mp3
Four. Mute Audio in Video
Just like the final activity, to perform this we’ll add a easy tag: -an (as a substitute of -vn).
ffmpeg -i video_input.mp4 -an -video_output.mp4
Notice: The -an tag will render all audio choices for that output file ineffective, since there will likely be no audio in the results of the operation.
5. Extract Photographs from Video
This may turn out to be useful for lots of people. For instance, you might need a collection of photos (e.g. a slideshow) and also you need to get all photos out of that. Merely enter:
ffmpeg -i video.mp4 -r 1 -f image2 image-%3d.png
Let’s take that aside for a second.
-r specifies the body price (what number of frames are extracted into photos in a single second, default: 25), -f specifies the output format (image2 really stands for image2 sequence.
The final parameter (the output file) has a considerably attention-grabbing naming: it makes use of %3d on the finish. This merely numbers your photos with three digits (00Zero, Zero01, and so forth.). You may also use %2nd (2 digit format) and even %4d (Four digit format) if you would like. Be at liberty to experiment and use no matter works for you!
Notice: There’s additionally a method to flip a set of photos right into a video/slideshow. I’ll cowl that within the Superior Utilization part.
6. Change Video Decision or Side Ratio
One other easy activity for ffmpeg. All it’s important to do to resize a video is specify a brand new decision after the -s flag:
ffmpeg -i video_input.mov -s 1024×576 video_output.mp4
Moreover, you may need to specify -c:a to ensure the audio codecs of the output file is the appropriate one:
ffmpeg -i video_input.h264 -s 640×480 -c:a video_output.mov
You may also change the facet ratio utilizing -aspect:
ffmpeg -i video_input.mp4 -aspect Four:three video_output.mp4
Notice: As soon as once more, one other barely extra highly effective technique will likely be talked about within the Superior Utilization part.
7. Add Poster Picture to Audio
It is a nice method to flip audio into video, utilizing a single picture (akin to a album cowl) for the audio. It is a very helpful function while you need to add audio recordsdata to websites that don’t enable something aside from video/photos (YouTube and Fb are two examples of such websites).
Right here is an instance:
ffmpeg -loop 1 -i picture.jpg -i audio.wav -c:v libx264 -c:a aac -strict experimental -b:a 192okay -shortest output.mp4
Simply change the codecs (-c:v specifies video codecs, -c:a specifies audio codecs) and the names of your recordsdata.
Notice: You don’t have to make use of -strict experimental if you’re utilizing a more recent model (Four.x).
eight. Add Subtitles to Video
Equally as helpful and as easy to do with ffmpeg is to add subtitles to a video (for instance a film). Enter the next:
ffmpeg -i video.mp4 -i subtitles.srt -c:v copy -c:a duplicate -preset veryfast -c:s mov_text -map Zero -map 1 output.mp4
After all, you’ll be able to specify any codecs that you really want (and every other further choices associated to audio/video). You possibly can learn this text about enhancing subtitles in Linux, graphically.
9. Compress Media Information
Compressing recordsdata drastically reduces file dimension, saving you numerous area. This may be essential for transferring recordsdata. With ffmpeg, there are a number of strategies to cut back file dimension.
Notice: Compressing recordsdata an excessive amount of will noticeably cut back the standard of the ensuing file.
To start with, for audio recordsdata, merely cut back the bit price (utilizing -b:a or -ab):
ffmpeg -i audio_input.mp3 -ab 128okay audio_output.mp3
ffmpeg -i audio_input.mp3 -b:a 192okay audio_output.mp3
Once more, some completely different bit price values are: 96okay, 112okay, 128okay, 160okay, 192okay, 256okay, 320okay. The upper the bit price, the upper the file dimension and the standard.
For video recordsdata, you will have extra choices. A technique could be to cut back the video bit price (utilizing -b:v):
ffmpeg -i video_input.mp4 -b:v 1000okay -bufsize 1000okay video_output.mp4
Notice: Video bit charges are completely different from audio bit charges (a lot increased values).
You may set the -crf flag (Fixed Charge Issue). A decrease crf means the next bit price. Additionally, utilizing libx264 as video codec helps. Right here’s a really environment friendly dimension discount with very small discount in high quality:
ffmpeg -i video_input.mp4 -c:v libx264 -crf 28 video_output.mp4
A crf of 20 to 30 is frequent, however be at liberty to mess around with the particular worth.
Decreasing the body price may work in some instances (though it would very simply make the video really feel sluggish):
ffmpeg -i video_input.mp4 -r 24 video_output.mp4
-r specifies the body price (on this case 24).
You may additionally attempt lowering the dimension of the video (already defined how do to that). An extra tip is to compress the audio too, making it stereo and lowering the bit price. For instance:
ffmpeg -i video_input.mp4 -c:v libx264 -ac 2 -c:a aac -strict -2 -b:a 128okay -crf 28 video_output.mp4
Notice: -strict -2 and -ac 2 deal with the stereo half. I already lined the half that reduces the bit price.
10. Minimize Media Information (Fundamental)
To trim recordsdata from the starting it’s important to specify a period utilizing -t:
ffmpeg -i input_video.mp4 -t 5 output_video.mp4
ffmpeg -i input_audio.wav -t 00:00:05 output_audio.wav
As you’ll be able to see, this works for each video and audio recordsdata. Each of the instructions above do the identical factor: save the primary 5 seconds of the enter file to the output file. I’ve used to other ways of inputting the period: a single quantity (variety of seconds) and HH:MM:SS (hours, minutes, seconds). The second model acts extra like an finish time.
You possibly can go even additional by specifying a begin time with -ss, and even an finish time with -to:
ffmpeg -i input_audio.mp3 -ss 00:01:14 output_audio.mp3
ffmpeg -i input_audio.wav -ss 00:00:30 -t 10 output_audio.wav
ffmpeg -i input_video.h264 -ss 00:01:30 -to 00:01:40 output_video.h264
ffmpeg -i input_audio.ogg -ss 5 output_audio.ogg
You possibly can see begin time (-ss HH:MM:SS), period (-t period) in seconds, finish time (-to HH:MM:SS), and begin time (-s period) in seconds (beginning after period seconds).
All these are legitimate instructions and you’ll work with any a part of a media file utilizing them.
Learn how to use ffmpeg: Superior Utilization
I’ll now cowl barely extra superior options (akin to display casting, utilizing gadgets and so forth.). Let’s get proper to the primary level!
1. Break up Media Information
I already lined trimming recordsdata. What it’s important to do to break up a file into a number of elements is to specify a number of trims (specifying begin instances, finish instances or period earlier than every output recordsdata).
Observe this instance:
ffmpeg -i video.mp4 -t 00:00:30 video_1.mp4 -ss 00:00:30 video_2.mp4
The syntax is kind of easy. I specified -t 00:00:30 as period for the primary half (the primary half will include the primary 30 seconds of the unique video). Subsequent, I specified that I’d like the remainder to be a part of a second video (ranging from the tip of the final half, 00:00:30).
This may be carried out for as many elements as you need! Have enjoyable enjoying with this function, since it is rather highly effective. Remember that it really works with audio too.
2. Concatenate Media Information
ffmpeg may do the alternative course of: placing a number of elements collectively.
To take action, you’ll need to create a brand new textual content file and begin enhancing it utilizing your most well-liked editor.
Since I like utilizing the terminal, I’ll use contact and vim. It doesn’t matter what you name this file. I’ll name mine be part of.txt and create the file utilizing contact:
Now, I’ll edit it utilizing vim:
You need to use no matter instruments you need (nano, gedit, and so forth.), that is simply what it’s the best for me to make use of.
Right here, enter the full paths to all of the recordsdata you want to be part of (they are going to be joined within the order you write them right here), one per line. Make certain they’ve the identical extension (e.g. mp4). Right here’s mine:
Save the file you simply edited. This technique works for any audio or video recordsdata.
Now enter the next:
ffmpeg -f concat -i be part of.txt output.mp4
Notice: My output file is output.mp4, however that’s as a result of all my enter recordsdata have the mp4 extension.
This could be part of all of the recordsdata you wrote down in be part of.txt (or nonetheless you named that file) into one single output file.
three. Be part of Photographs right into a Video
That is how one can put collectively a slideshow or something related. I’ll present you add audio too.
The very first thing I like to recommend doing is ensuring the pictures you need to put collectively are positioned in the identical listing. I’ll put mine in a listing known as my_photos. The extensions beneficial for the photographs are .png and .jpg. Whichever you select, be certain that all photos have the identical extension (in any other case, utilizing ffmpeg might need unintended results). You may merely convert the .png recordsdata to .jpg or vice-versa (see Fundamental Utilization).
The format (-f) of our conversion must be image2pipe. For the enter, you’ll have to supply a hyphen (–). What image2pipe means that you can do is pipe (utilizing | within the terminal)the outcomes of a command akin to cat into ffmpeg as a substitute of inputting all names one after the other. That’s precisely what I’ll do. For this to work, we’ll even have to say that we would like the video codecs to be copied (-c:v copy) (to correctly use the pictures):
cat my_photos/* | ffmpeg -f image2pipe -i – -c:v copy video.mkv
In case you play this file, you may suppose just some footage have been added to the slideshow. Really, your entire footage have been added, however ffmpeg cycles by means of them as quick as potential. This implies 1 body per picture; ffmpeg (by default) runs at round 23 fps.
To alter that, you’ll need to specify the body price (-framerate) you need:
cat my_photos/* | ffmpeg -framerate 1 -f image2pipe -i – -c:v copy video.mkv
In my instance, I set the body price to 1, that signifies that every body (that additionally means every photos) seems for 1 second.
So as to add audio, akin to a background track, we’ll need to specify the audio file as enter file (-i audo_file) and copy the audio codecs (-c:a duplicate). For the codecs, you might copy each audio and video codecs on the similar time with -c copy. Be sure to set the codecs proper earlier than specifying the output file. You may additionally set the body price in order that your entire footage synchronize with the period of the audio you need to use. To get that, divide the variety of footage by the period of the audio (in seconds). For my instance, I’ve a 22 seconds lengthy audio file and 9 photos. 9 / 22 is roughly Zero.40, in order that’s what I’ll use for my body price:
cat my_photos/* | ffmpeg -framerate Zero.40 -f image2pipe -i – -i audio.wav -c copy video.mkv
Four. Document Your Display
Screencasting with ffmpeg is just not a tough job. The format (-f) it is advisable use is x11seize. This can seize your XSERVER. As enter you’ll need to specify your display show quantity (primary display ought to usually be Zero:Zero). Nevertheless, this may solely seize a top-left portion of the display. It is best to add a display dimension (-s). Mine is 1920×1080. The display dimension must be talked about earlier than the enter:
ffmpeg -f x11seize -s 1920×1080 -i :Zero.Zero output.mp4
Press q or CTRL+C at any time to cease the screencast.
Bonus trick: You may make the scale of the output file fullscreen with inputting this after for the dimension (as a substitute of 1920×1080 or every other set decision):
-s $(xdpyinfo | grep dimensions | awk ”)
Right here’s the complete command:
ffmpeg -f x11seize -s $(xdpyinfo | grep dimensions | awk ”) -i :Zero.Zero output.mp4
5. Document Your Webcam
Recording enter out of your webcam (or one other gadget, such an usb digicam) is even simpler. In Linux, gadgets are shops in /dev as /dev/video0, /dev/video1, and so forth.:
ffmpeg -i /dev/video0 output.mkv
Once more, q or CTRL+C to cease recording.
6. Document Your Audio
Linux handles audio primarily by means of ALSA and pulseaudio. ffmpeg can document each, however I’ll cowl pulseaudio, since Debian-based distributions embrace it by default. The syntax is a little bit completely different for the 2 strategies.
For pulseaudio, it’s important to power (-f) alsa and specify default as enter (-i default):
ffmpeg -f alsa -i default output.mp3
Notice: In your distribution sound settings, be certain that the default recording gadget is the one you need to document.
I play guitar quite a bit and I’ve an audio interface so I can document it. I had a pleasing shock when ffmpeg recorded my guitar with no issues.
Bonus Tips about Recording
For any sort of recording you can even specify codecs, after all (already lined). You possibly can select a selected body price (-r). You possibly can mix recording audio with webcam/display recording too!
ffmpeg -i /dev/video0 -f alsa -i default -c:v libx264 -c:a flac -r 30 output.mkv
As a substitute of recording audio, you’ll be able to simply as simply add an audio file as enter to be recorded on high of your screencast/webcam:
ffmpeg -f x11seize -s $(xdpyinfo | grep dimensions | awk ”) -i :Zero.Zero -i audio.wav -c:a duplicate output.mp4
Notice: ffmpeg information in small items, so a very small recording may not get saved. I like to recommend recording just a bit longer and slicing it afterwards (for those who solely need a number of seconds), simply to ensure the file will really be written to your disk.
Fundamental utilization of Filters in ffmpeg
Filters are an extremely highly effective function of ffmpeg. There’s a loopy quantity of filters out there so that you can use, making ffmpeg fully able to dealing with any enhancing wants. Since there are such a lot of filters and this text is already complete, I’ll solely cowl some easy filters.
The fundamental construction for utilizing a filter is:
ffmpeg -i enter.mp4 -vf “filter=setting_1=value_1:setting_2=value_2,and so forth” output.mp4
ffmpeg -i enter.wav -af “filter=setting_1=value_1:setting_2=value_2,and so forth” output.wav
As you’ll be able to see, I’m specifying video filters (-vf, quick for -filter:v) and audio filters (-af, quick for -filter:a). The precise filters are written inside double quotes (“) and will be chained by separating them with commas (,). You possibly can specify as many filters as you need (I’ve written and so forth to notice further filters, it isn’t really a part of the command).
The normal kind of a filter is:
The completely different filter settings and their values are separated by colons.
You may also execute mathematical operations as values for the completely different settings.
Notice: Extra detailed protection of various constants for use in expressions and completely different settings will be discovered within the official ffmpeg filter documentation.
I’ll cowl each video and audio filters. Let’s see some examples!
1. Video Scaling
It is a quite simple filter. The one settings are width and top:
ffmpeg -i enter.mp4 -vf “scale=w=800:h=600” output.mp4
I discussed that you need to use mathematical operations for values too:
ffmpeg -i enter.mkv -vf “scale=w=half*in_w:h=half*in_h” output.mkv
This command units the output dimension to half (half) of the enter dimension (in_w, in_h).
2. Video Cropping
As for the scaling filter, the settings are the width and top of the ensuing file. Optionally, you’ll be able to specify coordinates for the top-left nook of the minimize (default: heart of the enter video):
ffmpeg -i enter.mp4 -vf “crop=w=1280:h=720:x=Zero:y=Zero” output.mp4
ffmpeg -i enter.mkv -vf “crop=w=400:h=400” output.mkv
As famous, the second minimize will minimize within the heart of the enter file (since I haven’t specified x and y coordinates for the top-left nook). The primary command will minimize from the top-left nook (x=Zero:y=Zero).
Right here is an instance that makes use of mathematical expressions as values:
ffmpeg -i enter.mkv -vf “crop=w=three/Four*in_w:h=three/Four*in_h” output.mkv
This command units the output dimension to three/Four of the enter dimension (in_w, in_h).
three. Video Rotation
You may also rotate a video clockwise by a worth (in radians). To make issues simpler, you’ll be able to specify a worth in levels and convert it to radians by multiplying that worth with PI/180:
ffmpeg -i enter.avi -vf “rotate=90*PI/180”
ffmpeg -i enter.mp4 -vf “rotate=PI”
The primary command will rotate the video clockwise 90 levels. The second command will flip the video upside-down (PI rad = 180 levels).
Four. Audio Channel Remapping
This may be useful for those who in some way solely get audio within the proper ear or something related. You may make the audio audible from each ears (on this specific case) like this:
ffmpeg -i enter.mp3 -af “channelmap=1-Zero|1-1” output.mp3
This maps the proper (1) audio channel to each the left (Zero) and the proper (1) audio channel. (the left quantity represents enter, the proper quantity represents output).
5. Audio Quantity Multiplying
You possibly can multiply the audio quantity by any actual quantity (integer or not). You solely must specify the multiplier:
ffmpeg -i enter.wav -af “quantity=1.5” output.wav
ffmpeg -i enter.ogg -af “quantity=Zero.75” output.ogg
The primary command amplifies the quantity 1.5 instances. The second command makes the audio 1/Four (Zero.25) instances quieter.
Bonus Tip: Playback Velocity Changes
I’ll embrace each the video (doesn’t have an effect on audio) and audio filters for this activity.
The filter for video is setpts (PTS = presentation time stamp). This works in a considerably attention-grabbing method. Since we really modify the PTS, a greater coefficient means a slower consequence and vice-versa:
ffmpeg -i enter.mkv -vf “setpts=Zero.5*PTS” output.mkv
ffmpeg -i enter.mp4 -vf “setpts=2*PTS” output,mp4
As I simply defined, the primary command doubles the playback velocity and the second command slows the video to half of the velocity.A
The filter for that is atempo. There’s one little caveat: it might solely take values between Zero.5 (half the velocity) and 2 (double the velocity). To get round that, you’ll be able to chain atempo filters:
ffmpeg -i enter.wav -af “atempo=Zero.75” output.wav
ffmpeg -i enter.mp3 -af “atempo=2.Zero,atempo=2.Zero” ouutput.mp3
The primary command slows the audio by 1/Four of the unique velocity. The second command speeds up the audio Four instances (2*2).
Notice: For altering video and audio playback velocity with the identical command you’ll need to look into filtergraphs.
On this complete information I lined the set up, fundamental utilization, superior utilization and fundamentals of filters.
I hope that this generally is a useful useful resource for anybody trying to check out ffmpeg, for somebody that wishes to make use of ffmpeg for a number of duties and even only for somebody who’s prepared to get extra conversant in the intensive options of this superb utility.
Tell us for those who discovered this text helpful within the feedback beneath and inform us what are you utilizing ffmpeg for! We look ahead to any additions of any variety. Thanks for studying our weblog and maintain an eye fixed out for an article masking extra complicated filtering. Get pleasure from!