Skip to content

Instantly share code, notes, and snippets.

@stipsan
Created July 28, 2015 07:15
Show Gist options
  • Save stipsan/20c3df064c2b690dadc6 to your computer and use it in GitHub Desktop.
Save stipsan/20c3df064c2b690dadc6 to your computer and use it in GitHub Desktop.
Shell script for converting screenrecordings and mpeg4 movies to great quality gifs
# Convert an animated video to gif
#
# Works well with both screencaptures from quicktime (.mov) and recordings from iOS (.mp4)
#
# @param $1 - video file name like `animation.mov`
# @param $2 - the output gif name like `animation.gif`
# @param @optional $3 - resize parameter as widthxheight like `400x300`
#
# Example: rec2gif animation.mov animation.gif 400x300
# Requirements: ffmpeg and gifsicle. Can be downloaded via homebrew
# You can also use rec2gif got a 25fps framerate, great for most gifs. If the file is too big you can try rec2giflq which do 12fps.
# If you don't care about filesize and want the best quality use rec2gifhq with 60fps
#
function rec2giflq() {
if [ -n "$1" ]
then
mkdir pngs gifs
ffmpeg -i "$1" -r 12 pngs/frame_%04d.png
sips -s format gif pngs/*.png --out gifs/
cd gifs
if [ -z "$3" ]
then
gifsicle --delay 8 --colors 256 *.gif --optimize=3 --loopcount > ../"$2"
else
gifsicle --delay 8 --colors 256 *.gif --optimize=3 --loopcount --resize "$3" > ../"$2"
fi
cd ..
rm -rf pngs gifs
else
echo "Use video file as first parameter"
fi
}
function rec2gif() {
if [ -n "$1" ]
then
mkdir pngs gifs
ffmpeg -i "$1" -r 25 pngs/frame_%04d.png
sips -s format gif pngs/*.png --out gifs/
cd gifs
if [ -z "$3" ]
then
gifsicle --delay 4 --colors 256 *.gif --optimize=3 --loopcount > ../"$2"
else
gifsicle --delay 4 --colors 256 *.gif --optimize=3 --loopcount --resize "$3" > ../"$2"
fi
cd ..
rm -rf pngs gifs
else
echo "Use video file as first parameter"
fi
}
function rec2gifhq() {
if [ -n "$1" ]
then
mkdir pngs gifs
ffmpeg -i "$1" -r 50 pngs/frame_%04d.png
sips -s format gif pngs/*.png --out gifs/
cd gifs
if [ -z "$3" ]
then
gifsicle --delay 2 --colors 256 *.gif --optimize=3 --loopcount > ../"$2"
else
gifsicle --delay 2 --colors 256 *.gif --optimize=3 --loopcount --resize "$3" > ../"$2"
fi
cd ..
rm -rf pngs gifs
else
echo "Use video file as first parameter"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment