https://imagemagick.org/script/download.php
Make sure to make the binaries available in your PATH
.
https://developers.google.com/speed/webp/download
Make sure to make the binaries available in your PATH
.
Save the following script as webp2gif.sh
:
#!/bin/bash
TARGET=.
TMP=.
OVERWRITE=false
CLEAN=true
DELAY_SET=auto
# parse config params
for VAR in ${BASH_ARGV[*]} ; do
if [[ $VAR == "-target="* ]]; then
TARGET=$(echo "$VAR" | cut -d'=' -f2- | sed 's:/*$::')
elif [[ $VAR == "-tmp="* ]]; then
TMP=$(echo "$VAR" | cut -d'=' -f2- | sed 's:/*$::')
elif [[ $VAR == "-overwrite="* ]]; then
OVERWRITE=$(echo "$VAR" | cut -d'=' -f2-)
elif [[ $VAR == "-clean="* ]]; then
CLEAN=$(echo "$VAR" | cut -d'=' -f2-)
elif [[ $VAR == "-delay="* ]]; then
DELAY_SET=$(echo "$VAR" | cut -d'=' -f2-)
fi
done
mkdir -p $TARGET
mkdir -p $TMP
# for all parameters
for VAR in ${BASH_ARGV[*]} ; do
# if is not a config param
if [[ "$VAR" != "-"* ]]; then
# if is webp file
if [[ "$VAR" == *".webp" ]]; then
FILE=$(realpath "$VAR")
PREFIX=$(basename "$FILE" | sed -e 's/^\(.*\).webp$/\1/')
echo "processing file $VAR"
echo " FILE = $FILE"
echo " PREFIX = $PREFIX"
# only overwrite file if allowed
if [ "$OVERWRITE" == "true" ] || [ ! -f $TARGET/$PREFIX.gif ]; then
FRAMES=$(webpinfo -summary $FILE | grep frames | sed -e 's/.* \([0-9]*\)$/\1/')
STATUS=${PIPESTATUS[0]}
# if webpinfo was successful
if [ $STATUS -eq 0 ]; then
echo " FRAMES = $FRAMES"
# check if is animation or single image
if [ "$FRAMES" != "1" ]; then
# detect animation speed from webp
if [ "$DELAY_SET" == "auto" ]; then
# get duration of animation
DURATION=$(webpinfo -summary $FILE | grep Duration | sed -e 's/.* \([0-9]*\)$/\1/' | uniq -c | sort -nr | head -n 1 | awk '{print $2}')
echo " DURATION = $DURATION"
# calculate delay for convert (DELAY = 100/FPS = DURATION/10)
DELAY=$(awk "BEGIN {print int($DURATION/10.0)}")
# DELAY should never be zero, change it to one, most likely still incorrect
if [ "$DELAY" == "0" ]; then
DELAY=1
fi
else
# use given delay by parameter
DELAY=$DELAY_SET
fi
echo " DELAY = $DELAY"
echo " dumping animation to $TMP"
anim_dump -folder $TMP -prefix $PREFIX. $FILE > /dev/null 2>&1
rm -f $PREFIX.gif
echo " converting to gif"
magick convert -delay $DELAY $TMP/$PREFIX.*.png -loop 0 $TARGET/$PREFIX.gif > /dev/null 2>&1
echo " created file at $TARGET/$PREFIX.gif"
if [ "$CLEAN" == "true" ]; then
echo " cleaning tmp folder"
rm $TMP/$PREFIX.[0-9]*.png
fi
else
# only image, no animation
echo " file $VAR has no animation, converting to png"
magick convert $FILE $TARGET/$PREFIX.png
fi
else
# webpinfo cannot read the file
echo " The file $VAR is corrupted"
fi
else
# file exists and should not be overwritten
echo " Skipping file $VAR, target already exists at $TARGET/$PREFIX.gif"
fi
else
# not a webp file
echo "skipping file $VAR, not a webp file"
fi
fi
done
./webp2gif.sh myanimation.webp
./webp2gif.sh myanimation.webp -delay=10
./webp2gif.sh *.webp -target=out -tmp=/tmp/webp -overwrite=true
Param | Description | Default |
---|---|---|
-target=<folder> | specifies the target folder | ./ |
-tmp=<folder> | specifies a temporary folder to dump animations to | ./ |
-overwrite=<true/false> | overwrite if target file exists | false |
-clean=<true/false> | clean up dumped imaanimations | true |
-delay=<value> | specify a fixed delay (delay=10/FPS) | detects automatically |