Discover more from Tech & Art with Eric
Processing Videos with MoviePy
There are a number of video editing tools around. Some are graphical user interface based tools, while others are command line based tools. A number allow you to batch together tasks so as to automate the editing and processing of video files.
In this tutorial, we shall briefly explore MoviePy. We shall then use some of its capabilities to edit a video file.
What is MoviePy?
MoviePy is a Python library that lets you automate various video editing tasks. You might want to process a long video file, possibly by cutting it up into chunks or replace the audio or perform some other special effects task. MoviePy is able to do all this.
MoviePy is a wrapper for ffmpeg which it uses to read and export audio and video files. It optionally uses ImageMagick for text and GIF file generation. NumPy, SciPy, PIL and Scikit-Image are used for processing the media, and adding advanced effects and enhancements. Finally, the output is generated by ffmpeg.
Clips are the central objects of MoviePy. These can be AudioClips or VideoClips. The actions you can perform on them are
Modify (cut, speed change, visual change)
Mix with other clips to form new clips
Render to output file
MoviePy is suitable for you if you want to
Process many videos in a complicated manner
Automate creation of videos
Automate repetitive tasks
Customize effects unsupported by existing video editing tools
Create animations from images produced by other tools
MoviePy is not suitable for you if you want to
Do a frame by frame video analysis
Convert a video file
Turn series of images into a movie
The MoviePy documentation provides more details on the library.
For this tutorial the following will be used:
To install MoviePy, use the following command:
|pip install moviepy|
For this tutorial, we shall use the video file that you can access at this link.
Reading and Displaying a Video File
The first task shall be to read in the file and display it. For this, we shall import the required module, then get an instance of a VideoClip object. We use the
ipython_display() method to show the clip.
|import moviepy.editor as mpy|
|clip = mpy.VideoFileClip("https://eric-gitonga-articles.s3.eu-west-2.amazonaws.com/videos/sample.mp4")|
|clip_duration = clip.duration + 1|
|mpy.ipython_display(clip, width=500, maxduration=clip_duration)|
width parameter is optional in the
ipython_display() method. It is used to specify how wide the displayed video should be. Without it, the displayed video might be too big to be well displayed. You don’t need to specify the
height. MoviePy will calculate the expected height to preserve the aspect ratio of the displayed video clip.
maxduration parameter is also optional. By default, the clip is expected to be 60 seconds long. If the video clip you read in is longer than 60 seconds, a ValueError exception will be raised, with the error message indicating that you have exceeded the default maximum duration value.
To avoid this exception from being raised, you need to set the
maxduration parameter to a value greater than the duration of the video clip being read in. To find out what duration your clip has, use the
duration attribute. The drawback from raising the duration is increased use of memory.
Now that we have read in the video clip, let us process it in a number of ways.
Cut, Resize and Crop a Video File
size attribute to know the dimensions of your video. You can also use the
aspect_ratio attribute to get the clip’s aspect ratio. The
fps attribute shows the clip’s frames per second.
You can specify what portion of video you want displayed using the
subclip() method. You specify the start time (
t_start) and end time(
To resize the video, use the
Specify either the
height or both. MoviePy will get the proper value of the width or height required to maintain the video clip’s aspect ratio if you specify just one of them. If you specify width and height values that result in a different aspect ratio, MoviePy will report that the file is corrupt and can’t be played.
To crop the video, use the
crop() method. Specify the starting and ending x and y coordinates as needed (
y2). The x coordinate starts counting from the left at 0 to the right at the width of the clip. The y coordinate starts counting from the top at 0 to the bottom at the height of the clip.
MoviePy offers quite a list of effects one can apply to a video file. To get a full list, check out the documentation. For this tutorial, we shall explore just three effects, namely mirroring (on both x and y axis), rotating and converting to black and white.
To access these effects, import the
vfx modules, then call the appropriate method as shown in the following code snippet.
|from moviepy.editor import vfx|
|clip1 = clip.fx(vfx.mirror_x)|
|clip2 = clip.fx(vfx.mirror_y)|
|clip3 = clip.fx(vfx.rotate, 180)|
|clip4 = clip.fx(vfx.blackwhite)|
|final_clip = mpy.clips_array([[clip1, clip2],|
We the use the
clips_array() method to arrange the four generated clips into one clip, with the various clips situated as called out in the array. Finally we write the clip to a video file we can watch with any media player.
In this brief article, we saw what MoviePy is and walked through some of its commands. We then used this knowledge to carry out some edits on a video file, processing it and then saving the output to an mp4 file. You can access the Jupyter notebook on github.