Skip to content

Instantly share code, notes, and snippets.

@rkorsak
Last active June 2, 2023 15:37
Show Gist options
  • Save rkorsak/c46ae326ddfaef1b9caf11c90b9ee298 to your computer and use it in GitHub Desktop.
Save rkorsak/c46ae326ddfaef1b9caf11c90b9ee298 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Generates an animated GIF from a source video (e.g. a .mov file)
# See:
# * http://superuser.com/questions/556029/how-do-i-convert-a-video-to-gif-using-ffmpeg-with-reasonable-quality
# * http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
# Requires ffmpeg on the PATH
# Usage:
# mkgif filename [output_width]
set -euo pipefail
inputfile=""
outputfile=""
vidwidth=640
palettegenMode="full"
function usage() {
echo "Usage:"
echo " ${0##*/} [--width|-w <value>] [--output <file path>] [-d | --optimize-diff] inputFilePath"
}
first_arg=""
second_arg="${DEFAULT_SECOND_ARG:-}"
positional_args=()
while [[ $# -gt 0 ]]; do
case "$1" in
-w | --width)
vidwidth="$2"
shift
;;
-o | --output)
outputfile="$2"
shift
;;
-d | --optimize-diff)
palettegenMode="diff"
;;
--help | -h)
usage
exit 1
;;
-* | --*)
usage
exit 1
;;
*)
positional_args+=("$1")
;;
esac
shift
done
set -- "${positional_args[@]}"
inputfile="$1"
if [[ -z "$inputfile" ]]; then
echo "No filename specified!"
usage
exit 1
fi
if [[ ! -f "$inputfile" ]]; then
echo "File $inputfile does not exist!"
usage
exit 1
fi
if [[ -z "$outputfile" ]]; then
outputfile=${inputfile%.*}.gif
fi
echo "Generating the GIF as $outputfile (width = $vidwidth)..."
ffmpeg -i "$inputfile" -vf "fps=10,scale=$vidwidth:-1:flags=lanczos,split[s0][s1];[s0]palettegen=stats_mode=$palettegenMode[p];[s1][p]paletteuse" "$outputfile"
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment