Skip to content

Instantly share code, notes, and snippets.

@giansegato
Last active October 20, 2020 09:07
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 giansegato/d41812dd2ae7bb9a702616b5ba8f0ac4 to your computer and use it in GitHub Desktop.
Save giansegato/d41812dd2ae7bb9a702616b5ba8f0ac4 to your computer and use it in GitHub Desktop.
Mov to Gif script (amazingly useful for inline demos)
#!/bin/bash
# see: https://medium.com/@colten_jackson/doing-the-gif-thing-on-debian-82b9760a8483
SOURCE=$1
DESTINATION="${SOURCE%.*}.gif"
# Palette generation
ffmpeg -i $SOURCE -vf fps=7,scale=320:-1:flags=lanczos,palettegen temp_palette.png
# Conversion
# To rescale to 400px (preserving ratio):
# ffmpeg -i $SOURCE -i temp_palette.png -filter_complex "fps=11,scale=400:-1:flags=lanczos[x];[x][1:v]paletteuse" $DESTINATION
# 600px:
# ffmpeg -i $SOURCE -i temp_palette.png -filter_complex "fps=11,scale=600:-1:flags=lanczos[x];[x][1:v]paletteuse" $DESTINATION
# To avoid rescale:
ffmpeg -i $SOURCE -i temp_palette.png -filter_complex "fps=11,paletteuse" $DESTINATION
# Compression
gifsicle -O3 $DESTINATION -o $DESTINATION
# Speed (optional)
numframes=`gifsicle $DESTINATION -I | grep -E "[0-9]+ images" --only-matching | grep -E "[0-9]+" --only-matching`
gifsicle -U $DESTINATION `seq -f "#%g" 0 2 $numframes` -O2 -o "${DESTINATION%.*}.2s.gif"
# Temp removal
rm temp_palette.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment