#!/bin/bash # Convert animated PNG to animated WebP. Maximum compression no blend. set -euo pipefail if [[ $# -lt 2 ]]; then echo "Usage: ${0} input.png output.webp" >&2 exit 1 fi input="$(realpath "${1}")" output="$(realpath "${2}")" mkdir -p apng2webp_work pushd apng2webp_work 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 webpmux_options="" counter=1 for file in apngdis/apngframe*.txt; do delay=$(grep -Po '=\d+/' "${file}") delay="${delay:1:-1}" webpmux_options="${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 apng2webp_work