Skip to content

Instantly share code, notes, and snippets.

@noio
Created August 22, 2014 15:23
Show Gist options
  • Save noio/44360f3307542a72b050 to your computer and use it in GitHub Desktop.
Save noio/44360f3307542a72b050 to your computer and use it in GitHub Desktop.
mo(vie) to gif
#!/bin/bash
W=0
DROP=1
F=2
while getopts ":w:d:f:" opt; do
case $opt in
w)
W=$OPTARG
;;
d)
DROP=$OPTARG
;;
f)
F=$OPTARG
;;
?)
echo "Usage" >&2
exit 1
;;
esac
done
# Shift, we should only have the input filename remaining now
shift $(($OPTIND-1))
INPUT_FILE=$1
OUTPUT_FILE="${INPUT_FILE%.*}.moto.gif"
# Get info from the input file
eval $(ffprobe -loglevel quiet -show_streams "$INPUT_FILE" | grep -E "width|height")
# Compute new size
if [ $W -gt 0 ]; then
height=$((height*W/width))
width=$W
echo "Resizing to ${width}x${height}..."
fi
# Compute framerate/droprate
DELAY=30
if [ $DROP -gt 1 ]; then
DELAY=$((30/DROP))
fi
# Create a directory for the images
dir=`mktemp -d -t motogif`
if [ $? -ne 0 ] ; then
echo "Failed to create temp dir..."
exit 1
fi
echo "Splitting movie to image sequence..."
ffmpeg -loglevel warning -i "$INPUT_FILE" -s "${width}x${height}" -f image2 $dir/a%03d.png
FRAMES=`ls -l $dir/*.png | wc -l`
echo "Converting to gif..."
convert +dither -fuzz ${F}% -delay 1x${DELAY} `seq -f $dir/a%03g.png 1 ${DROP} ${FRAMES}` -coalesce -layers OptimizeTransparency ${OUTPUT_FILE}
du -h ${OUTPUT_FILE}
# Clean up the temporary directory
rm -r $dir
if [ $? -eq 0 ] ; then
echo "Cleaned up temp dir..."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment