FFmpeg is a free and open-source software project offering extensive functionality for video and audio processing. It’s designed to run on a command line interface and has many different libraries to handle and manipulate multimedia files. FFmpeg is supported for Linux, Windows and macOS.
In this tutorial, we will provide you with the most useful commands to manipulate your videos. As you will see, the syntax of those commands is easy to use and only requires a basic technical background.
Here is how to install FFmpeg
- Go to the FFmpeg download page
- Under Get packages & executable files, choose your platform and follow the instructions to install it
- Open the terminal and type: ffmpeg -version. If this command is executed successfully, it means that the installation was successful
Here is how to edit and convert videos with FFmpeg
We recommend using a sample video file to practice the various commands and get practical experience with FFmpeg.
How to convert video from one format to another format
ffmpeg -i input.mp4 output.mov
Here, we convert a video in MP4 format to a video in MOV format.
The -i option specifies the input file.
You can use many video formats like MKV, AVIF and WEBM.
How to convert video from one format to another format with codecs
ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mkv
Here, we convert video in MP4 format to video in MKV format with H.264 as the video codec and AAC as the audio codec.
The -c:v option specifies the video codec.
The -c:a option specifies the audio codec.
You can use many video codecs like MPEG-4, VP9 and H.265.
You can use many audio codes like MP3, AAC and PCM.
How to trim video
ffmpeg -i <input file> -ss <start time> -t <duration> -c copy <output file>
The -ss option specifies the start time in the following format: HH:MM:SS.xxx (where xxx are milliseconds).
The -t option specifies the cut duration in seconds.
Here, we trim a video starting at a specific point with a specific duration.
How to change the audio volume
ffmpeg -i <input file> -filter:a "volume=<volume level>" <output file>
The -filter:a specifies we refer to the audio stream.
The “volume” option specifies the volume level. When the volume level is 2, it means the original volume is multiplied by 2. If the volume is 0.5, it means the original volume is divided by 2.
How to change audio from mono to stereo
ffmpeg -i <input file> -filter:a "channelmap=0-0|0-1" <output file>
Here, we copy the input left audio channel to the output left audio channel and the output right audio channel. This way, the audio is converted from mono to stereo.
How to crop video
ffmpeg -i <input file> -filter:v "crop=w=<width>:h=<height> :x=<top-left corner x position> :y=<top-left corner y position>" <output file>
The -filter:v specifies we refer to the video stream.
The top-left corner position parameters are optional. If unspecified cropping is centered in the frame.
For the width and height, you can use the variables in_w and in_h that represent the frame width and height respectively. For example, crop=in_w*2/3:h=in_h*2/3
How to scale video
ffmpeg -i <input file> -filter:v "scale=w=<width>:h=<height>" <output file>
You can use the variables in_w and in_h the same way as explained for the crop.
How to rotate video
ffmpeg -i <input file> -filter:v "rotate=<angle>*PI/180" <output file>
You need to specify the angle of the rotation.
How to extract audio from video file
ffmpeg -i input.mkv -vn audio_only.ogg
Here, we extract the audio from the input file, encode it as Vorbis, and save it in Ogg format.
The -vn option specifies striping the video stream. We can also specify -an to strip the audio stream and -sn to strip the subtitle stream.
How to add a subtitle as an external file to a video
ffmpeg -i <input file> -i <subtitle file> -c copy -c:s mov_text <output file>
The subtitle file should be in SRT format. The -c copy specifies copying all the streams (video, audio and subtitle) from the original video. -c:s mov_text specifies copying the subtitle from the subtitle file.
How to add an embedded subtitle to a video
ffmpeg -i <input file> -vf subtitles=<subtitle file> <output file>
The -vf option is an alias for -filter:v.
As for the case of an external file, the subtitle file should be in SRT format. Also, make sure that libass is enabled in FFmpeg.
Now, you can edit and convert your videos with FFmpeg and enjoy a wide range of capabilities.