Moviepy is Awesome! — Part2

Tejas Bobhate
4 min readApr 13, 2018

Let’s make Boomerang ( by Instagram) like gifs.

Photo by Jakob Owens on Unsplash

Knowledge grows exponentially. The more we know, the greater our ability to learn, and the faster we expand our knowledge base — Dan Brown

True it is!

If you haven’t read Part1 of the series, go read it here.

This time let’s learn a few more handy and elegantly powerful methods from Moviepy.

We will do it via an example. Say we want to make a funny gif which plays forwards and backwards ( just like Boomerang from Instagram)

I like cats ( just like everyone else 😅). I found a funny cat video on youtube. I’m going to use it to make a Boomerang like gif.

Here’s the url for the video. I downloaded the video using youtube-dl package in python.

You can install the package with command,

pip install youtube_dl # for Python2
pip3 install youtube_dl # for Python3

Once the package is installed, you can download any youtube video using following command from terminal.

cd ~/Downloads
youtube-dl youtube_url

This will save the video in Downloads directory.

Here’s the python script that will make the gif.

from moviepy.editor import *
import moviepy.video.fx.all as vfx
clip = VideoFileClip(path/to/downloaded/video)clip = clip.resize(0.4)sub = clip.subclip(179, 182)sub = sub.fx(vfx.crop, x1=115, x2=399, y1=0, y2=288)clip2 = sub.speedx(final_duration=2)clip3 = clip2.fx(vfx.time_mirror)final = concatenate_videoclips([clip2, clip3])final.to_gif("boomerang_like_gif.gif", fps=25)

And here’s how it looks.

Weekend vibes :)

Let’s see how it’s done.

Step 1 :

We read the video clip as clip using VideoFileClip.

Step 2 :

Then we resize the video to 40% of it’s original dimensions. (I’m doing it only to keep the file size small).

clip = clip.resize(0.4)

You can also mention exact dimensions. For example, you can also do,

clip = clip.resize((284, 288))

where (284, 288) is in the form (width, height).

Step 3 :

Then, I’m taking out a subclip of the video from 179 sec to 182 sec (3 sec long).

Step 4 :

Next, I’m cropping out a part of the video. Here’s the frame from video at 179th second.

Frame at 179th sec in the video

As you can see, there are two green vertical strips which we don’t want in the final gif. Hence, I’m cropping it out using,

sub = sub.fx(vfx.crop, x1=115, x2=399, y1=0, y2=288)

what this line does is, crop out a portion of the video. x1, y1 are the co-ordinates of top left point from which you want to crop out the video. x2, y2 are the bottom right co-ordinates of the point till which you want to crop the video.

Step 5 :

Then next step is speeding up the video. Usually, in GIFs, the clip plays at faster rate than usual. The sub clip we have is 3 sec long. Let’s play it at 150% original speed so that it can be played within 2 sec.

This can be easily done with a function called speedx .

clip2 = sub.speedx(final_duration=2)

This will play the sub clip at 150% of it’s original speed.

Step 6 :

Next, we need to play the same clip in reverse. It can be achieved through time_mirror function.

clip3 = clip2.fx(vfx.time_mirror)

clip3 is just the time reversed version of clip2.

Step 7 :

Now, we need to stack clip2 and clip3 next to each other in time. That means, clip3 should start playing as soon as clip2 ends. To do this, we will use concatenate_videoclips method.

final = concatenate_videoclips([clip2, clip3])

Step 8 :

That is how simple it is! Now that we have built all the required pieces, let’s stitch them together and get the gif.

final.to_gif("boomerang_like_gif.gif", fps=25)

This will save a gif to disk.

Aha, your Boomerang like gif is ready, it’s time to watch it. Enjoy the moment!

Weekend vibes :)

Thank you for reading! Hope you enjoyed it.

Stay tuned for more.

--

--