Video FX using Python and Moviepy

achang
3 min readJul 6, 2020

--

Diagram of video processing from MoviePy

Here I will share some of the basics video FX using moviepy. I hope you’ll enjoy it and make something awesome.

Tutorial: Getting Started Moviepy

In summary, Moviepy is a library that allows you to easily create video editing pipelines. You just need to chain many functions to a video object.

Example of video process pipeline from here

To install Moviepy you just need to install their pip package. Type this in the command line:

pip install moviepy

Extra: Let’s use Youtube DL to download sample videos to play with our code

Install it with:

pip install youtube_dl

Read and Write video files

The code will download a youtube video.

ydl_opts holds the options to download a youtube video. There are several options, such as only download the sound of the video. path has the youtube link. download function will download the video.

Use VideoFileClip to read the video file. It will return an object that will contain the video information. We can print some important information about the video: clip.duration video duration in seconds and clip.size the frame size [width, height].

Use AudioFileClip to read an audio file. Just like the video, it will get an audio object with the audio information.

clip has attribute audio. We can change the audio of the video by reading an audio file and assigning to clip.audio. The audio duration must match the video’s duration.

We can use subclip function to crop the either the audio or video. subclip takes in start and end times. There are different ways to represent time in moviepy.

can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: ‘01:03:05.35’. If end is not provided, it is assumed to be the duration of the clip (potentially infinite). If end is a negative value, it crops the end of the clip.

subclip is very useful, so it is recommended to try out doing some different things with it.

When finished editing the video object, you use write_videofile to output it into video file.

Compose and Resize

Another important skills are combining different videos clips to create some funny edits.

The video/audio object (clip/audio) can be combined by putting them in a list and then using concatenate_videoclips to create one clip out of a list of clips. The code below will download a list of video links, crop and concatenate them.

You can also use CompositeVideoClip. The difference is that CompositeVideoClip will combine the videos and not concatenate them. If it isn’t specified, clip1 wont start when clip0 ends. Both clip1 and clip0 will play at same time. You will need to set start of clip1 to begin after clip0 ends. Below is the part of code to change to use CompositeVideoClip.

Loop

Video loops are the essence of gifs and other kinds of video memes. We can use moviepy video fx to create loops.

clip.fx provides many out of the box video effects that can be applied to your videos. One of then is vfx.loop which will loop the video for the specified duration.

Try out other effects for fun.

Shake it

In addition to the video effects that already comes in Moviepy (vfx), we can extend it and create our own video effects. There are different ways to do this and you should check it out in the Moviepy docs.

Moviepy functions pass a frame counter that can be used to change some parameters for every frame. In this example, we use a lambda function to manipulate each frame’s position randomly to create a shaking effect.

set_pos will set the position of a video, but it needs a background to set position of the video on top of something. It needs the CompositeVideoClip to put the video on top of the background. In this example, background.png is just a blank image. You can get it however you want.

You can change the speed to see how it shakes faster or slower. The outcome of this code is this video:

You should check out the various cool Moviepy examples.

Congrats

Well done! You have learned important parts of Moviepy and video editing using python code.

Stay tuned for more video, music, anime, meme creating with python code.

Checkout following tutorials:

--

--