Skip to content

Instantly share code, notes, and snippets.

@yogithesymbian
Last active November 7, 2025 23:21
Show Gist options
  • Select an option

  • Save yogithesymbian/7725a23b2a9f4ddc66565c6cce69125e to your computer and use it in GitHub Desktop.

Select an option

Save yogithesymbian/7725a23b2a9f4ddc66565c6cce69125e to your computer and use it in GitHub Desktop.
Convert all image inside deep folder public to WEBP | 1.5MB to 47KB
#!/bin/bash
# ========================================================
# πŸ–ΌοΈ Auto convert all PNG & JPG (including subfolders) to WEBP
# by Yogi Arif Widodo β€” optimized for macOS / Linux
# ========================================================
# Default settings
TARGET_DIR="public"
COMPRESSION_LEVEL=6 # 0 (fast) - 6 (best compression)
QUALITY=25 # Lower = better quality (0–51)
DELETE_ORIGINAL=false
# ================================
# 🧠 Parse CLI arguments
# ================================
while [[ "$#" -gt 0 ]]; do
case $1 in
--dir) TARGET_DIR="$2"; shift ;;
--quality) QUALITY="$2"; shift ;;
--compression) COMPRESSION_LEVEL="$2"; shift ;;
--delete-original) DELETE_ORIGINAL=true ;;
-h|--help)
echo "Usage: ./convert_all_to_webp.sh [options]"
echo ""
echo "Options:"
echo " --dir <path> Folder target (default: public)"
echo " --quality <0-51> Quality level (default: 25)"
echo " --compression <0-6> Compression level (default: 6)"
echo " --delete-original Hapus file asli setelah sukses konversi"
echo " -h, --help Tampilkan bantuan"
exit 0 ;;
esac
shift
done
# ================================
# πŸš€ Start converting
# ================================
echo "πŸš€ Starting conversion"
echo "πŸ“ Folder: $TARGET_DIR"
echo "🎨 Quality: $QUALITY | Compression: $COMPRESSION_LEVEL"
echo "πŸ—‘οΈ Delete originals: $DELETE_ORIGINAL"
echo "-------------------------------------------"
# Find and convert all PNG/JPG/JPEG recursively
find "$TARGET_DIR" \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" \) -type f -print0 | \
while IFS= read -r -d '' file; do
output="${file%.*}.webp"
# Skip if already converted
if [ -f "$output" ]; then
echo "⚑ Skip (exists): $output"
continue
fi
echo "🎨 Converting: $file -> $output"
ffmpeg -nostdin -loglevel error -i "$file" -compression_level $COMPRESSION_LEVEL -qscale:v $QUALITY "$output"
if [ $? -eq 0 ]; then
echo "βœ… Success: $output"
if [ "$DELETE_ORIGINAL" = true ]; then
rm "$file"
echo "πŸ—‘οΈ Deleted original: $file"
fi
else
echo "❌ Failed: $file"
fi
done
echo "-------------------------------------------"
echo "πŸŽ‰ All conversions complete!"
echo "πŸ“‚ Check your folder: $TARGET_DIR"
@yogithesymbian
Copy link
Author

πŸ’‘ Cara pakai

  1. Simpan script:

    nano convert_all_to_webp.sh

    (paste isi di atas, lalu CTRL+O, ENTER, CTRL+X)

  2. Jadikan executable:

    chmod +x convert_all_to_webp.sh
  3. Jalankan sesuai kebutuhan:

βœ… Default (folder public, quality 25)

./convert_all_to_webp.sh

🎯 Atur kualitas dan target folder

./convert_all_to_webp.sh --dir public/meme --quality 30

🧹 Sekalian hapus file asli setelah konversi

./convert_all_to_webp.sh --delete-original

🧠 Bantuan

./convert_all_to_webp.sh --help

πŸ” Opsional: Logging hasil

Kalau mau menyimpan daftar file sukses/gagal ke log:
tambahkan setelah echo terakhir:

) | tee convert.log

Jadi line terakhir jadi:

done | tee convert.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment