WebP Shell

Last updated: 2021-02-07

Installation

Install the WebP CLI

Script

Create a new shell script named mwebp in the correct location. For macOS, this is preferably the /usr/local/bin/ directory. Windows location?

Set the file permissions:

chmod a+x mwebp
#!/bin/bash
set -e
for FILE in *;
  do
    filename=$(basename "$FILE")
    ext="${filename##*.}"
    if [ "$ext" == "jpg" ] || [ "$ext" == "png" ]
    then
      fn="$(basename "$filename" | sed 's/\(.*\)\..*/\1/')"
      cwebp "$FILE" -o "${fn}.webp"
    fi
done

Executing the script will batch convert all .jpg and .png files in the current directory using the default settings of cwebp.

References