Skip to content

Instantly share code, notes, and snippets.

@nicksieger
Created April 28, 2022 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicksieger/b73e5934950cc3d71191c755f1ff8fef to your computer and use it in GitHub Desktop.
Save nicksieger/b73e5934950cc3d71191c755f1ff8fef to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Convert a .mov or .mp4 to a .gif using ffmpeg and gifsicle.
#
# Tools install: https://gist.github.com/dergachev/4627207#installation
#
# Sources:
# 1. https://discovergrid.com/animated-gif-with-ffmpeg-palettegen/
# Use ffmpeg with a generated palette to map colors better
# 2. https://gist.github.com/dergachev/4627207
# Gifsicle usage
# 3. https://www.robinstewart.com/blog/2018/10/adding-a-delay-to-the-end-of-an-animated-gif/
# Add a delay at the end of the movie
# usage: $0 [-f FPS] .mov files...
getopts 'f:' fps
if [ "$fps" -a "$fps" != '?' ]; then
fps=$OPTARG
else
fps=10
fi
while [ $OPTIND -gt 1 ]; do
shift
OPTIND=$[ $OPTIND - 1 ]
done
while [ "$1" ]; do
f=$1
p=${f/.mov/}.palette.png
g=${f/.mov/.gif}
ffmpeg -y -i $f -vf fps=$fps,palettegen $p
ffmpeg -y -i $f -i $p -filter_complex "fps=$fps[x];[x][1:v]paletteuse" -f gif - \
| gifsicle -d3 "#0--2" -d200 "#-1" -O2 > $g
rm $p
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment