Last active
March 26, 2025 04:04
-
-
Save pedrovanzella/874be4ea2f20c4504fab746b76bc38bb to your computer and use it in GitHub Desktop.
Finds all cbz and cbr files in a directory and converts all files inside to webp
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/sh | |
# do-the-webp.sh | |
# Author: Pedro Vanzella <[email protected]> | |
# Usage: do-the-webp.sh <dir> | |
# License: BSD | |
# | |
# Finds all cbz and cbr files in a directory and converts all files inside to | |
# webp with quality 80, which should yield a 30% compression with no noticeably | |
# loss of quality | |
do_the_webp() { | |
for f in "$1"/*.jpg; do | |
[ -e "$f" ] || continue | |
b=${f%.*} | |
echo "[Converting] $b" | |
cwebp -q 80 "$f" -o "$b".webp > /dev/null 2>&1 | |
#convert "$f" -quality 80 -define webp:lossless=true "$b".webp | |
rm "$f" | |
done | |
} | |
process_dir() { | |
cd "$1" | |
for file in *; do | |
[ -e "$file" ] || continue | |
if [[ -d "$file" ]] | |
then | |
echo "Recursively visiting $file" | |
process_dir "$file" | |
fi | |
echo "" | |
base=${file##*/} | |
echo "[Processing]" $base | |
if [[ $base == *.cbr ]] | |
then | |
mkdir "$base"-unrard | |
cd "$base"-unrard | |
unrar e ../"$base" | |
do_the_webp . | |
cd .. | |
bn=${base%.*} | |
#rar a "$bn"-webp.cbr "$base"-unrard/* | |
zip "$bn"-webp.cbz "$base"-unrard/* | |
rm -rf "$base"-unrard | |
fi | |
if [[ $base == *.cbz ]] | |
then | |
#echo "unzip" | |
mkdir "$base"-unzipd | |
cd "$base"-unzipd | |
unzip -j ../"$base" | |
do_the_webp . | |
cd .. | |
bn=${base%.*} | |
zip "$bn"-webp.cbz "$base"-unzipd/* | |
rm -rf "$base"-unzipd | |
fi | |
# uncomment this to also delete the original files | |
#rm "$file" | |
done | |
} | |
process_dir $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment