Home > Back-end >  How to extract multiple short video clips from a long video using python?
How to extract multiple short video clips from a long video using python?

Time:02-02

How to extract multiple smaller video clips from a long video using some python package, I need it as part of my video preprocessing for my project. ffmpeg is a method but its too complex. Any other method would be really helpful.

I tried using moviepy, but the documentation is not that clear, so I could only extract one video at a time and not multiple.

CodePudding user response:

from moviepy.video.io.VideoFileClip import VideoFileClip

def extract_clips(video_file, clip_duration, clip_start_times):
    clip_list = []
    with VideoFileClip(video_file) as video:
        for start_time in clip_start_times:
            clip = video.subclip(start_time, start_time   clip_duration)
            clip_list.append(clip)
    return clip_list

video_file = "path/to/your/video.mp4"
clip_duration = 5  # duration of each clip in seconds
clip_start_times = [0, 10, 20]  # start times of each clip in seconds

clips = extract_clips(video_file, clip_duration, clip_start_times)

# save the clips to disk
for i, clip in enumerate(clips):
    clip.write_videofile("clip_{}.mp4".format(i))

CodePudding user response:

To do this you can watch this very helpful youtube video: https://www.youtube.com/watch?v=Q2d1tYvTjRw

It teaches you how to use moviepy, you should be able to do this with the help of the video Hope it helps!!!!

  • Related