Sunday, July 31, 2022

FFMPEG Command Line Audio and Video Processing Tool

Sometimes we need to post some videos of our setups, some issues, some behavior on some forums, emails, etc., but the videos recorded from our mobile are too big, so can't be attached to forums and also can't be sent over email, so to overcome this situation, we have found a tool, which is open source and can be used without installing, and this tool can be used to process audio and video files. Not only just size reduction but this can also be used for several other audio and video processing things. 
FFMPEG is a command line tool, below are some of the important commands which are useful for our needs. 



Convert File Format

With FFMPEG we can convert the video file format, like in the below example we are changing a *.avi file into *.mp4 format.
ffmpeg -i input.avi output.mp4

Split a File

ffmpeg -i source.m4v -ss 0 -t 593.3 -c copy part1.m4v
ffmpeg -i source.m4v -ss 593.3 -t 551.64 -c copy part2.m4v
ffmpeg -i source.m4v -ss 1144.94 -t 581.25 -c copy part3.m4v

Reduce the Size by Changing Quantizer Value

This method is applicable only for *.mp4 and *.avi files. There is a factor in image compression called quantizer that actually affects the file quality and it is linked to how it should choose the bit-rate when it comes to variable bit-encoding, for details check on the web.
The important point is smaller the quantizer higher the quality and the higher the quantizer lower is the quality. Higher quality means higher file size and lower quality means lower file size, so by changing this parameter, we can reduce the file size.
ffmpeg -i input.mp4 -q 20 output_q20.mp4
With the above command, we are setting the value of quantizer or quality to 20, I did an experiment where 40MB Full HD video is reduced to 6MB (video quality was still very good).
Note: If the input file is *.avi and the output required is *.mp4 then use -crf factor instead of -q, an example is given below.
ffmpeg -i input.avi -crf 18 output.mp4

Reduce the Size by Changing the Bitrate

The above method works well, but if our requirement is to have finer control in setting the bitrate, then we can also do this with FFmpeg. And this works for both audio and videos, the following are the commands.
ffmpeg -i in_file.mp3 -b:a 320k outputfile.mp3
ffmpeg -i in_file.mp4 -b:v 1000k outputfile.mp4
See the difference between -b:a and -b:v, here is for audio and is for video.
And with the following command, we can adjust the bitrate in a single command.
ffmpeg -i in_file.mp4 -b:v 1000k -b:a 128k outputfile.mp4
There are some filters also available with FFmpeg, check its documentation to have in-depth details, the few most commonly used filters are as below.
  • Audio: Volume
  • Audio: Channel Map
  • Video: Cropping
  • Video: Scale
  • Video: Rotate

Audio: Volume

With this filter we can increase or decrease the volume, the following is the command to double the volume.
ffmpeg -i inputFile -filter:a "volume=2" outputfile

This filter can be used to reduce external noise.

Audio: Channel Mapping

This is not so important for us, but still for documentation purposes adding it here.
ffmpeg -i inputFile -filter:a "channelmap=0-0|0-1" outputFile
Here with the line "channelmap=0-0|0-1" in the above command, we mean that we are mapping the Input Left to Output Left and Input Left to Output Right channel.

Video: Cropping

This is an important filter and with this, we can remove the redundant information from our videos and this also reduces the file size. The following is the example command.
ffmpeg -i inFile -filter:v "crop=w:output_width:h:output_height:x:y" outFile
4-parameters can be specified, i.e output width, output height, and top left corner parameters.
Top Left corner parameters are optional and if not specified, cropping is centered in the frame.
We can also use arithmetic also in these parameters like the following.
ffmpeg -i inFile -filter:v "crop=w=2/3*in_w:h=2/3*in_h" outFile
The variables in_w and in_h are supplied and they stand for input width and input height.

Video: Scaling

Syntax
ffmpeg -i inFile -filter:v "scale=w=640:h=480" outFile
Scaling with arithmetic and variables
ffmpeg -i inFile -filter:v "scale=w=2/3*in_w:h=2/3*in_h" outFile
Proportional Scaling
ffmpeg -i inFile -filter:v "scale=w=640:h=-1" outFile
Put -1 for any of the inputs to get the proportional scaling and its value will be calculated based on the input.

Video: Rotate

Syntax:
ffmpeg -i inFile -filter:v "rotate=45*PI/180" outFile
This is also important for us, as sometimes when videos are recorded from mobiles, the orientation is not proper and with the above command we can change the orientation so that it can be viewed properly.

No comments:

Post a Comment