Thanks for this.
Not as advanced as your script (with dithering, etc), but I've always just used this script, which accepts the input video, and a custom dimension (optional):
#!/usr/bin/env bash
# https://superuser.com/a/1049820/19800
IN=$1
W=$(ffprobe -v quiet -print_format json -show_format -show_streams $IN | jq '.streams[0].width')
H=$(ffprobe -v quiet -print_format json -show_format -show_streams $IN | jq '.streams[0].height')
WH="${W}x${H}"
DIM=${2:-$WH}
echo "video resolution is $DIM"
PAL="$IN.palette.png"
if [[ -f "$PAL" ]]; then
echo "skipping palette"
else ffmpeg -y -i "$IN" -vf palettegen "$PAL"
fiffmpeg -y -i "$IN" -i "$PAL" -filter_complex paletteuse -r 15 -s $DIM "$IN.gif"
# TODO https://cassidy.codes/blog/2017/04/25/ffmpeg-frames-to-gif-o...