Termux/Linux – Batch Compression With maxcso

Over the past week, I’ve rediscovered my love for the PlayStation 2, and its handheld counterpart, the PSP. Emulation for both consoles has come a very long way, especially on Android. PPSSPP and AetherSX2 (abandoned) do an excellent job of emulating just about anything I throw at my Pixel Fold. The only problem, though, is storage space. These games range from a few hundred megabytes, to a few gigabytes. Thankfully, though, utilities exist that can compress an ISO dump from one of these consoles, and maxcso is one such utility that can be used across multiple platforms. To my surprise, I found that it can be built and ran on Android, and this is where the inspiration for my script came from.

Feeding files to maxcso individually becomes tedious, and I could really only find Batch scripts for Windows. The only Bash script I came across was very rudimentary. It just used a for loop to look for files with the “iso” extension, and then it passed the path over to maxcso. This is fine (my script does the same thing), except it didn’t ignore the letter case of the extension. I found myself manually renaming extensions before I could compress an image, which felt like it was defeating some kind of purpose. So, with the script I wrote, it ensures that the extension is lowercase every time, and it also prints out before-and-after sizes of compressed files. Without further ado, though, let’s get into the set up.

Building maxcso

As I mentioned, maxcso is a multiplatform utility that can be built across several operating systems. On Linux and Android, you just need a compiler, liblz4-dev, libdeflate-dev, and libuv1-dev. Since I wrote this script for use on Android, I’ll cover the set up needed for Termux, but the instructions are essentially the same for Linux. Just substitute the dependency installation step with one that’s appropriate for your package manager.

$ termux-setup-storage
$ pkg install binutils build-essential git liblz4 libuv git

The first command grants storage access to Termux so we can access our ISO directory, and the second installs our build dependencies. Once done, run the commands below to clone the maxcso repository, and build it.

$ git clone https://github.com/unknownbrackets/maxcso.git
$ cd maxcso && make && make install

That’s it! Now, we get to the script itself.

compress.sh

Below is the script. You’ll want to place it in the same directory that houses your ISOs, but that’s pretty much it. I wrote it to move all ISO files to a temporary location, so you won’t have to worry about having a dedicated directory for compressing images. Also, it’s worth mentioning that it DOES delete the source ISO after compressing it. If you do not want this, then remove lines 23 and 28.

#!/bin/bash
## Batch compress PS2/PSP ISO dumps using maxcso
## This script assumes you have maxcso available in your $PATH
## https://github.com/unknownbrackets/maxcso

# Move images to a staging directory so that we ensure the extension is lowercase, ignoring existing case
# This also allows us to later get a list of before-and-after sizes
shopt -s globstar
shopt -s nullglob
shopt -s nocaseglob
mkdir stage

for i in *.iso; do
  mv "$i" "./stage/${i%.*}.iso"
done  

# Print list of images to compress, and their sizes, then compress with maxcso, print new sizes, and clean up
echo -e "\nFiles to be compressed:"; du -sch ./stage/*.iso
  echo -e "\nCompressing with maxcso:";
   for f in ./stage/*.iso; do
     name=${f%.iso}
      maxcso "$name.iso" -o "$name.cso"
   rm -rf "$f"
done   

echo -e "\nCompression finished, new file sizes:"; du -sch ./stage/*.cso; find ./stage/ -name "*.cso" -exec mv {} ./ \;

rmdir stage
shopt -u nocaseglob
shopt -u nullglob
shopt -u globstar

To execute it, make sure you set the proper permissions on it, and just call it with Bash.

$ cd ~/storage/shared/path/to/ISO
$ chmod +x compress.sh
$ bash compress.sh