Last active
November 7, 2025 23:21
-
-
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
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 | |
| # ======================================================== | |
| # πΌοΈ 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" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π‘ Cara pakai
Simpan script:
(paste isi di atas, lalu
CTRL+O,ENTER,CTRL+X)Jadikan executable:
Jalankan sesuai kebutuhan:
β Default (folder
public, quality 25)π― Atur kualitas dan target folder
π§Ή Sekalian hapus file asli setelah konversi
π§ Bantuan
π Opsional: Logging hasil
Kalau mau menyimpan daftar file sukses/gagal ke log:
tambahkan setelah echo terakhir:
) | tee convert.logJadi line terakhir jadi: