run this command to make it executable
chmod +x convert_to_webp.sh
usage:
$ ./convert_to_webp.sh image1.png image2.jpg
or to convert files in CWD
./convert_to_webp.sh
#!/bin/bash | |
convert_to_webp() { | |
for file in "$@"; do | |
if [[ $file == *.png ]]; then | |
filename=$(basename -- "$file") | |
filename="${filename%.*}" | |
ffmpeg -i "$file" -compression_level 6 "${filename}.webp" | |
elif [[ $file == *.jpg ]]; then | |
filename=$(basename -- "$file") | |
filename="${filename%.*}" | |
ffmpeg -i "$file" "${filename}.webp" | |
fi | |
done | |
} | |
if [ "$#" -gt 0 ]; then | |
convert_to_webp "$@" | |
else | |
# If no parameters are provided, convert all PNG and JPG files in the current directory | |
convert_to_webp *.png *.jpg | |
fi |