Almost anyone who deals with videos may have heard of FFmpeg before. For those who are unfamiliar, FFmpeg is a free and open-source program that can convert any video format to another and alter its codecs.
FFmpeg supports nearly all audio/video codecs (H.264, H.265, VP8, VP9, AAC, OPUS, and more), file formats (MP4, FLV, MKV, TS, WEBM, MP3, etc.), and even streaming protocols (HTTP, RTMP, RTSP, HLS, etc.).
Here’s how you can install and use FFmpeg to process audio and video files on Linux.
FFmpeg Installation on Linux
FFmpeg is a free and open-source tool available in the default repositories of almost every major Linux distribution. You can also get its source code for free if you wish to compile it yourself.
sudo apt install ffmpeg
sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm
sudo dnf -y install ffmpeg
yum install ffmpeg ffmpeg-devel
pacman -S ffmpeg
If everything goes well during installation, you should be able to see the FFmpeg version when you use the -version argument.
Getting Video Information With FFmpeg
It is possible to see the information of a video that you want to edit with FFmpeg using the -i flag:
ffmpeg -i example-video.mp4 -hide_banner
Here, the -hide_banner parameter’s job is to hide unnecessary information. You can remove this parameter and see the difference in the output.
As you can see, it is possible to get a lot of information such as video codec type, creation date, metadata, and encoder structure of the sample video.
Converting Video or Audio Files to Another Format
One of the most useful features of FFmpeg is that it can convert a video or audio to another format. You can do this with simple one-liners.
MOV to MP4 With FFmpeg
You can convert your MOV format video file to MP4 with FFmpeg using the command below:
ffmpeg -i input-mov-video.mov output-video.mp4
First, use the -i parameter, which stands for input video. Then, enter the file you want to convert. Finally, enter the format you want to convert it to. You can give your output any name you want.
While FFmpeg is running, it will show you the changes it has made on the command screen. Your output file will be stored in your present working directory.
WAV to MP3 With FFmpeg
Similar to videos, you can do the same conversions for audio files. For example, you can convert a WAV audio file to MP3 as follows:
ffmpeg -i example-wav.wav -vn -ar 48000 -ac 2 -b:a 256 output.mp3
This command has more parameters than the video conversion method, but they are not difficult to understand. To explain these:
- -vn: Sometimes the audio files you listen to have pictures. These images usually originate from videos. If you do not want such images in the output, you can use this parameter.
- -ar: This parameter allows you to set the audio frequency of the audio file you want to convert. You can adjust the sound quality and frequency with values such as 8kHz, 44.1kHz, or 48kHz.
- -ac: You may have heard the terms mono and stereo before. This parameter can help you set the number of audio channels.
- -b:a: This parameter allows you to set the audio bitrate per second. The higher the kilobit, the higher the sound quality.
Audio Operations With FFmpeg
FFmpeg can also separate audio from videos. For this, it is sufficient to use the -vn parameter:
ffmpeg -i example-video.mp4 -vn output.mp3
If you managed to separate the audio from the video, you can now try to remove the audio from the videos. The difference here is the -an parameter. You should keep this parameter in mind if you want to disable any sound in a video:
ffmpeg -i example-video.mp4 -an output-mute.mp4
Video Size Processing With FFmpeg
Video sizes can be pretty annoying sometimes, especially when you want to upload them somewhere. You no longer need to download programs to trim them as FFmpeg can do this for you. There are some parameters you need to know for this, though:
- -ss: Use this parameter to set the start time of the clip
- -to: Allows you to specify the end time of the clip
- -c: Set the codec of your clip using this parameter
- -t: Use this parameter to set the duration of the clip
You can derive many examples using these parameters. For example, if you want to trim a video, you can use something like:
ffmpeg -i example-video.mp4 -ss 00:02:25 -to 00:03:50 -c copy output-trim.mp4
It is also possible to crop only the image inside the video and not the whole video. For this, you can resort to something like:
ffmpeg -i example-video.mp4 -filter:v "crop=w:h:x:y" output-crop.mp4
Here are the parameters used in the aforementioned command:
- -filter:v: This parameter specifies the filtering process you will apply to the video
- crop: This parameter is for specifying that a clipping operation will be performed
- w:h:x:y: As you might’ve guessed already, the w, h, x, and y variables denote width, height, and position of the crop box, respectively
Editing Videos on Linux With FFmpeg
Editing videos doesn’t end with just trimming and cutting. Sometimes you also need to change the scale of the video. The following command will resize the video to a size you want:
ffmpeg -i example-video.mp4 -vf scale=1920:1080 output-scale.mp4
- -vf: This parameter works the same as the -filter:v argument seen above
- scale: You can specify the scale sizes you want in your output with this parameter
FFmpeg also allows you to combine multiple videos. Imagine you have multiple clips encoded with the same codec. Enter the list of videos you want to merge into a .txt file. Then, run the following command:
ffmpeg -f concat -i my-video-list.txt -c copy sum-output.mp4
The concat parameter here combines your files. It is also possible to rotate videos with FFmpeg:
ffmpeg -i example-video.mp4 -vf "transpose=2" output-rotate.mp4
- transpose=0: Flip vertically (default)
- transpose=1: Rotate 90 degrees clockwise
- transpose=2: Rotate 90 degrees counterclockwise
- transpose=3: Flip vertically
To rotate videos 180 degrees clockwise, you need to specify the transpose parameter twice:
ffmpeg -i example-video.mp4 -vf "transpose=2,transpose=2" output-rotate.mp4
FPS and GOP Operations
As you know, FPS means frames per second. GOP (group of pictures) is the distance between two keyframes. FFmpeg is also useful for changing some parameters, including FPS and GOP. If you use the command below, FFmpeg will change the original FPS to the value you set:
ffmpeg -i example-video.mp4 -vf "fps=60" output-fps.mp4
For GOP, you can use the -g parameter and set its value to whatever you want. Note that forcing too many keyframes can be harmful to some encoders’ forward algorithms.
ffmpeg -i example-video.mp4 -g 200 output-gop.mp4
Create Animated GIFs With FFmpeg
FFmpeg is also ideal for converting a video to animated GIFs. You can use a simple convert command to do this:
ffmpeg -i example-video.mp4 output-gif.gif
But sometimes, you may want to customize the GIF. You can use the various parameters discussed above to achieve this:
ffmpeg -ss 00:01:15 -i example-video.mp4 -to 10 -r 10 -vf scale=250:-1 output-gif.gif
The -r parameter here means the frame rate. As you can see, many different customizations are possible in a single line command.
Apart from converting one or many images to video, you can also extract frames from a video. The following command will extract one frame every second from your input video. Also, these extracted images will have two-digit names like 01.jpeg, 02.jpeg, etc. If you wish, you can also add other parameters you have learned.
ffmpeg -i example-video.mp4 -r 1 image-%02d.jpeg
You can also use other formats such as PNG and BMP for the extracted images.
Why Should You Use FFmpeg on Linux?
As you can see, FFmpeg is very advantageous in many ways. You do not need to have any technical knowledge or professional Linux experience for this. You can perform various media-processing functions with just a few parameters. If you are going to make edits that are not very long, you do not need expensive computer programs and online premium memberships. Moreover, FFmpeg works very well even on low-end devices.
Also, the features of FFmpeg are not limited to the above. When you read the documentation and user manual, you can see how powerful the software actually is. Even using the –help parameter and the man command, you can get more detailed information about using FFmpeg. There are also other great converters for Linux to rival FFmpeg.
Read the full article here