#!/usr/bin/env bash # Uses ffmpeg to convert a video file to a gif file. Optimizes the final result # with gifsicle. # # Requires: ffmpeg # Optional: gifsicle # # Example: # vid2gif some-vid.mp4 output.gif # vid2gif -f 10 -s 00:00:10 -t 00:00:05 some-vid.mp4 output.gif set -e PALETTE="/tmp/palette.png" usage() { cat <&2 echo "$OPTARG requires an argument" usage exit 1 ;; *) >&2 echo "Not a valid arg: $opt" usage exit 1 ;; esac done shift $((OPTIND-1)) # if (($# == 0)); then >&2 echo "No movie file specified" exit elif [[ ! -f $1 ]]; then >&2 echo "$1 does not exist" exit fi src="$1" dest="${2:-${${src##*/}%.mov}.gif}" flags="fps=${fps}" # stats_mode=full favors motion, causing the resulting palette to better # accomodate fades and transitions. ffmpeg -v warning $start $duration -i "$src" -vf "$flags,palettegen=stats_mode=full" -y "$PALETTE" ffmpeg -v warning $start $duration -i "$src" -i "$PALETTE" -lavfi "$flags [x]; [x][1:v] paletteuse" -y "$dest" if command -v gifsicle >/dev/null; then gifsicle -O3 "$dest" -o "$dest" fi