Skip to content

Instantly share code, notes, and snippets.

@vitovalov
Created February 20, 2015 22:23
Show Gist options
  • Save vitovalov/5cd969a2989c9752a5cb to your computer and use it in GitHub Desktop.
Save vitovalov/5cd969a2989c9752a5cb to your computer and use it in GitHub Desktop.
mp4 to gif using ffmpeg and gifsicle(brew)
# Author: @vitovalov
# Description: pass it a mp4 and you will have a gif
# Dependencies: ffmpeg, gifsicle
if [ "$#" -le 1 ]; then
echo "\nUsage: sh gifit.sh <source path to original mp4> <delay between frames>"
echo "Warning! filenames shouldn't contain the extension\n"
exit 1
fi
ffmpeg -i $1.mp4 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=$2 > $1.gif
@geluso
Copy link

geluso commented Jul 10, 2018

Great stuff. Your second argument is undocumented. I passed zero as the second argument like gifit mymovie 0 and it worked flawlessly. Thank you!

@Nemo64
Copy link

Nemo64 commented Feb 14, 2019

Here is my version which I tried to optimise ~ hovever Gifsicle seems to not reduce the file size much when already using a palette.

for f in "$@"
do
	scale="fps=20,scale='min(480,iw)':'min(480,ih)':force_original_aspect_ratio=decrease"
	/usr/local/bin/ffmpeg -hwaccel videotoolbox -i "$f" -vf "$scale,palettegen" "$f.png"
	/usr/local/bin/ffmpeg -hwaccel videotoolbox -i "$f" -i "$f.png" -filter_complex "$scale,paletteuse" "$f.gif"
	rm "$f.png"
done

This script can be used as a service in Mac Automator for a quick right click action.

Outside of mac you'll have to either remove -hwaccel videotoolbox or replace it with the corresponding hwaccel implementation available on your platform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment