FFmpeg Commands For Beginners
Introduction
FFmpeg is a command-line audio and video converter available for Linux, macOS, and Windows. This open-source tool helps users manipulate multimedia content and offers free access to several audio and video libraries.
Installing FFmpeg on Ubuntu
The official Ubuntu repositories contain FFmpeg packages that can be installed with the apt
package manager. This is the easiest way to install FFmpeg on Ubuntu.
Perform the steps below to install FFmpeg on Ubuntu:
1. Start by updating the packages list:
sudo apt update
2. Next, install FFmpeg by typing the following command:
sudo apt install ffmpeg
3. To validate that the package is installed properly use the following command which prints the FFmpeg version:
ffmpeg -version
4. The output should look something like this:
ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11 (Ubuntu 11.2.0-19ubuntu1)
FFmpeg Commands
1. Get Video Info
First, you can easily get the information of a given media file using FFmpeg. Normally it will require specifying the input file and output file, but in this case, the output file can be omitted since we only need to get some basic information. You can run the following command:
ffmpeg -i "file/address/input.mp4" -hide_banner
The “hide_banner” flag helps hide FFmpeg banners and other details and only shows the media file information.
2. Convert Video to Another Format
FFmpeg is extremely useful to convert your video file to another format. For example, to convert MP4 to MOV with FFmpeg, run the command below:
ffmpeg -i input.mp4 output.mov
3. Convert Video to Audio
FFmpeg can also remove the video part or the audio part separately. The following command will remove the video stream from a video file:
ffmpeg -i input.mp4 -vn output.mp3
Here, -vn
indicates that we have disabled video recording in the output file.
4. Remove Audio
Also, you can remove the audio stream and keep the video stream with FFmpeg using the command below. You can also set the output format as other supported formats.
ffmpeg -i input.mp4 -an output.mp4
Here, -an
indicates no audio recording. In other words, this option will mute the audio.
5. Convert a Specific Portion of a Video
In addition to converting the format of the entire video, FFmpeg also allows you to convert a specific part of the video or audio format when needed. In this situation, you need to use the -t value to specify the time. For example, the following command will convert the first 10 seconds of the input.mp4 to MKV format. Note you can also specify the time in hh.mm.ss format.
ffmpeg -i input.mp4 -t 10 output.mkv
6. Trim Video
FFmpeg can also easily help with trimming video and audio files. Use the --ss
value to set the start time of the clip, and -to
value to set the end time. The -c
value is to set the codec. Similarly, you can also use the -t
value in the example above to set the duration of the clip.
ffmpeg -i input.mp4 -ss 00:01:54 -to 00:06:53 -c copy output.mp4
7. Concatenate Videos
FFmpeg will serve as a video merger. If you have several video clips encoded with the same codec, you can merge or concatenate these videos into one. Just create a txt file with a list of all source files that you wish to concatenate, then run the command below.
ffmpeg -f concat -i files.txt -c copy output.mp4
And files.txt contain:
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
8. Rotate Video
You can easily rotate a video clockwise and counter-clockwise using FFmpeg, as well as vertically and horizontally flip a video. The feature we use in this case is “Transpose”. The following command will rotate the input video by 90 degrees clockwise.
ffmpeg -i input.mp4 -vf "transpose=1" output.mp4
The available parameters for transpose are 0, 1, 2, and 3.
- 0 — Rotate by 90 degrees counter-clockwise and flip vertically (default value).
- 1 — Rotate by 90 degrees clockwise.
- 2 — Rotate by 90 degrees counter-clockwise.
- 3 — Rotate by 90 degrees clockwise and flip vertically.
Note that you will need to mention the transpose parameter two times like below to rotate videos by 180 degrees clockwise.
ffmpeg -i input.mp4 -vf "transpose=2,transpose=2" output.mp4
9. Create Animated GIF
The basic formula is as simple as shown below:
ffmpeg -i input.mp4 output.gif
10. Resize the Image
You can use FFmpeg to resize images. The following command will resize the image to 1920:1080. You can set this value to any of your desired sizes.
ffmpeg -i input.png -vf "scale=1920:1080" output.png
11. Getting Help
You can always use -h
to print help:
ffmpeg -h
12. Convert a Video to X Images
This command will generate images named image1.jpg
, image2.jpg
, etc, from a given video file. The following image formats are available: PGM, PPM, PAM, PGMYUV, JPEG, GIF, PNG, TIFF, and SGI.
ffmpeg -i input.mp4 image%d.jpg
13. Resize a Video
Using the -vf scale
filter, it is possible to resize videos to the desired size:
ffmpeg -i input.mp4 -vf scale=320:240 output.mp4