Compressing pictures and videos

Preamble

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:

Compressing .jpg images

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:

Size(bytes)Size(MB)Methods used
88721228.5MNone (=input file)
88721228.5Mjpegoptim
14072251.4Mmat2
13025161.3Mmat2 + jpegoptim

As you can see the optimization alone didn't accomplish much, but if you clean up the metadata most of the filesize is gone!
An important detail here: Using jpegoptim BEFORE the metadata removal doesn't optimize the file at all, only afterwards jpegoptim reduced the filesize by 0.1MB.
An example with a different folder of Megumi Koneko's shooting pictures:
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.

TL;DR: If you have studio-quality pictures (or shooting pictures) removing the metadata can save a lot more disk space than only using jpegoptim.

Compressing .png images

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.png
Explanation 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).

With this the filesize went from 728KB to 189KB (!).
A comparison of both of these files (left unoptimized, right optimized):


Barely any noticeable difference between the two, while the filesize is more than halfed!

Compressing videos

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.mp4
This once lowered the filesize of a 4K 30min video from 3.2GB down to 800MB (!).

Another example would be NVidia's Shadowplay function: I sometimes clip gameplay videos that are 1 minute long, are in FullHD resolution and in 60fps. These clips are 330MB for one minute of video. If I simply run ffmpeg -i "Gameplay 03.02.2022.mp4" -crf 25 _gameplay_20220203.mp4 it reduces the filesize down to 100MB or even as low as 30MB with games like Phasmophobia!
An example:
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

Using ffmpeg to either optimize or compress the video to sizes you think are reasonably enough is a great way to save a lot of storage space.