Python: Moviepy Module

Featured Img Moviepy

Hello fellow coder! Today in this tutorial we will be learning a very interesting way of editing and loading videos with the Moviepy module.

Introduction to Moviepy Module

MoviePy module is used for loading, downloading and editing videos from your system where editing includes cropping, rotating, trimming into smaller clips and other video effects.

It can also be used to edit audio in the video. It can also add watermarks on the video and other text items according to your preferences.

Pretty cool right?! So let’s learn to implement it now!

Implementing Moviepy

To implement moviepy, first we need to make sure that we have moviepy installed in our system using the pip install command.

After you have successfully installed the moviepy module, we are all set to implement the module! For the following tutorial, I would be using a video that can be found here. You can choose any video you like.

But before processing further, we need to import the moviepy module to make it ready for editing in the following way.

from moviepy.editor import *

1. Loading the Video

To load the video we make use of the VideoFileClip function and pass the path of the video file as a parameter.

I have used mp4 format, you can whichever format you prefer as moviepy module supports a variety of video formats.

We will store the output of the function inside a variable and to view the video recorded we make make use of the ipython_display function.

You can keep the parenthesis blank but I have provided a width property for better visualization.

The code and output for the whole process is displayed below. The video is perfectly playable.

from moviepy.editor import *
v1 = VideoFileClip("video.mp4")
v1.ipython_display(width = 400)
Output Loading Video Moviepy
Output Loading Video Moviepy

2. Editing the Video

Now that we know how to successfully load a video, let’s start to edit the video a little bit.

All the functions of the moviepy module are out of scope for a single tutorial, but you will learn a few tricks by the end of the tutorial.

We will be doing the following operations on the video loaded in the code above:

  1. Trimming the video to 15 seconds
  2. Rotating the video by an angle of 45 degrees
  3. Lowering the volume of the audio

Let’s learn how to do each edit one after another!

First, we have trimming for which we make use of the function subclip which takes the starting and ending time of the video in seconds.

We will be trimming the video to 15 seconds so we provide start as 0 and end as 15. The code to do the same is shown below.

v2 = v2.subclip(0, 15)

Next we have, rotating the video to a certain angle which is simply done by the rotate function which takes the angle of rotation. You can play around with this function.

We are choosing a angle of 45 degrees for which the code is shown below.

v2 = v2.rotate(45)

Last but not the least we will be setting the volume of the audio in the video which is performed by the help of volumex function which sets the maximum volume you need for the audio.

The code for the same is shown below.

v2 = v2.volumex(0.3)

Now that we know how the individual edits take place, lets combine all of them and see the output of the video.

The complete code and output is shown below.

from moviepy.editor import *
v2 = VideoFileClip("video.mp4")

v2 = v2.subclip(0, 15)
v2 = v2.rotate(45)
v2 = v2.volumex(0.3)

v2.ipython_display(width = 400)
Output Edited Video Moviepy
Output Edited Video Moviepy

Conclusion

Congratulations! You’ve just learned to load and edit a video with simple python coding. But these were just the basics, there’s a lot more to learn about MoviePy.

Stay tuned for more such tutorials! Happy coding!