Skip to content

Instantly share code, notes, and snippets.

@bajzarpa
Last active March 25, 2024 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bajzarpa/0282b8df3e33a7fa91918c278d1d3734 to your computer and use it in GitHub Desktop.
Save bajzarpa/0282b8df3e33a7fa91918c278d1d3734 to your computer and use it in GitHub Desktop.
Convert png or jpg images to WebP using ffmpeg from cli

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment