Skip to content

Instantly share code, notes, and snippets.

@pwm
Created February 4, 2022 10:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pwm/de594c8032ab88e6d30c88ccaee698c8 to your computer and use it in GitHub Desktop.
Save pwm/de594c8032ab88e6d30c88ccaee698c8 to your computer and use it in GitHub Desktop.
Gif gen
#!/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%.*}"
# https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/
#ffmpeg -i $name -filter_complex "[0:v] fps=$fps,scale=$scale:-2,split [a][b];[a] palettegen [p];[b][p] paletteuse" -y frames/frame%04d.png
mkdir frames
# https://cassidy.codes/blog/2017/04/25/ffmpeg-frames-to-gif-optimization/
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
# https://github.com/phw/peek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment