fix: Rewrite to make more compliant

This commit is contained in:
Carlos Solís 2025-02-03 03:47:39 +00:00
parent 2ff422aaef
commit e7b88efe81

View file

@ -1,56 +1,71 @@
#!/bin/bash #!/bin/bash
IFS=" IFS="
" "
#media-optimize #media-optimize
find . -type f -iname "*.jp*" -size +50k | ( find . -type f -iname "*.jp*" -size +50k | (
while read p while read p
do do
nice -n 15 jpegoptim -m 76 "${p}" & nice -n 15 jpegoptim -m 76 "${p}" &
if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]] if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]]
then then
wait -n wait -n
fi fi
done; done;
wait wait
) )
find . -type f -iname "*.gif" -size +500k | ( find . -type f -iname "*.gif" -size +500k | (
while read q while read q
do do
nice -n 15 gifsicle --batch -O3 --lossy=80 --colors=255 "${q}" & nice -n 15 gifsicle --batch -O3 --lossy=80 --colors=255 "${q}" &
if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]] if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]]
then then
wait -n wait -n
fi fi
done; done;
wait wait
) )
find . -type f -iname "*.png" -size +300k | ( #Specific compression for large GIF files
while read r find . -type f -iname "*.gif" -size +512000 | (
do while read p
nice -n 15 oxipng -o max "${r}" & do
if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]] while [[ $(stat -c%s "${p}" || 0) -ge 512000 ]]
then do
wait -n frameamount=$(exiftool -b -FrameCount "${p}")
fi nice -n 15 gifsicle "${p}" $(seq -f "#%g" 0 2 "${frameamount}") -O3 --lossy=80 --colors=255 -o "${p}"
done; done
wait done
) )
#compress-webp find . -type f -iname "*.png" -size +500k | (
find . -type f -iname "*.webp" -size +200k | ( while read r
while read s do
do nice -n 15 oxipng -o max "${r}" &
nice -n 15 cwebp -mt -af -quiet "${s}" -o /tmp/"${s##.*\/}"_temp.webp if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]]
if [[ -f /tmp/"${s##.*\/}"_temp.webp ]] then
wait -n
fi
done;
wait
)
#compress-webp
find . -type f -iname "*.webp" -size +50k | (
while read s
do
#If file is not animated
if ! grep -v -q -e "ANIM" -e "ANMF" "${s}"
then
cwebp -mt -af -quiet "${s}" -o /tmp/"${s##.*\/}"_temp.webp
if [ -f /tmp/"${s##.*\/}"_temp.webp ]
then then
size_new=$(stat -c%s /tmp/"${s##.*\/}"_temp.webp) size_new=$(stat -c%s /tmp/"${s##.*\/}"_temp.webp || 0)
size_original=$(stat -c%s "${s}") size_original=$(stat -c%s "${s}" || 0)
if [[ "${size_original}" -gt "${size_new}" ]] if [ "$size_original" -gt "$size_new" ]
then then
mv /tmp/"${s##.*\/}"_temp.webp "${s}" mv /tmp/"${s##.*\/}"_temp.webp "$s"
else else
rm /tmp/"${s##.*\/}"_temp.webp rm /tmp/"${s##.*\/}"_temp.webp
fi fi
fi fi
done; fi
wait done;
) wait
)