dotfiles/.config/zsh/functions/apng2webp
2022-05-03 20:50:18 +02:00

51 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env zsh
# Convert animated PNG to animated WebP. Maximum compression no blend.
setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL
zmodload zsh/zutil
local -a o_help=()
local -a o_delay=()
zparseopts -D -K -- h=o_help -help=o_help -delay:=o_delay
if [[ ${#o_help} -ne 0 || ! -v 2 ]]; then
local ret=$(( ${#o_help} ^ 1 ))
print -u $(( 1 + ${ret} )) \
"usage: ${0} [-h|--help] [--delay MILLISECONDS]" \
"<INPUT FILE> <OUTPUT FILE>"
return ${ret}
fi
local input="$(realpath "${1}")"
local output="$(realpath "${2}")"
local tmpdir=$(mktemp --directory --suffix='.apng2webp')
pushd ${tmpdir}
mkdir -p apngdis
pushd apngdis
cp "${input}" "input.png"
apngdis "input.png"
popd
counter=1
for file in apngdis/apngframe*.png; do
cwebp -lossless -q 100 "${file}" -o "$(printf "%02d" ${counter}).webp"
counter=$(( counter+1 ))
done
local -a webpmux_options=()
local counter=1
for file in apngdis/apngframe*.txt; do
local delay=$(grep -Po '=\d+/' "${file}")
delay=${delay:1:-1}
[[ ${#o_delay} -eq 2 ]] && delay=${o_delay[2]}
webpmux_options+=(-frame $(printf "%02d" ${counter}).webp +${delay}+0+0+0-b)
counter=$((counter+1))
done
# shellcheck disable=2086
webpmux ${webpmux_options} -o "${output}"
popd
rm -r ${tmpdir}