So recently I almost run out of free disk space on my Google Drive – free 15G was mostly used by some video recorded with my mobile phone. Before purchasing any further space I decided to take a look on how much I could save on the space if I transcoded the video files that other way were not touched by anything since recorded. After while I learned on how to use the ffmpeg tool to achieve this – the results overcame my expectations.
$ffmpeg -i [input video file] -crf 28 -vcodec libx264 -preset veryslow [output video file].mp4
Then to tackle all the files in current directory I wrapped this in a simple FOR LOOP (BASH):
$for F in $(find ./ -maxdepth 1 -type f -iname '*.mp4'); do P=$(basename $F); echo $P; ls -lah $F; ffmpeg -i $F -crf 28 -vcodec libx264 -preset veryslow encoded/$P; done
The command above finds all video files in current directory that have extension *.mp4 and is transcoding all of them using the H.264 library with output being written to “encoded” directory under current location (the directory must exist!).
The transcoding takes significant amount of time so for large files it’s best to leave it to run over the night. Saved space vs. quality loss is brilliant – my files shrieked between 4-8 times and I was not able to observer any quality loss (most of the video files in question was taken with my Nexus 5 at HD quality).
Explanation of the used arguments:
Constant Rate Factor – The range of the quantizer scale is 0-51: where 0 is lossless, 23 is default, and 51 is worst possible. A lower value is a higher quality and a subjectively sane range is 18-28. Consider 18 to be visually lossless or nearly so: it should look the same or nearly the same as the input but it isn’t technically lossless.
The general guideline is to use the slowest preset that you have patience for. Current presets in descending order of speed are: ultrafast,superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo. A slower preset will provide better compression (compression is quality per filesize).
The explanation is taken from here where more information is available.