Skip to content

Instantly share code, notes, and snippets.

@tdhooper
Last active August 8, 2023 13:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdhooper/ca37d14edc5f1ee8a98fe20f3b64827f to your computer and use it in GitHub Desktop.
Save tdhooper/ca37d14edc5f1ee8a98fe20f3b64827f to your computer and use it in GitHub Desktop.
#!/bin/sh
# From http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
# Usage:
# ./makegif.sh inputframes output.gif
# where inputframes is a directory containing .png frames
# Change size here
filters="scale=640:-1:flags=lanczos"
# Generate palette, you can change max_colors
palette="palette.png"
ffmpeg -thread_queue_size 512 -v warning -pattern_type glob -i "$1/*.png" -vf "$filters,palettegen=stats_mode=diff:max_colors=256:reserve_transparent=false" -y $palette
# Create gif
ffmpeg -thread_queue_size 512 -v warning -pattern_type glob -i "$1/*.png" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse=dither=sierra2_4a" -y $2
# Optimise and adjust
# * Delay between frames is 100ths of a second, for example 4 is 25fps
# * To repeat twice do "#0-" "#0-"
gifsicle --batch --optimize=03 --delay=4 $2 "#0-"
# Display final file size
stat --format=%s $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment