If you’d ask anyone which is the most popular software project or library to handle audio and video processing. Chances are that most of them would mention FFmpeg. It is widely used for tasks such as transcoding, packaging, video playback, basic editing, video scaling, etc. It is one of the most popular tools used for video and image processing. Many video platforms use FFmpeg as part of their project.
What is FFMPEG?
FFmpeg is a free and open-source software project consisting of different libraries and programs for handling video, audio, and other multimedia files and streams. FFMPEG is a command-line tool used for processing video and audio files. It is widely used for format video transcoding, basic editing (trimming and concatenation), video scaling, and video post-production effects.
How to Install FFmpeg on Windows?
Installing FFMPEG is fairly straightforward. You can head to this link to download from gyan.dev.
For a much detailed installation guide, you can see the instructions in our blog on setup and install FFMPeg on Windows.
You can even watch this video and follow instructions on how to install FFMPEG on Windows.
How to install FFMpeg on Mac?
- Go to https://ffmpeg.org/download.html and click the Apple logo in the “Get packages & executable files” section.
2.Click “Static builds for macOS 64-bit”.
3.You’ll see two options for downloading ffmpeg. Choose the one with the shorter filename; this will look like ffmpeg-<versionNumber>.7z, where <versionNumber> is something like 4.3.1.
- When you extract the file and click on the file, you’ll see this message.
- You will have to go to the system preferences and go to the general tab
You can also install FFmpeg using homebrew on mac. This is the mac installation program using homebrew
brew install ffmpeg
What Are The Uses of FFmpeg ?
It is used for a wide range of tasks such as video transcoding, packaging,and video playback. Various software projects use FFmpeg and it’s libraries are currently the part of several software projects, such as VLC, YouTube, and more.
These are the popular use cases of FFmpeg:
You can use FFmpeg for video processing tasks such as denoising, rotation, extraction of frames, blurring, color conversion, letter-boxing, etc
It provides excellent support for video compression. Its an open secret that most streaming companies use or have used FFmpeg for their production systems
Video Packaging Support
For OTT or eLearning platform providers or developers, FFmpeg also has complete support for packaging your videos in both HLS and MPEG-DASH protocols. It can also be configured to stream the videos using RTMP or other protocols.
Audio and Video Container Support
FFmpeg also has extensive support for containers and can be used to read, write, and convert between containers such as avi, mp4, mp3, wma, wav, ts, flv, mkv, and so many other obscure formats.
How To Use FFMPEG?
Getting File information from a video file
To access all the file information including metadata of a video you can use the following command.
|ffmpeg -i sample.mp4|
Cut/Trim a video file
You can trim a video starting from a specific time using ffmpeg,To trim a video using ffmpeg use the following command.
|ffmpeg -ss 00:00:05 -i sample.mp4 -to 00:00:10 -c:v copy -c:a copy trim_sample.mp4|
This is used to seek the video to time from where you want to start the trimming.
You can specify the duration of the required clip using the -t parameter.
You can specify the end-time using the -to parameter.
Resize the video file
You can resize a video using the below command, -s is used to resize the video in below command.
|ffmpeg -i sample.mp4 -s 640×480 -c:a copy resizedSample.mp4|
Split a video into multiple parts
Using FFMPEG you can split a large video file into smaller parts. You can use below command to split a video
|ffmpeg -i sample.mp4 -t 00:00:30 -c copy fragment1.mp4 -ss 00:00:30 -c copy fragment2.mp4|
In the above command -t 00:00:59 represents a part that is created from the start of the video to the 30th second of the video. -ss 00:00:30 shows the starting time stamp for the video. It means that the 2nd part will start from the 30th second and will continue up to the end of the original video file.
Convert images into a video sequence
The below command will transform all the images in a directory to a video file.
|ffmpeg -framerate 30 -i filename-%03d.jpg output.mp4|
Convert a video to x images
The below 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, SGI.
|ffmpeg -i video.mpg image%d.jpg|
Convert a video file from one format to another format
This command will convert a video file from one format to another.
|ffmpeg -i sample.wmv-c:v libx264 sample.mp4|
Crop a video file
FFMPEG provides a crop parameter for specific purposes.
|ffmpeg -i sample.mp4 -filter:v “crop=out_w:out_h:x:y” output.mp4|
- out_w is the width of the output rectangle
- out_h is the height of the output rectangle
- x and y specify the top left corner of the output rectangle
- output.mp4 is the output file
Resize a video
To resize a video to desired size you can use -vf parameter.
|ffmpeg -i sample.mp4 -vf scale=320:240 sample.mp4|
Extracting audio from a video file
Use the below command to extract audio from a video file.
|ffmpeg -i sample.mp4 -vn -ab 128 audio.mp3|
Here -vn is used to extract audio and -ab is used to save audio as a 128Kbps MP3 file. You can change the bitrate to 256Kbps or something else. Just change the value after -ab.
Mute audio in a video file
The below command will mute audio in a video file.
|ffmpeg -i sample.mp4 -an mutesample.mp4|
Adding Poster image to video file
Adding a poster image with FFMPEG is an easy task, The below command will add a poster to a video file.
|ffmpeg -i video.mp4 -i image.png -map 1 -map 0 -c copy -disposition:0 attached_pic out.mp4|
Add text subtitles to a video
Using FFMPEG it is easy to add subtitles to a video file. The below command will add subtitles to a video file.
|ffmpeg -i input.mp4 -i subtitles.srt -c copy -c:s mov_text output.mp4|
With FFmpeg, you can easily convert videos file without worrying about picking the right format and container. FFmpeg automatically selects the right codec and container without any need to configure it.
Let’s say you want to convert an mp4 file to avi file, you can easily do so :
ffmpeg -i original.mp4 convert.webm
This command takes the mp4 file called original.mp4 and converts it to the WebM file called convert.webm. In this case, as WebM is a well-known video format, FFmpeg already knows which video and audio stream it supports and thus will convert the streams into a valid WebM file.
Although FFmpeg might not pick the right container you’d need every single time. In the case of containers such as Matroska, it might result in the converted file using the same codec. For example, let’s take this case.
ffmpeg -i original.mp4 output.mkv
Here the file might have the same codec as the original video, which in certain cases might not be what you want.
Changing the Quality of the Video File
With FFmpeg you can change the video bitrate and frame rate of the input file as well
You can use the following command to change the video bitrate of the output file to 64 kbit/s:
ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi
And use this command to restrict the frame rate of the output file to 30 fps:
ffmpeg -i input.avi -r 30 output.avi
You can also adjust the dimensions of your video using FFmpeg. The simplest way is to use a predetermined video size:
You can even change the dimensions of yout videos using FFmpeg, one of the ways it to use a standard video size
ffmpeg -i inital.mkv -c:a copy -s hd720 final.mkv
This changes the video to, 120×720, you can even change the width and height manuall as well.
ffmpeg -i inital.mkv -c:a copy -s 1280×720 final.mkv
This command acts the same as the earlier one, just be wary of the fact that width always comes before height.
Selecting your codecs
With FFmpeg, you can select any codec you want by using the -c flag. With this flag, you can set different codecs for different streams. So in case you want to set the audio stream to Vorbis, you can use the following command:
Ffmpeg -i original.mp4 -c:a libvoris convert.mkv
If you want to change the video stream along with the audio stream. You can do this as well. Here it’ll make a Matroska container with a VP9 video stream and Vorbis audio stream using this command:
Ffmpeg -i original.mp4 -c:v vp9 -c:a libvorbis convert.mkv
The command FFmpeg -codecs will print every codec FFmpeg knows about. The output will change according to the FFmpeg version you have.
Changing a single stream
More often than you’d like, the file you have is partially correct with only a single stream in the wrong format. It can be very time-consuming to re-encode the correct stream. FFmpeg can help with this situation:
At time there might be some cases where the file might have a single stream which is in the wrong format. Re-encoding it completely can be time consuming, at times like this ffmpeg comes to your rescue:
ffmpeg -i initial.webm -c:v copy -c:a flac final.mkv
This command takes the same video stream from initial.webm into final.mkv and encodes the audio stream into FLAC.
Changing a container
You can use the above command to allow you to use the same audio and video streams for the different container format without any need to additional encoding
ffmpeg -i initial.webm -c:av copy final.mkv
FFmpeg is a great library to have in your video processing and compression toolbox. FFmpeg has endless capabilities and being open-source, you are more than welcome to modify the source and extend it’s capabilities.
Supercharge Your Business with Videos
At VdoCipher we maintain the strongest content protection for videos. We also work extremely hard to deliver the best viewer experience. We'd love to hear from you, and help boost your video streaming business.
Hi. I’m Shubham. I make jokes when I’m uncomfortable.