Created
June 23, 2025 11:07
-
-
Save Denys-Bushulyak/8252a208aebaabd2b1321fb5ccd05527 to your computer and use it in GitHub Desktop.
Compress png to jpeg by reducing size to limit
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 | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <input_file.png>" | |
exit 1 | |
fi | |
input_file="$1" | |
filename=$(basename -- "$input_file") | |
name="${filename%.*}" | |
target_size=100000 # в байтах | |
quality=2 | |
while true; do | |
ffmpeg -y -i "$input_file" -q:v $quality temp.jpg | |
actual_size=$(stat -c %s temp.jpg) | |
if [ "$actual_size" -le "$target_size" ] || [ "$quality" -ge 30 ]; then | |
mv temp.jpg "$name.jpg" | |
break | |
fi | |
quality=$((quality + 1)) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment