.dotfiles/.local/bin/optimizechar
2025-06-09 04:34:35 -03:00

175 lines
5.0 KiB
Bash
Executable File

#!/bin/bash
# Function to downscale images if they're larger than 960x544
downscale_image() {
local img="$1"
local width height
# Get the image dimensions
dimensions=$(identify -format "%wx%h" "$img")
width=$(echo $dimensions | cut -d'x' -f1)
height=$(echo $dimensions | cut -d'x' -f2)
# If width > 960 or height > 544, resize
if [ "$width" -gt 960 ] || [ "$height" -gt 544 ]; then
mogrify -resize 960x544\> "$img"
echo "Downscaled $img to 960x544"
fi
}
downscale_webp() {
local img="$1"
local width height
dimensions=$(identify -format "%wx%h" "$img")
width=$(echo $dimensions | cut -d'x' -f1)
height=$(echo $dimensions | cut -d'x' -f2)
if [ "$width" -gt 960 ] || [ "$height" -gt 544 ]; then
gifname="${img}.gif"
magick "$img" "$gifname"
gifsicle --resize _x544 --colors 256 "$gifname" > "${gifname}s"
rm "$img"
rm "$gifname"
magick "${gifname}s" "$img"
rm "${gifname}s"
fi
}
# downscale_apng() {
# local img="$1"
# local width height
# dimensions=$(identify -format "%wx%h" "$img")
# width=$(echo $dimensions | cut -d'x' -f1)
# height=$(echo $dimensions | cut -d'x' -f2)
# if [ "$width" -gt 960 ] || [ "$height" -gt 544 ]; then
# gifname="${img}.gif"
# magick "$img" "$gifname"
# gifsicle --resize _x544 --colors 256 "$gifname" > "${gifname}s"
# rm "$img"
# rm "$gifname"
# magick "${gifname}s" "$img"
# rm "${gifname}s"
# fi
# }
convert_audio_files() {
local audio_file="$1"
local dir=$(dirname "$audio_file")
local base=$(basename "$audio_file")
local filename="${base%.*}"
local opus_file="$dir/$filename.opus"
# Convert to opus format
ffmpeg -i "$audio_file" -c:a libopus -b:a 128k "$opus_file" && \
if [[ "${base,,}" == *.wav ]]; then
rm "$audio_file"
echo "Deleted original WAV file: $audio_file"
fi
}
# Function to resize char_icon to 60x60 if it's bigger
resize_char_icon() {
local img="$1"
local width height
# Get the image dimensions
dimensions=$(identify -format "%wx%h" "$img")
width=$(echo $dimensions | cut -d'x' -f1)
height=$(echo $dimensions | cut -d'x' -f2)
# If width > 60 or height > 60, resize
if [ "$width" -gt 60 ] || [ "$height" -gt 60 ]; then
convert "$img" -resize 60x60 "$img"
echo "Resized $img to 60x60"
fi
}
# Function to optimize images using pngquant
optimize_images() {
local img="$1"
if [[ "$img" == *.png ]]; then
pngquant --force --ext .png "$img"
echo "Optimized $img using pngquant"
fi
}
# Function to resize images in the emotions folder to 40x40 if they are bigger
resize_emotions_images() {
local img="$1"
local width height
# Get the image dimensions
dimensions=$(identify -format "%wx%h" "$img")
width=$(echo $dimensions | cut -d'x' -f1)
height=$(echo $dimensions | cut -d'x' -f2)
# If width > 40 or height > 40, resize
if [ "$width" -gt 40 ] || [ "$height" -gt 40 ]; then
mogrify -resize 40x40\> "$img"
echo "Resized $img to 40x40"
fi
}
# Delete all files containing "_on" in the filename
delete_files_with_on() {
local img="$1"
if [[ "$img" == *"_on"* ]]; then
rm -f "$img"
echo "Deleted $img"
fi
}
# Delete all Thumbs.db files recursively
delete_thumbs_db() {
find . -type f -iname "Thumbs.db" -exec rm -f {} \;
echo "Deleted all Thumbs.db files"
}
# Resize the char_icon image to 60x60 if it's larger than that
char_icon="char_icon"
for ext in webp apng jpg jpeg png gif bmp; do
if [ -f "$char_icon.$ext" ]; then
resize_char_icon "$char_icon.$ext"
break
fi
done
# Delete files containing "_on" in the filename
find . -type f | while read -r img; do
delete_files_with_on "$img"
done
# Optimize all images
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.webp" -o -iname "*.gif" -o -iname "*.bmp" \) | while read -r img; do
# Skip files in "emotions" folder
if [[ "$img" == *"/emotions/"* ]]; then
# Resize image in "emotions" folder if necessary
resize_emotions_images "$img"
continue
fi
if [[ "$img" == *".webp" ]]; then
downscale_webp "$img"
# elif [[ "$img" == *".apng" ]]; then
# downscale_apng "$img"
elif [[ "$img" == *".gif" ]]; then
dimensions=$(identify -format "%wx%h" "$img")
width=$(echo $dimensions | cut -d'x' -f1)
height=$(echo $dimensions | cut -d'x' -f2)
if [ "$width" -gt 960 ] || [ "$height" -gt 544 ]; then
gifsicle --resize _x544 --colors 256 "$img" > "${img}s"
rm "$img"
mv "${img}s" "$img"
fi
else
# Downscale image if necessary
downscale_image "$img"
optimize_images "$img"
fi
done
find . -type f \( -iname "*.wav" -o -iname "*.mp3" \) | while read -r audio; do
convert_audio_files "$audio"
done
# Delete all Thumbs.db files recursively
delete_thumbs_db
echo "Script completed!"