Python moviepy: Automate Video Editing in Python!

Filed Under: Python Modules
Video Editing With Moviepy

Readers, how are you doing today? I’ve been looking through websites for the past months for a video editing software to make my Youtube videos. And then I found this package that I will introduce you to today. Hopefully, it will blow your mind by showcasing the power of the Python moviepy library.

Challenges in video editing

There are many objectives to video editing. Some of the popular processes that can be solved by automation include:

  • composition many videos in a complicated but similar manner
  • automate video or GIF creation on a web server (Django, Flask, etc.)
  • tedious tasks, such as tracking objects by title insertions, cutting scenes, making end credits, subtitles, etc.
  • code your own video effect templates.
  • create animations from images created by another library (Matplotlib, seaborn, etc.)

What is Python moviepy?

MoviePy is a video editing library in Python: cutting, concatenating, adding names, video compositing (a.k.a. non-linear editing), video encoding, and custom effect development.

For some examples of its use, see the MoviePy gallery.

All the most popular audio and video formats, including GIF, can be read and written by MoviePy and it runs on Windows/Mac/Linux.

Features of Python moviepy:

  • Plain and intuitive. On one side, simple operations can be performed. For beginners, the code is easy to read and easy to understand.
  • Flexible. You have complete control over the video and audio clips, and it is as simple as Py to make your own effects.
  • Portable. The code uses software that is very popular (Numpy and FFMPEG) and can run on almost any computer with almost any Python version.

What it can’t do:

  • read from a webcam
  • render a video live on a distant machine

Python MoviePy Tutorial – Automating Video Editing

Let’s get right to the meat of the tutorial now! How to automate your video editing process with the use of Python moviepy.

1. Join Video Clips with Python

Two easy ways to bring clips together are to concatenate them (to play them in a single long frame, one after the other) or stack them (to them side by side in a single larger clip). The last clip is a clip that runs one after the other in clips 1, 2, and 3:

from moviepy.editor import *
clip1 = VideoFileClip("myvideo.mp4")
clip2 = VideoFileClip("myvideo2.mp4").subclip(50,60)
clip3 = VideoFileClip("myvideo3.mp4")

final_clip = concatenate_videoclips([clip1,clip2,clip3])
final_clip.write_videofile("concat.mp4")

The subclip function here lets you cut a part of the video. The syntax of subclip is subclip(self, t_start=0, t_end=None).

The time can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: ‘01:03:05.35’. If t_end is not provided, it is assumed to be the duration of the clip.

Notice that there is no need for the clips to be the same height.

If they aren’t, they will all appear centred in a clip wide enough to accommodate the largest of them, optionally filling the borders with a colour of your choice.

2. Stacking Video Clips

You can split your screen into multiple videos playing on each too. It’s called stacking, and is done with clip_array function:

from moviepy.editor import VideoFileClip, clips_array, vfx
clip1 = VideoFileClip("myvideo.mp4").margin(10)
clip2 = clip1.fx( vfx.mirror_x)
clip3 = clip1.fx( vfx.mirror_y)
clip4 = clip1.resize(0.50)
final_clip = clips_array([[clip1, clip2],
                          [clip3, clip4]])
final_clip.resize(width=480).write_videofile("stacked_vid.mp4")
Video Stacking 1
Video Stacking 1

3. Floating Video Windows

If you want to float video windows on top of each other, you can use the CompositeVideoClip method in the moviepy library.

video = CompositeVideoClip([clip1,clip2,clip3])

Think of this as layering videos on top of each other. If all videos are of different sizes, you’ll see all the videos playing behind each other. But, if clip2 and clip3 are the same size as clip1, then only clip3 at the top of the video is visible… unless clip3 and clip2 have masks that conceal parts of them.

Notice that the composition has the scale of its first clip by chance (as it is generally a background).

Video Width Height
Video Width Height

But often you want to make your clips float in a larger composition, so you can specify the final composition scale.

4. Set Start Time for Video Clips

You can also set the time for the floating window to appear:

clip1 = clip1.set_start(5) # start after 5 seconds

So we can compose a clip like:

video = CompositeVideoClip([clip1, # starts at t=0
                            clip2.set_start(10), # start at t=10s
                            clip3.set_start(15)]) # start at t=15s

5. Adding Transitions to Video Clips using Python MoviePy

In the example above, maybe clip2 will start before clip1 is over. In this case you can make clip2 appear with a cross-fade transition of one second:

video = CompositeVideoClip([clip1, # starts at t=0
clip2.set_start(10).crossfadein(1),
clip3.set_start(15).crossfadein(1.5)])

The crossfade is the most popular transition ever, but there are others in the library.

Ending Note

This is going to be the most interesting library for you to work on if you are into media and video editing. Play around with the MoviePy library enough and you might even be able to automate most of your repetitive workflows.

Hope you learned something interesting here! Let us know your thoughts in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content