Convert files to webp

Do you have a folder with many images that you want to convert to webp? Don’t want to do it all by hand? Use this script then. You will need to set the WORKDIR variable, and perhaps change the path to the bash shell (like when you’re on FreeBSD, the path is /usr/local/bin/bash).

Requirements: webp (obviously, see https://developers.google.com/speed/webp/download), bash (duh), basename (should be installed already).

#!/bin/bash

WORKDIR=/path/to/images

# Search for files per extension in subdirectories and convert them to webp.
for EXT in jpg jpeg JPG JPEG ; do

        for DIR in `find $WORKDIR -type d` ; do

                for FILE in `ls -1 $DIR/*.$EXT` ; do
                        FILENAME=`basename --suffix=.$EXT $FILE`
                        /usr/bin/cwebp $FILE -o $DIR/$FILENAME.webp
                done

        done

done

Comments

Leave a Reply

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

six + nine =