Here’s my personal notes (wordpressed up) on reducing video file size using tools on an Ubuntu Linux Distro.
Last post (here) I showed how to change the format of video files from (say) mp4 to (say) mpg. That’s all fine for converting files. But what if you want to crush down your mp4 or mpg file and make it smaller?
Reducing the size of a video file is usually a trade off with quality. You may shrink the file but lose resolution. But this might not matter. If the original is from a low resolution source you will not lose any significant information by reducing it. There is an example below.
It’s a bit like images in a web browser. There no point in having a stupendously high resolution image on a webpage. All it will do is slow down the load time. It will not look any better than the optimum sized image that may be ten times smaller.
Of course you may also be happy to have a smaller file even if you lose some quality. It depends what your constraints are.
But whatever the reason reducing the size of a typical mp4, mpeg or avi (etc) file is straightforward. My personal preference is to use a command line tool called ffmpeg on my Ubuntu Linux distro.
Here is a typical ffmpeg operation that can result in a file shrinkage of about 50%.
ffmpeg -i inputfilename.mp4 -c:v libx264 -preset slow -crf 22 \ -c:a copy outputfilename.mp4
If the command throws an error stating that you don’t have libx264 installed then either go to the Synaptic package manager, search on libx264 and install libx264-142 or alternatively install it from the command line like so:
sudo apt-get install libx264-142
Reducing Video File Size using CRF
The CRF stands for Constant Rate Factor. The “22” CRF value in the ffmpeg command above (sort of) halves the size of an existing video without affecting its physical size.
IMHO you won’t notice much loss in resolution. If you up the CRF value from “22” to “24” you will shave off another 5% but the quality noticeably drops.
The CRF value can be set from 0 (no change) to 50 (utterly crap)
You can do the same with avconv (ffmpeg and avconv (I think) are simply alias’s of each other).
avconv -i inputfilename.mp4 -c:v libx264 -preset slow -crf 24 \ -c:a copy outputfilename.mp4
Reducing Video File Size by Scaling
You can also scale the video size. Reducing the size has a very marked affect on the size of the final video. Here is an example for going from an avi to an mkv file and changing size to 720 width. The -1 scales the vertical in proportion to the width.
ffmpeg -i input.avi -filter:v scale=720:-1 -c:a copy output.mkv
Here’s what you need to do to convert an avi file into a scaled mp4 file
First convert it to mp4
ffmpeg -i inputfilename.AVI -strict -2 outputfilename.mp4
now change the size to something that is easily displayed on a wordpress site
ffmpeg -i outputfilename.mp4 -filter:v scale=360:-1 \ -c:a copy outchangedsizefilename.mp4
You can play around with a mix of all these commands to reduce the crf and scale to get something that works for you. Personally I always do a three stage process.
I convert to an mp4 file.
Then I play around with the crf value to get the size down (usually a useless ritual – just use 22)
Then I scale it to an appropriate size – this reduces the size further.
Here is an example of a file of two Badgers playing that I reduced from a 34Mbyte AVI file to a 600Kbyte mp4 file.
The video was filmed at night on (I think) a phone by a friend of mine. So the reduction in CRF and size makes little to no difference to the perceived quality of the final video. But hosting a 600KByte video on a shared hosting website is viable. Hosting a 33Mbyte video is not.