Sh Code to Convert Files from JPG to WEPB in bulk from a folder

Firstly Need to Install webp Extension on the linux or operating system

#!/bin/bash


# Usage: ./jpg-to-webp-folder.sh /path/to/input /path/to/output


INPUT_DIR="{location}"
OUTPUT_DIR="{location}"


# Check inputs
if [ -z "$INPUT_DIR" ] || [ -z "$OUTPUT_DIR" ]; then
    echo "Usage: $0 <input_folder> <output_folder>"
    exit 1
fi


# Create output folder if it doesn't exist
mkdir -p "$OUTPUT_DIR"


# Loop through JPG/JPEG files
for img in "$INPUT_DIR"/*.jpg "$INPUT_DIR"/*.JPG "$INPUT_DIR"/*.jpeg "$INPUT_DIR"/*.JPEG; do
    [ -e "$img" ] || continue


    # Get filename without extension
    base=$(basename "$img")
    name="${base%.*}"


    out="$OUTPUT_DIR/$name.webp"


    echo "Converting $img → $out"
    cwebp "$img" -q 80 -o "$out"
done


echo "All done!"

Leave a comment

Your email address will not be published. Required fields are marked *