Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Denys-Bushulyak/8252a208aebaabd2b1321fb5ccd05527 to your computer and use it in GitHub Desktop.
Save Denys-Bushulyak/8252a208aebaabd2b1321fb5ccd05527 to your computer and use it in GitHub Desktop.
Compress png to jpeg by reducing size to limit
#!/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