Sometimes producing a good animated GIF requires a few advanced tweaks, for which scripting can help. So I added a GIF export feature to MoviePy, a Python package originally written for video editing.
For this demo we will make a few GIFs out of this trailer:
You can download it with this command if you have Youtube-dl installed:
1
youtube-dl 2Jw-AeaU5WI -o frozen_trailer.mp4
Converting a video excerpt into a GIF
In what follows we import MoviePy, we open the video file, we select the part between 1’22.65 (1 minute 22.65 seconds) and 1’23.2, reduce its size (to 30% of the original) and save it as a GIF:
For my next GIF I will only keep the center of the screen. If you intend to use MoviePy, note that you can preview a clip with clip.preview(). During the preview clicking on a pixel will print its position, which is convenient for cropping with precision.
Many GIF makers like to freeze some parts of the GIF to reduce the file size and/or focus the attention on one part of the animation.
In the next GIF we freeze the left part of the clip. To do so we take a snapshot of
the clip at t=0.2 seconds, we crop this snapshot to only keep the left half, then we make a composite clip which superimposes the cropped snapshot on the original clip:
123456789101112
anna_olaf=(VideoFileClip("frozen_trailer.mp4").subclip(87.9,88.1).speedx(0.5)# Play at half speed.resize(.4))snapshot=(anna_olaf.crop(x2=anna_olaf.w/2)# remove right half.to_ImageClip(0.2)# snapshot of the clip at t=0.2s.set_duration(anna_olaf.duration))composition=CompositeVideoClip([anna_olaf,snapshot])composition.write_gif('anna_olaf.gif',fps=15)
Freezing a more complicated region
This time we will apply a custom mask to the snapshot to specify where it will be transparent (and let the animated part appear)
.
1234567891011121314151617
importmoviepy.video.tools.drawingasdwanna_kris=(VideoFileClip("frozen_trailer.mp4",audio=False).subclip((1,38.15),(1,38.5)).resize(.5))# coordinates p1,p2 define the edges of the maskmask=dw.color_split(anna_kris.size,p1=(445,20),p2=(345,275),grad_width=5)# blur the mask's edgessnapshot=(anna_kris.to_ImageClip().set_duration(anna_kris.duration).set_mask(ImageClip(mask,ismask=True))composition=CompositeVideoClip([anna_kris,snapshot]).speedx(0.2)# 'fuzz' (0-100) below is for gif compressioncomposition.write_gif('anna_kris.gif',fps=15,fuzz=3)
Time-symetrization
Surely you have noticed that in the previous GIFs, the end did not always look like the beginning. As a consequence, you could see a disruption every time the animation was restarted. A way to avoid this is to time-symetrize the clip, i.e. to make the clip play once forwards, then once backwards. This way the end of the clip really is the beginning of the clip. This creates a GIF that can loop fluidly, without a real beginning or end.
12345678910111213
deftime_symetrize(clip):""" Returns the clip played forwards then backwards. In case you are wondering, vfx (short for Video FX) is loaded by >>> from moviepy.editor import * """returnconcatenate([clip,clip.fx(vfx.time_mirror)])clip=(VideoFileClip("frozen_trailer.mp4",audio=False).subclip(36.5,36.9).resize(0.5).crop(x1=189,x2=433).fx(time_symetrize))clip.write_gif('sven.gif',fps=15,fuzz=2)
Ok, this might be a bad example of time symetrization,it makes the snow flakes go upwards in the second half of the animation.
Adding some text
In the next GIF there will be a text clip superimposed on the video clip.
123456789101112131415
olaf=(VideoFileClip("frozen_trailer.mp4",audio=False).subclip((1,21.6),(1,22.1)).resize(.5).speedx(0.5).fx(time_symetrize))# Many options are available for the text (requires ImageMagick)text=(TextClip("In my nightmares\nI see rabbits.",fontsize=30,color='white',font='Amiri-Bold',interline=-25).set_pos((20,190)).set_duration(olaf.duration))composition=CompositeVideoClip([olaf,text])composition.write_gif('olaf.gif',fps=10,fuzz=2)
Making the gif loopable
The following GIF features a lot of snow falling. Therefore it cannot be made loopable using time-symetrization (or you will snow floating upwards !). So we will make this animation loopable by having the beginning of the animation appear progressively (fade in) just before the end of the clip. The montage here is a little complicated, I cannot explain it better than with this picture:
The next clip (from the movie Charade) was almost loopable: you can see Carry Grant smiling, then making a funny face, then coming back to normal. The problem is that at the end of the excerpt Cary is not exactly in the same position, and he is not smiling as he was at the beginning. To correct this, we take a snapshot of the first frame and we make it appear progressively at the end. This seems to do the trick.
Let’s dive further into the scripting madness: we consider this video around 2’16 (edit: not the video I originally used, it was removed by the Youtube user, I add to find another link):
And we will remove the background to make this gif (with transparent background):
The main difficulty was to find what the background of the scene is. To do so, the script gathers a few images in which the little pigs are are different positions (so that every part part of the background is visible on at least several (actually most) of the slides, then it takes the pixel-per-pixel median of these pictures, which gives the background.
# Requires Scikit Images installedimportnumpyasnpimportskimage.morphologyasskmimportskimage.filterasskffrommoviepy.editorimport*### LOAD THE CLIPpigsPolka=(VideoFileClip("pigs_in_a_polka.mp4")).subclip((2,16.85),(2,35)).resize(.5).crop(x1=140,y1=41,x2=454,y2=314))### COMPUTE THE BACKGROUND# There is no single frame showing the background only (there# is always a little pig in the screen) so we use the median of# several carefully chosen frames to reconstitute the background.# I must have spent half an hour to find the right set of frames.times=(list(np.linspace(2.3,4.2,30))+list(np.linspace(6.0,7.1,30))+8*[6.2])frames_bg=[pigsPolka.get_frame(t)fortintimes]background=np.percentile(np.array(frames_bg),50,axis=0)### MASK GENERATIONdefmake_mask_frame(t):""" Computes the mask for the frame at time t """# THRESHOLD THE PIXEL-TO-PIXEL DIFFERENCE# BETWEEN THE FRAME AND THE BACKGROUNDim=pigsPolka.get_frame(t)mask=((im-background)**2).sum(axis=2)>1500# REMOVE SMALL OBJECTSmask=skm.remove_small_objects(mask)# REMOVE SMALL HOLES (BY DILATIATION/EROSION)selem=np.array([[1,1,1],[1,1,1],[1,1,1]])foriinrange(2):mask=skm.binary_dilation(mask,selem)foriinrange(2):mask=skm.binary_erosion(mask,selem)# BLUR THE MASK A LITTLEmask=skf.gaussian_filter(mask.astype(float),1.5)returnmaskmask=(VideoClip(make_mask_frame,ismask=True,duration=pigsPolka.duration)### LAST EFFECTS AND GIF GENERATIONfinal=(pigsPolka.set_mask(mask).subclip(12.95,15.9).fx(vfx.blackwhite)# black & white effect !final.write_gif('pigs_polka.gif',fps=10,fuzz=10)