Cut or trim a video using moviepy in Python

In this post, we are going to learn how to trim or cut a video using the moviepy module in Python. Moviepy is a Python module that allows us to do some basic video editing like trimming or compressing a video. Trimming means removing a part of the video we don’t want in our original video. This operation is done by the VideoFileClip object. We use the cutout method to do the cropping operation. The cutout method takes two arguments, which are integers, i.e., the start and end times of the video until where we want to trim.

Be sure to install moviepy onto your system by  pip install moviepy command on your terminal. After this, you are all set to write code.


Below is given the Python program:

# Importing required module
from moviepy.editor import *
# uploading the video
video = VideoFileClip("video.mp4")
video = video.subclip(0, 20)
#time is always in seconds
# trimming some part of the video
video = video.cutout(5, 10)

# display clip
video.ipython_display(width = 360)


Moviepy - Building video __temp__.mp4.
MoviePy - Writing audio in __temp__TEMP_MPY_wvf_snd.mp3
MoviePy - Done.
Moviepy - Writing video __temp__.mp4

Moviepy - Done !
Moviepy - video ready __temp__.mp4

The output will give us a formatted video by the name “__temp__.mp4” and the duration of the video for this example will be 15 seconds.


About the modules imported

In the above example, we import the moviepy module which is a video editing module for video trimming, management, and other basic edits. Then, moviepy.editor allows us to use the editing options available in the moviepy module.

About the code

VideoFileClip is an object of the moviepy module which is used to perform the operations on the video. Then we have the cutout method. The cutout method on your code will follow this syntax video.cutout(st, et). This takes two integer arguments.

Where st = start time we want to trim/skip the video.

and et = end time we want the video to end after trimming.

These take integers as values and it always takes time in seconds. Moviepy allows ease of video editing and helps in the easy processing of the video.

video.ipython_display(width = 360) makes sure that the video is displaying with the width of 360.

With this example, we have completed our tutorial. We have successfully been able to trim our video using Python programming.

Also read: Merge or join two videos using moviepy in Python.

Leave a Reply

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