Try Live Demo

If you’ve been working with multimedia and video, but are not very tech savvy, you’ve probably heard of FFMPEG. You’ve probably thought it was something only video engineers know about and its way out your league.

But you keep asking yourself, what is FFMPEG?

In this post, we’ll unveil the wonders of FFMPEG, guide you on how to install FFMPEG, how to use FFMPEG and finally give you some essential FFMPEG commands you can use to look like a pro.

what really is ffmpeg?

Almost everyone who is developing video and audio software possesses an in-depth understanding of FFmpeg and everyone else is left scratching their head, unsure of its intricacies and functionalities.

So without further ado, let’s get start.

What is FFmpeg?

What is ffmpeg

So What is FFmpeg? FFmpeg stands as an exceptional open-source library and tool, proficiently enabling the seamless decoding and encoding of various video formats interchangeably.

It supports almost all audio and video codecs(h264, h265, vp8, vp9, aac, opus, etc.) and file formats(mp4, Flv, Mkv, ts, WebM, mp3 etc.). It also supports all streaming protocols(HTTP, RTMP, RTSP, HLS, RTSP, etc.).

Because FFmpeg is a library and a tool, it means you can use FFmpeg as a standalone executable on your computer or you can use FFmpeg as a library in your custom video or audio software. Ooo!!!,  here is another interesting fact about FFmpeg:

Lots of the well known media projects(VLC, YouTube, MPlayer, etc.) somehow use FFmpeg which powers these projects.

History Of FFmpeg

The project was started by Fabrice Bellard in 2000 and was led by Michael Niedermayer from 2004 until 2015. Some FFmpeg developers were also part of the MPlayer project.

On January 10, 2014, two Google employees announced that over 1000 bugs had been fixed in FFmpeg during the previous two years.

In January 2018, the ffserver command-line program, a long-running component of FFmpeg, was removed. Developers had previously deprecated the program, citing high maintenance efforts due to its use of internal application programming interfaces.

The project publishes a new release every three months on average. While release versions are available from the website for download, FFmpeg developers recommend that users compile the software from the source using the latest build from their source code Git version control system.

History of Codecs

Two video coding formats with corresponding codecs and one container format have been created within the FFmpeg project so far.

The two video codecs are the lossless FFV1, and the lossless and lossy Snow codec. The development of Snow has stalled, while its bit-stream format has not been finalized yet, making it experimental since 2011. The multimedia container format called NUT is no longer being actively developed but is still maintained.

In the summer of 2010, Fiona Glaser, Ronald Bultje, and David Conrad of the FFmpeg Team announced the ffvp8 decoder. Through testing, they determined that ffvp8 was faster than Google’s own libvpx decoder. Starting with version 0.6, FFmpeg also supported WebM and VP8.

In October 2013, a native VP9 and the OpenHEVC decoder, an open-source High-Efficiency Video Coding (HEVC) decoder, were added to FFmpeg. In 2016 the native AAC encoder was considered stable, removing support for the two external AAC encoders from VisualOn and FAAC. FFmpeg 3.0 (nicknamed “Einstein”) retained build support for the Fraunhofer FDK AAC encoder. Since version 3.4 “Cantor” FFmpeg supported the FITS image format. Since November 2018 in version 4.1 “al-Khwarizmi” AV1 can be muxed in MP4 and Matroska incl. WebM.

How to get FFmpeg?

FFmpeg with ant media

As mentioned above, FFmpeg is a free open source library, you can download the source code at ffmpeg.org and compile it on your computer.  It can compile and run on Windows, Linux, Mac, Android, iOS, etc.

But the easiest way to start using FFMPEG is to download the binary package for your machine. There are binary packages available for Windows, Linux, and Mac.

Assuming you’ve installed FFmpeg, lets go ahead and test that is working. Type the following command into the terminal and press enter:

ffmpeg

You should see something similar to this after executing that command:

Screenshot 2023 08 14 at 22.44.45

If all went well, FFmpeg is now install. Now we’ll convert some media files to test the functionality.

Lets convert a flv video file to an mp4 video file:

ffmpeg -i input.flv output.mp4

Lets break that down a bit. To execute any FFmpeg command, you need to start with the keyword ffmpeg.

Then this command takes an input. The input is the file you want to convert. In our example, its the FLV file. So we’ll use the -i flag followed by the input file.

Because this command will convert or transcode the FLV file into an MP4 file, the next part of the command is to specify the name of the new file followed by the required file extension. In our case MP4.

The command changes the audio and video codecs to convert the FLV into an MP4. There are default audio and video codecs for each format in the FFmpeg. If you do not specify which codec to use, FFmpeg uses default ones.

To convert an Flv file to mp4 without changing codecs

ffmpeg -i input.flv -acodec copy -vcodec copy output.mp4

In this example, it should run much faster than the previous one because it does not change codecs, it just gets audio and video packets from FlV and copies them to an mp4 container.

The key parts to this command that copy the codecs are -acodec and -vcodec followed by the keyword copy.

Even if there are lots of different and advanced samples that we can do with FFmpeg, the scope of this blog post does not cover advanced samples. Nevertheless, we plan to write a second blog post about how to use FFmpeg to show some more examples.

Some useful FFmpeg commands

You can find some of the most used FFmpeg codes below to look like a pro and impress your peers.

1. Getting audio or video file information

To see the details of a media file, run:

$ ffmpeg -i video.mp4

2. Converting video files to different formats

It’s possible to convert media files between different formats with FFmpeg. For example, to convertmp4file toavifile, run:

$ ffmpeg -i video.mp4 video.avi

Similarly, you can convert media files to any format of your choice.

To convert flvformat videos tompegformat, run:

$ ffmpeg -i video.flv video.mpeg

If you want to preserve the quality of your source video file, use'-qscale 0'parameter:

$ ffmpeg -i input.webm -qscale 0 output.mp4

To check the list of supported formats by FFmpeg, run:

$ ffmpeg -formats

3. Converting video files to audio files

To convert a video file to an audio file, just specify the output format as.mp3 or any other audio formats.

The command will convertinput.mp4video file tooutput.mp3audio file.

$ ffmpeg -i input.mp4 -vn output.mp3

Also, you can use various audio transcoding options for the output file as following

$ ffmpeg -i input.mp4 -vn -ar 44100 -ac 2 -ab 320k -f mp3 output.mp3

Take a look at,

  • -vn– Indicates that we have disabled video recording in the output file.
  • -ar– Set the audio frequency of the output file. The common values used are 22050,44100,48000Hz.
  • -ac – Set the number of audio channels.
  • -ab– Indicates the audio bitrate.
  • -f – Output file format.

4. Changing the volume of audio files

You can change the volume of an audio file using"volume filter".

For example, the following command decreases the volume:

$ ffmpeg -i input.mp3 -af 'volume=0.5' output.mp3

The following command increases the volume:

$ ffmpeg -i input.mp3 -af 'volume=1.5' output.mp3

5. Changing the resolution of video files

If you want to send a video file in a specific resolution, you can use the following command:

$ ffmpeg -i input.mp4 -filter:v scale=1280:720 -c:a copy output.mp4

Or,

$ ffmpeg -i input.mp4 -s 1280x720 -c:a copy output.mp4

6. Compressing video files

You can reduce the media file size to keep the file size low to save disk space.

The following command will compress and reduce the output file’s size.

$ ffmpeg -i input.mp4 -vf scale=1280:-1 -c:v libx264 -preset veryslow -crf 24 output.mp4

Please note that reducing the video file size will reduce the video quality. You can also transcode the audio down a bit and make it stereo to reduce the size by including the following options.

-ac 2 -c:a aac -strict -2 -b:a 128k

7. Compressing audio files

You can also compress audio files using-abflag to again save disk space.

For example, you have a 320kbps bitrate audio file. You want to compress it by reducing the bitrate to lower.

$ ffmpeg -i input.mp3 -ab 128 output.mp3

Available audio bitrates list:

  1. 96kbps
  2. 112kbps
  3. 128kbps
  4. 160kbps
  5. 192kbps
  6. 256kbps
  7. 320kbps

8. Removing audio stream from a video file

If you don’t want audio in a video file, use-anflag.

$ ffmpeg -i input.mp4 -an output.mp4

'an'means no audio recording.

Thşs command will undo all audio-related flags.

9. Removing video stream from a media file

If you don’t want a video stream, you can easily remove it from the media file using'vn'flag. vn stands for no video recording. This command converts the video media file into an audio file.

The following command will remove the video from the media file.

$ ffmpeg -i input.mp4 -vn output.mp3

You can also specify the output file’s bitrate using'-ab'flag as shown in the following command.

$ ffmpeg -i input.mp4 -vn -ab 320 output.mp3

10. Extracting images from the video file

You can easily extract images from a video file. This is a really useful feature for some use cases.

To extract images from a video file, use the following command:

$ ffmpeg -i input.mp4 -r 1 -f image2 image-%2d.png

Here,

  • -r– Set the frame rate. I.e the number of frames to be extracted into images per second. The default value is25.
  • -f– Indicates the output format i.e image format in our case.
  • image-%2d.png– Indicates the name format of extracted images. In this case, the names should start likeimage-01.png,image-02.png,image-03.pngand so on. If you use%3d, then the name of images will start likeimage-001.png,image-002.png..

11. Cropping videos

FFMpeg allows us to crop a given media file in any dimension.

The syntax to crop a video file is given below:

ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4

Here,

  • input.mp4– source video file.
  • -filter:v– Indicates the video filter.
  • crop– Indicates crop filter.
  • w– Width of the rectangle that we want to crop from the source video.
  • h– Height of the rectangle.
  • xxcoordinate of the rectangle that we want to crop from the source video.
  • y– y coordinate of the rectangle.

For example, you want to have a video with awidth of640pixelsand aheight of480pixels, from theposition (200,150), the command should be like that:

$ ffmpeg -i input.mp4 -filter:v "crop=640:480:200:150" output.mp4

12. Converting a specific duration of a video

You may want to convert only a specific duration of the video file to a different format. The following command will convert thefirst20secondsof the video.mp4 file to video.avi format.

$ ffmpeg -i input.mp4 -t 20 output.avi

We specified the time in seconds. It is also possible to specify the time in hh.mm.ssformat.

13. Setting the aspect ratio to the video file

You can set the aspect ratio to a video file using-aspect like below.

$ ffmpeg -i input.mp4 -aspect 16:9 output.mp4

14. Adding poster images to media files

You can add the poster images to your files so that the images will be displayed while playing the audio or video files. Usually, it is used for audio streams.

$ ffmpeg -loop 1 -i inputimage.jpg -i inputaudio.mp3 -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest output.mp4

15. Trimming a media file using start and stop times

To trim down a video to smaller clips using start and stop times, you can use the following command.

$ ffmpeg -i input.mp4 -ss 00:00:50 -codec copy -t 50 output.mp4

Here,

  • --s the starting time of the video clip.
  • -t the total time duration.

You can trim down the audio file like that.

$ ffmpeg -i audio.mp3 -ss 00:01:54 -to 00:06:53 -c copy output.mp3

16. Splitting audio/video files into multiple parts

In some use cases, it is needed to split the file.

$ ffmpeg -i input.mp4 -t 00:00:30 -c copy part1.mp4 -ss 00:00:30 -codec copy part2.mp4

-t 00:00:30indicates a part that is created from the beginning of the video to the 30th second.-ss 00:00:30indicates the starting time for the next part. In this example, the second part starts in 00:00:30 and lasts until the end of the video.

17. Merging multiple audio or video files into one

You can also merge multiple video files and create a single video file.

Createjoin.txtfile that contains the exact paths of the files that you want to merge. All files should be in the same format. The path name of all files should be mentioned one by one like this.

file /home/sk/videos/part1.mp4
file /home/sk/videos/part2.mp4
file /home/sk/videos/part3.mp4
file /home/sk/videos/part4.mp4

Merge all files using this command:

$ ffmpeg -f concat -i join.txt -c copy output.mp4

You might get an error like this;

[concat @ 0x555fed174cc0] Unsafe file name '/path/to/mp4'
join.txt: Operation not permitted

Add"-safe 0":

$ ffmpeg -f concat -safe 0 -i join.txt -c copy output.mp4

This command will mergepart1.mp4,part2.mp4,part3.mp4, andpart4.mp4files into a single file named"output.mp4".

You can also use the following command to merge all files in a directory. Go to the directory where you have files and run the following command to merge the files namedaudio1.mp3,audio2,mp3andaudio3.mp3intooutput.mp3.

$ ffmpeg -i "concat:audio1.mp3|audio2.mp3|audio3.mp3" -c copy output.mp3

18. Adding subtitles to a video file

You need to download a subtitle for your file and add it to the video file by using this command

$ fmpeg -i input.mp4 -i subtitle.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast output.mp4

19. Play video or audio files on the terminal

You might want to see the preview of your video or test it. You can play it on your terminal with this command:

$ ffplay video.mp4

You can also play the audio files with this command.

$ ffplay audio.mp3

20. Increase or decrease video playback speed

You can adjust the video playback speed.

To increase it, you can use this command:

$ ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" output.mp4

The command will double the speed of the video.

To slow down your video,  use a multipliergreater than1.

$ ffmpeg -i input.mp4 -vf "setpts=4.0*PTS" output.mp4

21. Increase or decrease audio playback speed

Use the"atempo"audio filter. The following command will double the speed of the audio.

$ ffmpeg -i input.mp4 -filter:a "atempo=2.0" -vn output.mp4

Get further information about FFmpeg

Of course, the best place to get information about FFmpeg is ffmpeg.org. Here are some specific links to get further information

To sum up, in this post I have tried to answer the question of what is FFmpeg and make a simple introduction to FFmpeg. Firstly, I have described what FFmpeg is, secondly I have told how to get FFmpeg and given some simple examples and lastly, I gave a few links to get further information about FFmpeg. In addition, I plan to write a second post about how to use FFmpeg and give more examples in that post.

Categories: Tutorial

chatsimple