Movie → Animated Gif on the command line
Here’s how I make animated gifs from movies using command line tools. This is an ad hoc workflow I’ve come up with, having no idea how you’re “supposed” to do this kind of stuff.
I use a Mac. The first step is to download MPlayerX from the Mac App Store. It’s free. "I thought you said this was using the command line?" I’m getting there. MPlayerX is an open source OS X derivation of MPlayer, the free and open source media player for Linux. Bundled in the package contents for the MPlayerX app is a command line utility, located here:
I created a symlink in my
/usr/local/bin directory for easy access on my
Alternatively, you could use ffmpeg.
First, we have to find the location in the movie that you want to turn into a gif.
mplayer -ao null -loop 0 -ss 02:09 -endpos 6 movie.mp4
This command will tell mplayer to open up a new window with no audio output (
-ao null), infinitely looping (
-loop 0) beginning at the 2 minute and 9 second mark (
-ss 02:09) and ending after 6 seconds (
-endpos 6). Adjust the options to seek to the position of the video you want and see what mplayer outputs. This command will accept whatever video format MPlayerX supports.
Second, we have to output that video into a sequence of images.
MPlayerX has a constrained set of output formats, but it does support
mplayer -ao null -ss 02:09 -endpos 6 movie.mp4 -vo png:outdir=Frames
This is similar to the previous command, except we have removed the
-loop argument and added a
png:outdir=Frames specifies that we want to output the frames of the video into a series of png images into the directory
outdir part of the argument will create the named directory in your current working directory.
Read the mplayer manpage at your leisure.
You should now have the frames of the video in a directory. Here, you can tweak and remove or duplicate frames for your gif, and resize them if need be (I recommend Automator for that task).
Making the gif.
brew install imagemagick && brew install imagesnap
And then use Ruby’s
sudo gem install gifme
You might want to remove that
sudo depending on your Ruby environment.
gifme -r -w 480 -d 5 Frames/*.png
This command takes the png files in the Frames directory and transforms them to an animated gif. The
-r argument reverses the animation, so that you can do a clean looping animation.
-w 480 resizes the gif so that it’s 480px wide.
-d 5 sets the delay between frames of animation. Adjust to your tastes.
Optimizing the gif.
gifsicle -O2 --colors 256 < original.gif > optimized.gif
The end result.
You have a handsome gif. Experiment by removing frames, playing with dimensions, etc. to further optimize. Here’s mine, from Louis C.K.’s Live at the Beacon Theater: