Created
September 7, 2023 17:23
-
-
Save Spazholio/36592a141a2fe40aa5541beba918c874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## | |
# Author: Paul Martin <[email protected]> | |
# | |
# Description: This script will take a CBR or CBZ | |
# file and convery whatever's in it | |
# to webp format, and re-package it. | |
# | |
# Usage: comic_optimizer.sh [DIRECTORY | FILE] | |
## | |
# Abort if no directory/file specified | |
if [ -z "$1" ] | |
then | |
echo "No directory/file specified!" | |
exit 1 | |
fi | |
## | |
# Variable definitions | |
# (I tend to over-variablize things, I know) | |
## | |
# Programs | |
P7Z='' # 7z location | |
RAR='' # rar location | |
CWEBP='' # cwebp location | |
# Locations | |
TRASH_DIR=$(realpath "$HOME/.Trash/") # Where the original file goes when done | |
TEMP_DIR='' # Where we do the work, recommended to put in /tmp/ | |
FINAL_DIR='' # Where the finished file goes | |
## | |
# If your version of `rar` isn't working or installed, | |
# you're gonna have a bad time. This happens with macOS | |
# brew. Unsure how to fix other than to re-install it | |
# or go to "Settings > Privacy & Security" and clicking | |
# the "Just run it anyway" button, whatver that is. | |
## | |
if ! "${RAR}" > /dev/null 2>&1; | |
then | |
echo "RAR is missing or broken - fix it" | |
IFS=$SAVEIFS | |
exit 1 | |
fi | |
SAVEIFS=$IFS | |
IFS=$(echo -en "\n\b") | |
FILES=$(find "$1" -maxdepth 1 -type f -iname "*.cbr" -o -iname "*.cbz") | |
for FILE in $FILES | |
do | |
KIND=$(file -b "${FILE}") | |
FILETYPE=$( echo "${KIND:0:3}" | tr "[:upper:]" "[:lower:]") | |
FILENAME=$(basename "${FILE%.*}") | |
mkdir "${TEMP_DIR}" | |
case "${FILETYPE}" in | |
"rar") | |
echo "File is a CBR" | |
# 7zip doesn't support v5 rar files, so we have to use rar here | |
"${RAR}" e -y "${FILE}" "${TEMP_DIR}" | |
;; | |
"zip") | |
echo "File is a CBZ" | |
"${P7Z}" e "${FILE}" -o"${TEMP_DIR}" | |
;; | |
esac | |
## | |
# Here's where the heavy lifting takes place. Find all the image files, | |
# and pipes them into xargs. Here's an explanation of the flags: | |
# -0: input is terminated with a null, used with find's -print0 | |
# -P0: run the command in an unlimited number of parallel processes | |
# -I '{}': names the input | |
# cwebp '{}': replaced with the previous input | |
# -mt: multi-threaded | |
# -short: cwebp can be noisy. This helps. | |
# -progress: should be self-explanatory | |
# -o '{}'.webp: append 'webp' to the original filename | |
## | |
find -s "${TEMP_DIR}" -type f \( -iname "*.jp*g" -o -iname "*.png" -o -iname "*.gif" \) -print0 | xargs -0 -P0 -I '{}' "${CWEBP}" '{}' -mt -short -progress -o '{}'.webp | |
# Now we pack it back up into a CBZ | |
"${P7Z}" a -tzip "${TEMP_DIR}"/"$FILENAME".cbz "${TEMP_DIR}"/*.xml "${TEMP_DIR}"/*.webp | |
# OPTIONAL: Preserve file modification time | |
#touch -r "$OLDFILE" "$NEWNAME" | |
# Move it to its final destination | |
mv "${TEMP_DIR}"/"$FILENAME".cbz "${FINAL_DIR}" | |
# OPTIONAL: Delete the old file | |
mv "${FILE}" "${TRASH_DIR}" | |
# Delete the temporary directory | |
rm -r "${TEMP_DIR}" | |
done | |
IFS=$SAVEIFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment