Create an Animated GIF from a movie clip

From Pabut
Revision as of 19:44, 9 October 2015 by Pabut (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Decompose the video clip using ffmpeg into a bunch of individual GIFs. Each frame from the video becomes a GIF. Keep in mind, 1 second of video is 30 frames.

mkdir GIF-FOLDER
cd GIF-FOLDER
ffmpeg -i PATH_TO_VIDEO out%04d.gif

This will create GIF files with 4 digit sequence numbers (i.e. out0000.gif, out0001.gif, ect.)

If the video is large you can use the -ss and -t ffmpeg options to set a start time and duration.

Depending o the size of the source video you may want to scale it down. An iphone, for example shoots at 1080p for the resulting GIFs are 1920x1080 pixels. Additionally, the video may need to be rotated.

To Rotate the GIFs:

cd GIF-FOLDER
mkdir ../ROTATED-GIFS
find . -type f -exec convert {} -rotate 90 ../ROTATED-GIFS/{} \;

To scale the GIFs:

cd ROTATED-GIFS
mkdir ../SCALED-GIFS
#Adjust the dimension as needed remembering to maintain aspect ratio 
find . -type f -exec convert {} -resize 540x960 ../SCALED-GIFS/{} \;

You may also find you can or need to crop the images, you can do this as well. The crop dimensions are give as NEW SIZE + starting point. For example, if we're starting with a 540x960 frame, and we would like to shave 200 pixels off the top the input to crop would be 540x760+0+200. Origin is in the top left corner, so +0+200 means start the new image after skipping down 200 pixels and start from the left. Note, we subtracted the 200 pixels from the final height.

cd SCALED-GIFS
mkdir ../CROPPED-GIFS
find . -type f -exec convert {} -crop 540x760+0+200 ../CROPPED-GIFS/{} \;

Optionally, you can add a caption to the images.

cd CROPPED-GIFS
mkdir ../FINAL-FOLDER
find . -type f -exec convert -pointsize 120 -fill white -draw \
    'text 170,660 "FOX-1A is in Orbit!!" ' {} ../FINAL-FOLDER/{} \;

Finally, armed with our processed GIF images we can splice them all together to create the animated GIF:

convert -delay 1x30 -loop 0 FINAL-FOLDER/*.gif animated.gif

The "delay 1x30" maintains the playback speed at 30 frames per second and "loop 0" indicated the GIF should loop endlessly.