Skip to content

Instantly share code, notes, and snippets.

@rsms
Created September 27, 2016 21:51
Show Gist options
  • Select an option

  • Save rsms/3b18391fe38d5bd38b44b5e6fd06581e to your computer and use it in GitHub Desktop.

Select an option

Save rsms/3b18391fe38d5bd38b44b5e6fd06581e to your computer and use it in GitHub Desktop.
Convert a video to GIF
#!/bin/bash
set -e
if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ]; then
echo "usage: ${0##*/} <video-input-file> [<gif-output-file>]" >&2
echo " if <gif-output-file> is not provided, <video-input-file> with" >&2
echo " filename extension replaced by \".gif\" is used" >&2
exit 1
fi
infile=$1
outfile=$2
if [ "$outfile" = "" ]; then
outfile=$(echo "$infile" | sed -E 's/\.[^\.]+$/.gif/g')
fi
if [ -e "$outfile" ] && [ -d ~/.Trash ]; then
mv -f "$outfile" ~/.Trash/"$(basename "$outfile")"
fi
if [ -z $TMPDIR ]; then
TMPDIR=/tmp
fi
palette="${TMPDIR}/palette.png"
echo "palette: $palette"
filters="fps=5,unsharp=luma_msize_x=3:luma_msize_y=3:luma_amount=1.0,scale=800:-1:flags=lanczos"
paletteops="stats_mode=diff"
paletteops=""
ffmpeg -v warning -i "$infile" -vf "$filters,palettegen=$paletteops" -y "$palette"
ffmpeg -v warning -i "$infile" -i "$palette" -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$outfile"
if (which gifsicle >/dev/null); then
gifsicle --optimize=3 --output "$outfile" "$outfile"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment