Skip to content

Animated GIF Creation

Zev Eisenberg edited this page Mar 14, 2017 · 4 revisions

The first step is to record a demo movie. The current video is produced by installing the demo app on device, then running through the local images demo and recording using QuickTime over USB.

To create a better animated GIF, I adapted the example ffmpeg scripts from here.

The first step is to install ffmpeg. If you have Homebrew installed, then run brew install ffmpeg.

Next, copy this script:

#!/bin/sh

palette=`mktemp -d`"/palette.png"

# deletes the temp palette
function cleanup {
  rm "$palette"
  echo "Deleted temp palette at: $palette"
}

trap cleanup EXIT

filters="fps=5,setpts=0.8*PTS,scale=540:-1:flags=lanczos"

ffmpeg -v warning -i $1 -vf "$filters,palettegen" -y $palette
ffmpeg -v warning -i $1 -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$2"

into a movToGif.sh file, and then run chmod +x movToGif.sh.

Finally run ./movToGif.sh inFile.mov outFile.gif to convert the .mov file to a .gif

For anyone interested in what the filters in ffmpeg are doing, they're setting the output video framerate to 5 fps, speeding up the video to 1.25x to make the animation slightly snappier, and scaling the video down to 540 px wide while maintaining the aspect ratio using Lanczos resampling.

During the first pass, ffmpeg is generating an optimized color palette to use when encoding. The second pass is rendering the video to an animated GIF using that palette.

Clone this wiki locally