Popcar's Hub

File Compression Is Awesome: A Practical Guide


Table of Contents


Introduction

Behold: A picture of a European Bluebird I lifted off the internet

I've recently been in a bit of a rabbit hole regarding file compression, and I came out of that hole with a revelation: compressing things is really neat, and a lot easier than people might expect. Up there are two of the same picture: One is a 2.8MB jpg image, and the other is a 295KB webp image (roughly 10% of the original size)!

They're both the same resolution and have almost indistinguishable quality (zoom in if you don't believe me). The original picture has a huge resolution, so I can probably get away with making it a tad smaller by resizing it as well (with a few minor changes along the way).

This image is just 55KB, a whopping 2% of the original image.

I know this isn't news for some, but in this post I'll share a practical guide to how I'm compressing my files to save on local and cloud storage. It's also a good habit to get into considering how often I share things online, and occasionally bumping into Discord's file limits.

I'm also not going to explain how compression works since that would take forever. Let's just say it's magic.


Compressing Images

Luckily, images are the easiest of the bunch. There's a great web-app called Squoosh which makes image compression a breeze. On the right side of the screen, you can choose a file type and mess with a bunch of intuitive sliders to get the image exactly where you need it to be.

Squoosh shows you a side-by-side comparison, which is very useful.

You can also compress the left side of the image to compare different file types and compression algorithms.

In my experience, the best is WEBP by far. WebP is supported by every browser at this point and is quickly becoming supported everywhere else. If for whatever reason you can't use webp, then MozJPEG is probably your second best choice. Just know that it can be pretty crusty when you try to compress heavily.

Yup. Looks good to me.

There are other good image formats like AVIF and JPEG-XL that can get even better image quality for smaller file sizes, but they're not supported that much so I wouldn't recommend it.

The biggest problem with Squoosh though is that you can only do one image at a time. Squoosh was made by Google, and they actually had a command line tool that made compressing batches of images really simple. Unfortunately, Google did what they do best: cancel squoosh-cli and move the team to different, slightly less cancelled projects.

Thankfully Squoosh is open source, and a brave soul called frostoven has a fork with the command line tool still intact. I'll spare you the technical details since that GitHub has all the info you might need. When you do have it set up, it's super simple to use. It even has an auto function to estimate the best settings, which is usually good enough!


Compressing GIFs

STOP USING GIFS. I MEAN IT. STOP IT.

Okay, I'm fine now. Gifs are a disaster that should have gone extinct fifteen years ago, but I still find them in the wild and am forced to download tons of megabytes to see a crusty poorly compressed 5 second animation running at 10 frames per second.

They're definitely dying, websites like Giphy don't even use gifs anymore, they're actually just autoplaying WEBMs. Unfortunately, sometimes you still just have to use gifs since some websites demand it, like itch.io which I use to publish my games.

For gifs, use ezgif.com. It's a website that has all sorts of tools to help you reduce the file size of your gif. There aren't many tricks to compress a gif without crushing the quality, but here are a few pointers:

If you don't have to use gifs and aren't tied by any rules, you should either use an autoplaying video with the controls hidden or use a much better format that's gaining a lot of traction and actually has a chance to run over gif in the next few years.

What format? I'm glad you asked. It's AVIF! You might've seen it in Squoosh, but AVIF can actually be used for moving images too, and it's very optimized for it. I did say that it's barely supported anywhere, and that's true (my image format even supports JPEG-XL but not AVIF)... However... Starting from January 2024, every major browser now supports AVIF, so it's only a matter of time before you start seeing them around.

I have a 3 second gif that's 3.2MB large. Converting it to AVIF using the gif to avif ezgif converter, it comes out at only 42KB!

If you see this, your browser is AVIF ready. If not, you should update your browser.

I really hope support for this format kicks off, it's really convenient to finally have a drop-in replacement for gif that isn't awkwardly using a video. The image format itself isn't half bad, too.


Compressing videos

Compressing videos needs a bit more knowledge than images, but for this there's a great program called Handbrake. If you've dabbled in the video world, you probably already use this. Note that websites like YouTube compress your videos automatically so you don't always need to compress them yourself.

Handbrake uses FFMPEG under the hood, which is the holy grail of video conversion and compression. It's just a GUI that allows you to use ffmpeg without being in eternal suffering.

There are a lot of buttons and sliders and advanced settings you can mess around with, but generally you just want to pay attention to two things: The preset (1) and quality slider (2).

Handbrake may look busy, just pay attention to these two.

Click on "Open Source" at the top left to choose your video file, then choose a preset (#1 in the picture). The preset sets up all the settings to fit whatever you're trying to do, and gives you an estimated file size. If you're sharing a video on the internet, I highly recommend using one of the Web presets. If you're optimizing for file size without sacrificing quality too much, use one of the Matroska H.265 presets. The framerate and resolution is up to you.

The quality slider over at the video tab (#2 in the picture) dictates how good your video will look. Moving it to the left results in more artifacting (resulting in a blurry video), and moving it to the right results in better quality (but larger files). Somewhere in the middle is usually preferable, but you should just try it and tune until you're satisfied. The handbrake documentation recommends somewhere between 18 and 28. If the quality isn't to your liking (the web presets can be rough sometimes), try a different preset like the general ones.

One last thing to mention is a big slider in the middle also called "Preset". Making this slow or very slow will result in better quality but make compression take longer. You should always set this to as slow as you can, so I usually just leave it at slower.

Hit start at the top and you're done! Everyone will thank you for not sharing your 12 gigabyte 4K 60FPS camera footage. Why were you recording at such a high resolution anyways?

Anyways, here's an example clip of me playing Dr. Robotnik's Ring Racers. It was recorded at 720p and resulted in a 60MB file, and now it's down to 7MB at 480p.

Gotta go fast...

Compressing Everything Else

You might think compressing every other file would just mean putting them into a zip archive... And you're not wrong, but there's a bit more nuance to it.

Even though there are different archive formats like zip, rar, 7z, etc, they can also be compressed using different algorithms. Different algorithms might be better depending on the files you're looking to compress.

KDE's archive manager lets you choose.

For creating file archives, I highly recommend 7-Zip (the program, not the file type with the same name). Other programs like Winrar (people still use that?) may support the same file types, but 7zip often compresses the best and has more features.

Update: Alternatively, PeaZip is also a solid choice.

.zip formats are the most universally supported, but the best compression actually comes in the .7z format since it supports the LZMA2 compression method, which is probably the best one for general use. The difference between a regular zip file and an optimized 7zip file can be huge.

For example, compressing a 44MB folder using the default zip settings comes out at 16.4MB. Compressing that same folder using 7zip with LZMA2 at max compression comes out at 9.1MB. Your mileage may vary depending on what exactly you're compressing, but in general compression type really does matter.

Anyways, let's get into it. Using 7-zip, right click your folder and click "Add to archive..."

Just focus on the first 3 options.

I often use this to compress old personal files to back them up in the cloud, or compress other things to move onto my external hard drive like Steam games. You can save a lot of space just by archiving your files.

Summary

Compressing files might seem like a daunting task, but once you get accustomed to the tools it becomes quick and easy.

And there you have it. Hopefully you, too, can reduce the file sizes being shared around in chat rooms and emails. My limited mobile internet will thank you.


Liked the article? Consider dropping me a few dollars at ko-fi!