FFmpeg is a remarkable piece of open source software. It offers an impressive range of commands for editing video files. From simple format conversion to complex media transformation, it's a Swiss army knife for video enthusiasts and professionals alike.
In this article, we'll demystify FFmpeg's most powerful and useful commands to help you unleash the full potential of your videos.
Getting started with FFmpeg
You may not be familiar with FFmpeg. If so, chances are you don't have it installed on your computer. You can download it from the official website: https://ffmpeg.org/
Once this is done, all you need to do is open the command prompt on your computer. Simultaneously press "Windows" and "R" or "Command" and "R" (on Mac), then type "cmd" in the conversation bar.
The command prompt will open as soon as you press "Enter".
Remember to use the command "cd" followed by the path to the directory containing the media files you wish to modify.
You're now ready to use the essential commands for FFmpeg.
PS: we're going to share with you some codecs that include variables. Each variable will appear in bold. This is the only part of the command you need to modify to match your files.
Getting media information
Before editing a media file, it's important to understand its properties. To display details such as codecs, resolution, duration, etc. enter the following command:
ffmpeg -i file.mp4
Convert video format
If you wish to convert a video file from one format to another, use the following command:
ffmpeg -i input.mp4 output.avi
In this example, we convert a source file in mp4 format named "input.mp4" to an output file in avi format, which we decide to name "output.avi". However, this works for other video formats.
Convert to audio format
Imagine you want to extract audio from a video to make a podcast, for example. In this case, use the following command:
ffmpeg -i input.mp4 -vn output.mp3
This is (almost) the same principle as the previous command, except that the "vn" option indicates that only the audio track will be extracted.
Resize a video
If you want to publish the same video on different social networks, you may need to modify its format. In this case, to resize a video to a specific size, use this command:
ffmpeg -i input.mp4 -vf scale=640:360 output.mp4
In this example, "scale=640:360" indicates the new resolution. You can modify the values according to the desired format.
Cut a video
If you want to create an excerpt from a video, then here's the command to use:
ffmpeg -i input.mp4 -ss 00:00:30 -t 00:00:30 output.mp4
In this command, "-ss 00:00:30" indicates the point at which you wish to retrieve the video sequence from the initial video. "-t 00:00:30" indicates the length of the output video, named "output.mp4" in this example.
Merging videos
This operation first requires you to create a "join.txt" file containing the paths to the videos to be merged. Specify one path per line, as in the example below:
Once this is done, run the following command:
ffmpeg -f concat -safe 0 -i join.txt -c copy output.mp4
This will merge the videos mentioned in "join.txt" into a single "output.mp4" file.
Extract images from a video
Few tools allow you to do this. With FFmpeg, all you have to do is enter the command :
ffmpeg -i input.mp4 -r 1 -f image2 image-%2d.png
Here, "-r 1" means you will extract one image per second with the following name "image-%2d.png". (image -%2d means that images will be named imageX. X being a number from 0 to 99).
Add subtitles
We've written an article on the subject: "How to subtitle your videos with FFmpeg?". You'll find all the details.
Compress a video
Need to reduce the size of a video while preserving its quality? Use the following command:
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4
Here, "-crf 23" is used to control the quality of compressed video using the Constant Rate Factor (CRF) method. A lower CRF value (e.g. 18) will produce better quality but a larger file, while a higher value (e.g. 28) will produce lower quality but a smaller file. In this case, a value of 23 is used.
Furthermore, "-b:a 128k" sets the audio bitrate to 128 kbps (kilobits per second). This controls the amount of audio data used to represent the sound. Higher bitrates generally offer better audio quality, but may increase file size.
Change playback speed
To adjust the playback speed of a video, use :
ffmpeg -i input.mp4 -vf "setpts=0.5PTS" output.mp4
Here, "0.5" reduces video speed by half.
Create a video from an image
Here again, we refer you to our article: "Combining images in a video" for more details.
Add a cover image to a video
Whether for a Youtube video or even your Instagram reels, you can add a cover image to your videos. In this case, enter the following command:
ffmpeg -i input.mp4 -i cover.jpg -c copy -map 0 -map 1 output.mp4
Here, "cover.jpg" represents the name of your cover image. This image will be superimposed on the 'input.mp4' video.
Increase audio volume
Is the sound on your video too low? Turn it up with this command:
ffmpeg -i input.mp4 -af volume=2 output.mp4
"volume=2" doubles the volume of your video. You can also decrease it with values between 0 and 1. For example, "0.5" reduces the volume by half.
Apply a color filter
For a better look, you may need to add a color filter to your video. In this case, enter the command:
ffmpeg -i input.mp4 -vf colorbalance=rs=0.1 output.mp4
In this command, "rs" is used to target the red saturation level with a 10% increase (0.1 on the codec).
If you wish to modify saturation, color matrix or hue, here's a handy list:
- rs: Red saturation adjustment
- gs: Green saturation setting
- bs: Blue saturation setting
- rm: Red color matrix setting
- gm: Green color matrix setting
- bm: Blue color matrix setting
- rh: Red hue adjustment
- gh: Green hue setting
- bh: Blue hue setting
Convert video to GIF
For this command, we refer you to the second part of our article: "5 good reasons to use animated GIFs and how to create them with FFmpeg"
Crop a video
Would you like to eliminate the edges of your video? In that case, here's the command that might come in handy:
ffmpeg -i input.mp4 -vf "crop=640:360:0:0" output.mp4
To adapt this codec to your use, here are the details:
- 640:360: This specifies the width and height of the crop area, respectively. In this example, an area 640 pixels wide by 360 pixels high will be cropped.
- 0:0: These values specify the starting horizontal (x) and vertical (y) positions for cutting. In this case, 0:0 means that the top left corner of the input video is used as the starting point for trimming.
Reverse a video
Need to hide a brand name on your video? Forget about unsightly blur, and instead invert your video. To do this, enter :
ffmpeg -i input.mp4 -vf "hflip" output.mp4
Convert video to black & white
To convert your video to black and white, use the following command:
ffmpeg -i input.mp4 -vf "colorchannelmixer=.3:.4:.3:0:.3:.4:.3:0:.3:.4:.3" output.mp4
This will apply a color conversion matrix to create a black and white effect.
Edit video audio
If you want to change the audio of your video to background music, for example, here's the command to use:
ffmpeg -i input.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental output.mp4
Your file named "audio.mp3" in your directory will replace the sound in your video.
Apply a fade-in/fade-out effect
To add a fade effect to the beginning or end of your video, use the following command:
ffmpeg -i input.mp4 -vf "fade=in:0:30,fade=out:330:30" output.mp4
Here are the details of this codec so that you can modify it:
- fade=in:0:30 creates a fade-in effect on the video. The parameters are defined as follows: in indicates a fade-in effect, 0 is the number of the first frame where the effect starts, and 30 is the duration in frames of the fade-in effect. In this example, the video will appear progressively over the first 30 frames.
- fade=out:330:30 creates a fade-out effect on the video. The parameters are defined as follows: out indicates a fade-out, 330 is the frame number where the effect starts (in this command, it starts at the 330th frame), and 30 is again the duration in frames of the fade-out effect. In this example, the video will gradually fade out over the last 30 frames.
Finally, these few FFmpeg commands allow you to edit your videos as you see fit. Nevertheless, they can be difficult to implement.
That's why, at Abyssale, we make it a point of honor to offer you an easy-to-use interface for editing your videos. You'll be able to perform the same feats as those presented in this article without having to code, and in a totally intuitive way.