Cut or trim a video using moviepy in Python

There are many resources to edit or trim a video on our PC but is there any way to cut out a portion of a video in Python? The answer is yes, and it can be done just by four lines of code 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  on your system by  pip install moviepy command on your terminal. After this, you are all set to write code.

or

conda  install  -c  conda-forge  moviepy

Cut a portion of a video file in Python using moviepy

This is the code that we will use for cutting out a portion of a video in Python:

from moviepy.editor import *
clip = VideoFileClip('Test_video.mp4')
clip1 = clip.subclip((4,30),(5,15))
clip1.write_videofile('edited.mp4',codec='libx264')
  • In the first line of code, we are importing everything quickly from our module-MoviePy.
  • In the next line, we are naming our video whose portion we are gonna cut(here- Test_video.mp4) as ‘clip’.
  • Furthermore, we are assigning the portion of our video that we want to cut as ‘clip1′. As you can see, we have entered the timings of the video’s required portion. Here we are cutting the portion of the video from 4.30-5.15 minutes.
  • And at last, we are downloading and save the required portion of the video as ‘edited.mp4‘.

Screenshot:

Cut or trim a video using moviepy in Python

CODE:

Below is another 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)

 Output

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.

Explanation

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 *