Created
February 14, 2026 18:48
-
-
Save the-code-rider/4223f37aa8e594d500866154ea15bfe2 to your computer and use it in GitHub Desktop.
convert png images in folder into 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
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # Usage: | |
| # ./png2webp_ffmpeg.sh /path/to/folder | |
| # Env vars: | |
| # QUALITY=80 (0-100, higher = better quality / larger) | |
| # DELETE=0 (1 to delete original png) | |
| DIR="${1:-.}" | |
| QUALITY="${QUALITY:-80}" | |
| DELETE="${DELETE:-0}" | |
| command -v ffmpeg >/dev/null 2>&1 || { echo "Error: ffmpeg not found."; exit 1; } | |
| shopt -s nullglob | |
| for f in "$DIR"/*.png; do | |
| out="${f%.png}.webp" | |
| if [[ -e "$out" ]]; then | |
| echo "Skip (exists): $out" | |
| continue | |
| fi | |
| # libwebp uses 0-100 quality. -pix_fmt yuva420p preserves alpha if present. | |
| ffmpeg -hide_banner -loglevel error -y \ | |
| -i "$f" \ | |
| -c:v libwebp -quality "$QUALITY" -pix_fmt yuva420p \ | |
| "$out" | |
| echo "Converted: $f -> $out" | |
| if [[ "$DELETE" == "1" ]]; then | |
| rm -f -- "$f" | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment