This article explains how to reduce the filesize of videos and pictures.
This is not an analysis about the different compression methods or how they compare in their algorithm against each other.
This is a simple comparison of my own experiments and methods on how to reduce filesizes.
To install the tools mentioned here:
I tried to compress the following image of Megumi Koneko with jpegoptim:
My findings: The metadata of image files can be HUGE. I tried to compress the image with jpegoptim but the filesize wouldn't change much. Then I tried to remove the Metadata of it and it reduced the filesize by an immensely 80%!
The following table shows the filesize of the image in corrolation to what commands were ran:
|8872122||8.5M||None (=input file)|
|1302516||1.3M||mat2 + jpegoptim|
user@fedora:~/temp$ du -sh Rurika_HD_Set/ 469M Rurika_HD_Set/ user@fedora:~/temp$ mat2 --inplace Rurika_HD_Set/* user@fedora:~/temp$ du -sh Rurika_HD_Set/ 77M Rurika_HD_Set/ user@fedora:~/temp$ jpegoptim Rurika_HD_Set/* [...] user@fedora:~/temp$ du -sh Rurika_HD_Set/ 72M Rurika_HD_Set/I saved nearly 400MB (!!) of my 470MB collection simply by removing metadata.
The same concept as above applies for .png images aswell: Use mat2 to remove metadata and then use pngquant to optimize the image.
As an example here is a character of the mobile game "Arknights" with a transparent background.
To compress the image I used the following command:
pngquant -v --skip-if-larger -s 1 -f --ext .png filename.pngExplanation of the command above: -v makes it verbose, skip-if-larger is self-explanatory, -s 1 means "brute-force the optimization as good as possible", --ext .png sets the output filename to be the same as the input filename and -f forces the output file to overwrite the input file (because they have the same name thanks to --ext .png).
Videos are optimized a bit differently than images. You mainly use the tool ffmpeg to run as many commands as you need.
To simply compress a video you can use ffmpeg -i "input.mp4" -crf 25 output.mp4. crf means "constant rate factor" and the higher you set it the more compressed the videos get. 25 is a good number for compression vs quality, and the video quality is always acceptible with it.
An example to compress an old video further: You can change the codecs to more modern ones. Simply add -vcodec h264 -acodec aac inside the ffmpeg command to change the codecs to h264/aac respectively. (This would make the command look like ffmpeg -i "input.mp4" -crf 25 -vcodec h264 -acodec aac output.mp4)
If the framerate of the video is 60 you could lower it to 30 to save even more space. Simply add -r 30 inside the ffmpeg command to limit the framerate to 30.
Last but not least: Some videos have an extremely high resolution (e.g. 4K) and you can save a LOT of space if you limit the resolution to a lower one. Simply add -vf "scale=1280:720" to set the resolution of the output video to HD.
With all the optimizations combined the command would look like:
ffmpeg -i "input.mp4" -crf 25 -vcodec h264 -acodec aac -r 30 -vf "scale=1280:720" output.mp4This once lowered the filesize of a 4K 30min video from 3.2GB down to 800MB (!).
user@debian:~/Videos$ ls -ahl -rwxrwxrwx 1 user user 341M Jan 22 16:41 'divinity2_20220122_16.mp4' user@debian:~/Videos$ ffmpeg -i "divinity2_20220122_16.mp4" -crf 25 _divinity2_20220122_16.mp4 user@debian:~/Videos$ ls -ahl _divinity2_20220122_16.mp4 -rwxrwxrwx 1 user user 54M Feb 3 10:31 _divinity2_20220122_16.mp4