Skip to content

Instantly share code, notes, and snippets.

@pwm
Created May 24, 2021 15:26
Show Gist options
  • Save pwm/96c1b0847ca8bf2cc3f60ed957a32bcf to your computer and use it in GitHub Desktop.
Save pwm/96c1b0847ca8bf2cc3f60ed957a32bcf to your computer and use it in GitHub Desktop.
gif.sh
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p ffmpeg pngquant gifski gifsicle
# shellcheck shell=bash
set -euo pipefail
if [[ "$#" -ne 3 ]]; then
echo "Usage: gif.sh <name> <scale> <fps>"
exit 1
fi
name=$1
scale=$2
fps=$3
name_root="${name%.*}"
mkdir frames
ffmpeg -i $name -vf palettegen -y palette.png
ffmpeg -i $name -v warning -vf "fps=$fps,scale=$scale:-1:flags=lanczos,palettegen=stats_mode=diff" -y palette.png
ffmpeg -i $name -i palette.png -lavfi "fps=$fps,scale=$scale:-1:flags=lanczos,paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle" -y frames/frame%04d.png
pngquant --strip --ext=.png --force frames/*.png
gifski -W $scale frames/*.png -o tmp.gif
gifsicle --colors 256 -O3 tmp.gif -o $name_root-$scale-$fps.gif
rm -rf frames palette.png tmp.gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment