Processing Videos with MoviePy
Introduction
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.
Basic Concepts
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
Preview
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.
Installation
For this tutorial the following will be used:
Python 3.8.8
MoviePy 1.0.3
To install MoviePy, use the following command:
pip install moviepy |
Video Editing
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) |
The 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.
The 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
Use the 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(t_end
).
clip.subclip(t_start=15, t_end=25) |
To resize the video, use the resize()
method.
clip.resize(width=300) |
Specify either the width
or 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 (x1
, y1
, x2
, 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.
clip.crop(x1=200,x2=450,y1=20,y2=350) |
Effects
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 fx
and vfx
modules, then call the appropriate method as shown in the following code snippet.
import moviepy.video.fx | |
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], | |
[clip3, clip4]]) | |
final_clip.write_videofile("video/effects_array.mp4") |
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.
Conclusion
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.