Tutorial Powerful commandline tool for Multimedia files which you need to know

Using ffmpeg to multimedia flles.png

Image from Pexels ( NC )
Overview
I made this blog post begineer friendly as much as possible and took a lot of time to explain and write things with real world practical examples.
In this blog post, we will get to know about ffmpeg which is a popular cli tool to process multimedia files. Command-line is much faster and easier if you get familiar with it. You can start using Linux. Take a look at my Linux Basics post if you want to. You can do almost what you need easier and more faster. We all came across using some 3rd party GUI software to convert video files, trim them, trim audio files, resize image files. What If I tell you can do them in just few seconds without relying on any web service and hard steps.

FFMPEG
As I've mentioned eariler, we are going to use ffmpeg to do this. You can get ffmpeg from their Offical download page or if you're using Linux. Then you can install it by your desired package manager. Most commands works regardless of OS/Environment. But, some of the commands need to be executed on specific OS/Environment. I will try to mention the alternative method if possible. Now, you've got a basic view on what's ffmpeg. Let's jump on to some use cases and how to use it.

How to use it ( Basics )
Understanding the syntax and basic arguments
ffmpeg takes 2 important arguments. -i and output . Where it process or converts the file to another format. The example usage would look like this.
Bash:
ffmpeg -i {input_file} {output_file}

1. Converting Video files
This is same in all environment. But, you have to replace ffmpeg with ffmpeg.exe if you're using Windows.

As I've mentioned eariler. We are going to give it the input file and the output file name.
Bash:
ffmpeg -i "Memorable Moment.avi" "Memories.mp4"
If you execute the above command. it will convert the file named "Memorable Moment.avi" to "Memories.mp4"

Example Use Cases
1.1. Converting Big Avi Files to Mp4 to save space

1630484156115.png

Conversion of an avi file to mp4. Reduced almost 1.795 GB.

Not just avi to mp4. You can do alot of conversion type. You can check their about or website to check the supported file types. You don't have to configure through GUI screens to change the format to export or convert to. Just specify the file extension and the tool will take care of it.

1.2. Converting video files/song to mp3
Yea, I can hear you want to convert video files to audio format to play them in background or in a audio player to save space and lesser file size. You can do that too.
Bash:
ffmpeg -i "The Score  Unstoppable.mp4" "Unstoppable.mp3"
As you might have guessed. Yes, we have converted a mp4 to mp3.

2. Converting Image files
As you already know. We can specify input as an image file and then output to other image file format.
Bash:
ffmpeg -i sample.webp output.png
This above command will convert a webp image to png. Cause, some websites/services don't support profile pictures in webp format. You can specify the format as file extension to convert files.. eg. jpg, jpeg, tiff, jfif etc.,.

Example Use Cases
2.1. Resizing Image for profile or to meet requirement.

In some websites, you need to upload images in the required size like a profile in your document must be in the pixel size they provide.
Syntax :
Bash:
ffmpeg -i {input} -vf scale={width}:{height} {output}
You can replace height and width as you want.

2.1.1 Custom values ( doesn't preserve aspect ratio )
To show how it looks, I converted the High quality SupremeGamers logo to 40x40. This would work if the input image is a perfect square. If it is not, then you can use the next method for that.
Bash:
ffmpeg -i SG_logo.png -vf scale=40:40 converted.png

Output :
1630486505442.png

SG_logo.png is the original hq file. As you can see, converted.png is stretched a bit horizontally.
2.1.2 Auto value ( Preserves aspect ratio )
Here, it becomes more handy. You can specify -1 in height or either width to make it adjusting to the value you provided. If you use 50:-1. It will get the calculate the height by itself using the aspect ratio of the image. -1:100 do the same since you understood how it works. -1 gets the value according to the aspect ratio.
Bash:
ffmpeg -i SG_logo.png -vf scale=40:-1 converted_preserve.png
You can use if this if you are unsure about if the image is a perfect square or it's random landscape image.

Output :
1630486863420.png

You can see now the converted_preserve.png is identical to the Original one. It's not streched like the previous one. It preserves the aspect ratio.
2.2 Converting image to make it lesser in size and usability
Like TIFF files are larger in size. You can convert them to save space in your disk. Meanwhile JPEG is the lightest one. You can use ffmpeg to do that easily. Some un-popular file formats will not be supported on some sites/apps. You can convert them to png or jpg to make them work.

3. Audio
The same things goes for audio. File input and file output if you want to convert. I'll tell you the ad steps to trim, crop, batch convert them later on.

3.1 Changing Audio Bitrate ( quality )
Syntax:
-ab followed by space and audio bitrate
Bash:
ffmpeg -i "blackbear my worst Lyrics.mp3" -ab 64 low_quality.mp3
Initially, the quailty of the file was 128Kbps. Now, we converted it to 64Kbps for example

Output:
1630487766933.png

First file is the original mp3 and the second one is converted.

Advanced Usage
In this part we'll cover about batch convert, trimming, cropping specific clip or audio from video and audio files. The syntax might get harder. It's better to know about bash for the loop part.

1. Batch/Bulk Convert [ Linux ]
We can use bash loop or find to do this. Follow the syntax and commands carefully to understand. Before going to that. You can get some information on what we're going to use.

Understanding Wildcards ( * ) :
Wild cards are like auto fill. Here we use ( * ) star wildcard. Basically you can call it everything. If you input *.mp4, As we've mentioned before *.mp4. It will fetch all the files ending with .mp4. Same goes for all. We can use this to filter through file types or file names. Like if you have images from a Professional camera and just want to convert that. You can specify DSC_ to copy files start with DSC. And If you want to check both sides. You can use a syntax like this Blackbear-*.mp4 to get all the files named in this format.. e.g Blackbear-IDFC.mp4, Blackbear-Myworst.mp4. I hope you can understand the concept behind this. Don't worry if you can't. You can read it again to better understand.

Loops ( for ) :
In bash and some other shells. There is for loop to do a task for every entry/name. We can use wildcards to make it more powerful.

Variables :
In programming and scripting. We can use variable to define a value and get it later on. Eg. if you use
Code:
name="Jack"
It will be stored to $name and when you try to print $name. It will show "Jack" since $name is assigned to "Jack" before.

Example Use case
We have a folder named "Songs" where we have files filled with mkv and mp4 files and some mp3 files too. Now, we want to just conver the mp4 files to mp3 in the Mp3 folder. Can we do that? Yes, We can use wildcard to filter mp4 files and use loops to batch operate them. Let's get started.
1630489108116.png

Doesn't it looks hard to do manually. We got you covered. I know most people don't organize things. But, we can still filter out and convert.

1.1 Using Loop
Syntax:
Bash:
for file in *.mp4;do ffmpeg -i "$file" "Mp3/${file//.mp4}.mp3";done
Here *.mp4 just gets the mp4 files present in the current folder and each file will be processed one by one and the file name will be stored to a variable ( file ) . Then, we will add "Mp3/" so the files will be saved to folder named Mp3. Then, remove the .mp4 part and append .mp3
at the end ${file//.mp4} is substitution. If we don't remove the .mp4 from te files. The filename of the mp3 would look like this.
Dance Monkey.mp4.mp3, Dua Lipa Levitating.mp4.mp3 which doesn't look great.

When being processed, the command will look like this to the computer.
Bash:
ffmpeg -i "Dance Monkey.mp4" "Mp3/Dance Monkey.mp3"

Output :
I stopped converting at 7th file.
This will take some time like 10 or 15 minutes according to the files and the video size.

1630489925046.png

Output of ls. Files present in Mp3 folder. You can see the naming is not affected and looks normal.
Notes :
This isn't just limited to video files. You can replace the wildcard *.mp4 to any other file type that you want to batch convert and same goes for the substitution and the output file name appending. If you want some example.

Code for converting webp files to png in a folder called "Png"
Bash:
for file in *.webp;do ffmpeg -i "$file" "Png/${file//.webp}.png";done

Code for png files to ico by 100xauto for using as icons ( Output to "Icons" )
This example is useful when you want to set custom icon files for apps and folders. Give it a try.
Bash:
for file in *.png;do ffmpeg -i "$file" -vf scale=100:-1 "Icons/${file//.png}.ico";done


1.2 Using find ( binary ) :
find is a linux binary which is available to use in every linux distro. You can use this if you don't want to learn about loops, variables, wildcards and things. Keep everything as simple and use find to do the work.
Bash:
find . -exec ffmpeg -i {} {}.mp3\;
Source stack overflow

2. Trimming Video/Audio files
This will look like a bit advanced. A hard thing which can be done like a piece of cake. Yes, I assume that you've used some paid/3rd party GUI multimedia tool to trim/crop video/audio files. You can do that simply in seconds and just in a line of command. Let's get started.

2.1 Trimming
Have a video/audio which has unwanted or useless things in the ending. You can convert the first part of the video/audio without the unwanted part.

Syntax:
-t followed by time stamp. ( eg. 1:00 or 0:30 etc.,. )

Example use case
I have a video named Color BG.mp4 and I want to get the first 30 seconds of the video and save as another file. We are going to do that.
1630491061533.png

Video that we are going to trim. Duration is 3:00 ( 3 mins ).
Bash:
ffmpeg -i "Color BG.mp4" -t 0:30 output.mp4

Output :
1630491185660.png

Converted Video file. Duration 00:30 ( 30 seconds ).
Note : The same goes for audio files. You can just change the filenames according to you and execute.

2.2 Cropping Specific clip/sound ( e.g 1:10 to 3:30 ) etc.,.
Want to crop a meme you like from a Meme compilation video? Yes, you can do that. But, we need to use a video player as a preview panel in video editor to get the time stamps.

Syntax :
-ss followed by start time stamp -to followed by end time stamp.
Bash:
ffmpeg -i "video.mp4" -ss 1:10 -to 3:30 meme.mp4

Example Use case
We are now going to cut out a meme video from a meme compilation video so we can share on social media.
1630491509008.png

I want to trim this dog meme for example. It starts on 3:50 and ends on 4:09. You need to note the start and end time stamps.
Bash:
ffmpeg -i "Meme Comp.mp4" -ss 03:50 -to 04:09 "Dog Meme.mp4"
This will start from the start time stamp and end on the end timestamp.

Output :
1630491861262.png

Now, I got the dog meme video from the whole meme compilation. So, I can share them on social media.

3. Extracting frames from video
Requested to add by @Xtr . We can extract frames from the video to get sequence of image files. This will become handy if you want to create boot animations or get a specific clicks from a video.

Syntax:
Bash:
ffmpeg -i {input} -vf fps={fps} %0{digits}_output

Note :
fps
mentioned above is optional and you can remove that part. Mostly, boot animations are 24 frames and they will work better. digits is the file name sequence. Image sequences need to be in a format that they must be sort by A-Z format and will be read by the computer in that format. So, the longer the video, the higher the digits should.

%06d.png
The above argument will generate images like this. 000001.png, 000002.png, 000003.png etc.,. Since you know how this works. Don't worry. You can still specify file names and output folder before the name. The %d is an actual variable like parameter and can be inside a string.

Output/Frame_%04d.png
The above argument will generate images like Frame_0001.png, Frame_0002.png, Frame_0004.png etc.,. inside Output folder. So, you can consider it as a variable and use feel free to work with filenames. But, here we are going to use simple digits and we're going to store them in a separate folder to create a boot animation.

Usage :
Now, I want to extract frames from a video called Render.mp4 in 24 fps and in the sequence of 4 digit numbers and specifically save them inside a folder called Frames. Shall we move.
Bash:
ffmpeg -i Render.mp4 -vf fps=24 Frames/%04d.png

Output :
1630560499606.png

Image sequence saved in a seperate folder. The file names follow the digits.

Extra Info
1. My Opinion :
When I was new to Linux. I wish I knew them. I used some online services to the do this above things without knowing that we can do all of them from our commandline in seconds. It's up to you to use this or other software or online services. You can pretty much do things easily if you get familiar with command line.
It took me alot of time to manage things. Write them, order them, organize them, execute code and upload screenshot to make the fully understandable.

2. Is it free?
It's FOSS ( Free and Open Source ). You don't have to buy it. You can use it freely and see how it's developed and you can contribute if you can.

3. Why this blog post?
I just wanted to share my thoughts and things I do. So, people who faced things like me can use this to save their time. If you see any grammatical mistakes or code erros or doubts. Feel free to post a reply and I will try to reply you as soon as possible.
 
Last edited:

Xtr

Active member
View attachment 1498
Image from Pexels ( NC )
Overview
I made this blog post begineer friendly as much as possible and took a lot of time to explain and write things with real world practical examples.
In this blog post, we will get to know about ffmpeg which is a popular cli tool to process multimedia files. Command-line is much faster and easier if you get familiar with it. You can start using Linux. Take a look at my Linux Basics post if you want to. You can do almost what you need easier and more faster. We all came across using some 3rd party GUI software to convert video files, trim them, trim audio files, resize image files. What If I tell you can do them in just few seconds without relying on any web service and hard steps.

FFMPEG
As I've mentioned eariler, we are going to use ffmpeg to do this. You can get ffmpeg from their Offical download page or if you're using Linux. Then you can install it by your desired package manager. Most commands works regardless of OS/Environment. But, some of the commands need to be executed on specific OS/Environment. I will try to mention the alternative method if possible. Now, you've got a basic view on what's ffmpeg. Let's jump on to some use cases and how to use it.

How to use it ( Basics )
Understanding the syntax and basic arguments
ffmpeg takes 2 important arguments. -i and output . Where it process or converts the file to another format. The example usage would look like this.
Bash:
ffmpeg -i {input_file} {output_file}

1. Converting Video files
This is same in all environment. But, you have to replace ffmpeg with ffmpeg.exe if you're using Windows.

As I've mentioned eariler. We are going to give it the input file and the output file name.
Bash:
ffmpeg -i "Memorable Moment.avi" "Memories.mp4"
If you execute the above command. it will convert the file named "Memorable Moment.avi" to "Memories.mp4"

Example Use Cases
1.1. Converting Big Avi Files to Mp4 to save space

View attachment 1499
Conversion of an avi file to mp4. Reduced almost 1.795 GB.

Not just avi to mp4. You can do alot of conversion type. You can check their about or website to check the supported file types. You don't have to configure through GUI screens to change the format to export or convert to. Just specify the file extension and the tool will take care of it.

1.2. Converting video files/song to mp3
Yea, I can hear you want to convert video files to audio format to play them in background or in a audio player to save space and lesser file size. You can do that too.
Bash:
ffmpeg -i "The Score  Unstoppable.mp4" "Unstoppable.mp3"
As you might have guessed. Yes, we have converted a mp4 to mp3.

2. Converting Image files
As you already know. We can specify input as an image file and then output to other image file format.
Bash:
ffmpeg -i sample.webp output.png
This above command will convert a webp image to png. Cause, some websites/services don't support profile pictures in webp format. You can specify the format as file extension to convert files.. eg. jpg, jpeg, tiff, jfif etc.,.

Example Use Cases
2.1. Resizing Image for profile or to meet requirement.

In some websites, you need to upload images in the required size like a profile in your document must be in the pixel size they provide.
Syntax :
Bash:
ffmpeg -i {input} -vf scale={width}:{height} {output}
You can replace height and width as you want.

2.1.1 Custom values ( doesn't preserve aspect ratio )
To show how it looks, I converted the High quality SupremeGamers logo to 40x40. This would work if the input image is a perfect square. If it is not, then you can use the next method for that.
Bash:
ffmpeg -i SG_logo.png -vf scale=40:40 converted.png

Output :
View attachment 1501
SG_logo.png is the original hq file. As you can see, converted.png is stretched a bit horizontally.
2.1.2 Auto value ( Preserves aspect ratio )
Here, it becomes more handy. You can specify -1 in height or either width to make it adjusting to the value you provided. If you use 50:-1. It will get the calculate the height by itself using the aspect ratio of the image. -1:100 do the same since you understood how it works. -1 gets the value according to the aspect ratio.
Bash:
ffmpeg -i SG_logo.png -vf scale=40:-1 converted_preserve.png
You can use if this if you are unsure about if the image is a perfect square or it's random landscape image.

Output :
View attachment 1502
You can see now the converted_preserve.png is identical to the Original one. It's not streched like the previous one. It preserves the aspect ratio.
2.2 Converting image to make it lesser in size and usability
Like TIFF files are larger in size. You can convert them to save space in your disk. Meanwhile JPEG is the lightest one. You can use ffmpeg to do that easily. Some un-popular file formats will not be supported on some sites/apps. You can convert them to png or jpg to make them work.

3. Audio
The same things goes for audio. File input and file output if you want to convert. I'll tell you the ad steps to trim, crop, batch convert them later on.

3.1 Changing Audio Bitrate ( quality )
Syntax:
-ab followed by space and audio bitrate
Bash:
ffmpeg -i "blackbear my worst Lyrics.mp3" -ab 64 low_quality.mp3
Initially, the quailty of the file was 128Kbps. Now, we converted it to 64Kbps for example

Output:
View attachment 1503
First file is the original mp3 and the second one is converted.

Advanced Usage
In this part we'll cover about batch convert, trimming, cropping specific clip or audio from video and audio files. The syntax might get harder. It's better to know about bash for the loop part.

1. Batch/Bulk Convert [ Linux ]
We can use bash loop or find to do this. Follow the syntax and commands carefully to understand. Before going to that. You can get some information on what we're going to use.

Understanding Wildcards ( * ) :
Wild cards are like auto fill. Here we use ( * ) star wildcard. Basically you can call it everything. If you input *.mp4, As we've mentioned before *.mp4. It will fetch all the files ending with .mp4. Same goes for all. We can use this to filter through file types or file names. Like if you have images from a Professional camera and just want to convert that. You can specify DSC_ to copy files start with DSC. And If you want to check both sides. You can use a syntax like this Blackbear-*.mp4 to get all the files named in this format.. e.g Blackbear-IDFC.mp4, Blackbear-Myworst.mp4. I hope you can understand the concept behind this. Don't worry if you can't. You can read it again to better understand.

Loops ( for ) :
In bash and some other shells. There is for loop to do a task for every entry/name. We can use wildcards to make it more powerful.

Variables :
In programming and scripting. We can use variable to define a value and get it later on. Eg. if you use
Code:
name="Jack"
It will be stored to $name and when you try to print $name. It will show "Jack" since $name is assigned to "Jack" before.

Example Use case
We have a folder named "Songs" where we have files filled with mkv and mp4 files and some mp3 files too. Now, we want to just conver the mp4 files to mp3 in the Mp3 folder. Can we do that? Yes, We can use wildcard to filter mp4 files and use loops to batch operate them. Let's get started.

Doesn't it looks hard to do manually. We got you covered. I know most people don't organize things. But, we can still filter out and convert.

1.1 Using Loop
Syntax:
Bash:
for file in *.mp4;do ffmpeg -i "$file" "Mp3/${file//.mp4}.mp3";done
Here *.mp4 just gets the mp4 files present in the current folder and each file will be processed one by one and the file name will be stored to a variable ( file ) . Then, we will add "Mp3/" so the files will be saved to folder named Mp3. Then, remove the .mp4 part and append .mp3
at the end ${file//.mp4} is substitution. If we don't remove the .mp4 from te files. The filename of the mp3 would look like this.
Dance Monkey.mp4.mp3, Dua Lipa Levitating.mp4.mp3 which doesn't look great.

When being processed, the command will look like this to the computer.
Bash:
ffmpeg -i "Dance Monkey.mp4" "Mp3/Dance Monkey.mp3"

Output :
I stopped converting at 7th file.
This will take some time like 10 or 15 minutes according to the files and the video size.

View attachment 1505
Output of ls. Files present in Mp3 folder. You can see the naming is not affected and looks normal.
Notes :
This isn't just limited to video files. You can replace the wildcard *.mp4 to any other file type that you want to batch convert and same goes for the substitution and the output file name appending. If you want some example.

Code for converting webp files to png in a folder called "Png"
Bash:
for file in *.webp;do ffmpeg -i "$file" "Png/${file//.webp}.png";done

Code for png files to ico by 100xauto for using as icons ( Output to "Icons" )
This example is useful when you want to set custom icon files for apps and folders. Give it a try.
Bash:
for file in *.png;do ffmpeg -i "$file" -vf scale=100:-1 "Icons/${file//.png}.ico";done


1.2 Using find ( binary ) :
find is a linux binary which is available to use in every linux distro. You can use this if you don't want to learn about loops, variables, wildcards and things. Keep everything as simple and use find to do the work.
Bash:
find . -exec ffmpeg -i {} {}.mp3\;
Source stack overflow

2. Trimming Video/Audio files
This will look like a bit advanced. A hard thing which can be done like a piece of cake. Yes, I assume that you've used some paid/3rd party GUI multimedia tool to trim/crop video/audio files. You can do that simply in seconds and just in a line of command. Let's get started.

2.1 Trimming video
Have a video/audio which has unwanted or useless things in the ending. You can convert the first part of the video/audio without the unwanted part.

Syntax:
-t followed by time stamp. ( eg. 1:00 or 0:30 etc.,. )

Example use case
I have a file named Color BG.mp4 and I want to get the first 30 seconds of the video and save as another file. We are going to do that.
View attachment 1508
Video that we are going to trim. Duration is 3:00 ( 3 mins ).
Bash:
ffmpeg -i "Color BG.mp4" -t 0:30 output.mp4

Output :
View attachment 1510
Converted Video file. Duration 00:30 ( 30 seconds ).
Note : The same goes for audio files. You can just change the filenames according to you and execute.

2.2 Cropping Specific clip/sound from a video ( e.g 1:10 to 3:30 ) etc.,.
Want to crop a meme you like from a Meme compilation video? Yes, you can do that. But, we need to use a video player as a preview panel in video editor to get the time stamps.

Syntax :
-ss followed by start time stamp -to followed by end time stamp.
Bash:
ffmpeg -i "video.mp4" -ss 1:10 -to 3:30 meme.mp4

Example Use case
We are now going to cut out a meme video from a meme compilation video so we can share on social media.
View attachment 1511
I want to trim this dog meme for example. It starts on 3:50 and ends on 4:09. You need to note the start and end time stamps.
Bash:
ffmpeg -i "Meme Comp.mp4" -ss 03:50 -to 04:09 "Dog Meme.mp4"
This will start from the start time stamp and end on the end timestamp.

Output :
View attachment 1514
Now, I got the dog meme video from the whole meme compilation. So, I can share them on social media.
Extra Info
1. My Opinion :
When I was new to Linux. I wish I knew them. I used some online services to the do this above things without knowing that we can do all of them from our commandline in seconds. It's up to you to use this or other software or online services. You can pretty much do things easily if you get familiar with command line.
It took me alot of time to manage things. Write them, order them, organize them, execute code and upload screenshot to make the fully understandable.

2. Is it free?
It's FOSS ( Free and Open Source ). You don't have to buy it. You can use it freely and see how it's developed and you can contribute if you can.

3. Why this blog post?
I just wanted to share my thoughts and things I do. So, people who faced things like me can use this to save their time. If you see any grammatical mistakes or code erros or doubts. Feel free to post a reply and I will try to reply you as soon as possible.
Good ffmpeg tutorial, but how to extract frames from video (for purposes such as creating bootanimation) ? Thanks
 
Top