From 51f1670e8e370eb5589b301b21ae7fb026f2d41f Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 10 May 2017 10:25:11 +0200 Subject: [PATCH] Rewrite bin/mov2gif --- bin/mov2gif | 78 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/bin/mov2gif b/bin/mov2gif index 45ed31fa..e25cf9b4 100755 --- a/bin/mov2gif +++ b/bin/mov2gif @@ -1,7 +1,55 @@ #!/usr/bin/env bash -usage() { echo "Usage: $0 [-f ] [-o ] [-h]"; } +# 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 @@ -10,27 +58,15 @@ elif [[ ! -f $1 ]]; then exit fi -fps=30 src="$1" -dest="${${src##*/}%.mov}.gif" -while getopts hf:o: opt; do - case $opt in - -f) fps="$OPTARG" ;; - -o) dest="$OPTARG" ;; - -h) usage - exit - ;; - :) >&2 echo "$OPTARG requires an argument" - usage - exit 1 - ;; - esac -done - -palette="$(mktemp)" +dest="${2:-${${src##*/}%.mov}.gif}" flags="fps=${fps}" -ffmpeg -v warning -i "$src" -vf "$flags,palettegen" -y "$palette" -ffmpeg -v warning -i "$src" -i "$palette" -lavfi "$flags [x]; [x][1:v] paletteuse" -y "$dest" +# 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" -[[ -f $palette ]] && rm -f "$palette" +if command -v gifsicle >/dev/null; then + gifsicle -O3 "$dest" -o "$dest" +fi