Create GIF from movie sequence
2015-09-22 23:15
You know all those funny GIFs with sequences from movies? I got curious and wanted to see how I could create one of my own.
This is a simple way to do it utilizing ffmpeg in conjunction with gimp or imagemagick.
Capture Images From a Movie Sequence
It's very easy to capture images from a movie sequence. Here is an example that I hope is pretty easy to understand:
ffmpeg -i themovie.mp4 -ss 134 -t 10 -r 10 img-%03d.png
This captures an image every 10 millisecond (-r 10
) for a 10 second sequence (-t 10
) starting at second 134 (-ss 134
) from the input file themovie.mp4
and outputs them as img-001.png
to img-100.png
.
NOTE: Regarding the ffmpeg/avconv/libav mess.
Create a GIF with Gimp
Open up Gimp and use File -> Open as Layers... to select the images created in previous step. Make sure the images is sorted by name (click the Name column title to sort by name otherwise). Click Open to load the images.
You can check the order of the images in the Layers dialog (Use Windows -> Dockable Dialogs -> Layers or ctrl + l if the dialog is not visable) and delete or manipulate the order.
Crop or scale image as you wish (for example use Image -> Scale Image... to scale the image) or manipulate single frames as you wish.
To save as a GIF select File -> Export As... and give it a name ending with the suffix .gif and click Export. The dialog Export Image as GIF shows up and you need to click the checkbox As animation and then Export.
Creta a GIF using Imagemagick
This is a pure command line option for creating a GIF.
mogrify -resize 640x480 *.png
convert -delay 10 -loop 0 *.png output.gif
The mogrify step is used to transform all images to a size of 640x480 pixels before convert is used to actually produce a GIF called output.gif
from all PNG images (*.png
) which will loop forever (-loop 0
) and have a delay of 10 (-delay 10
) between frames
Final words
I noticed the GIFs created with Gimp is smaller and seems to have better quality by default. Probably Gimp could be used in batch mode to automate the steps to create a GIF. There might also be a way to produce more optimized GIFs using Imagemagick.