Certainly! If you want to create multiple clips from a single video based on specified time slices, you can modify the existing code accordingly. Here’s how you can do it:
Step 1: Define the Time Slices
You can define the time slices you want to extract in a list. Each slice can be a tuple with the start and end times.
Step 2: Rewrite the Code
Below is an example of how to implement this:
from moviepy.video.io.VideoFileClip import VideoFileClip
# Specify the input video file
input_video_path = "input_video.mp4" # Change this to your input video file path
# Define the output path format (you can change this if necessary)
output_path_format = "clip_{:01d}.mp4" # This will create clip_1.mp4, clip_2.mp4, etc.
# Time slices for cutting the video (in seconds)
time_slices = [
(0 * 60 + 0, 1 * 60 + 30), # Clip 1: 0:00 - 1:30
(1 * 60 + 30, 3 * 60 + 50), # Clip 2: 1:30 - 3:50
(3 * 60 + 50, 5 * 60 + 0), # Clip 3: 3:50 - 5:00
# Add more clips as needed
]
# Load the video file
video = VideoFileClip(input_video_path)
# Loop through the specified time slices and create clips
for i, (start_time, end_time) in enumerate(time_slices, start=1):
# Cut the video clip
cut_video = video.subclip(start_time, end_time)
# Define the output file name
output_video_path = output_path_format.format(i)
# Write the result to a file
cut_video.write_videofile(output_video_path, codec="libx264", audio_codec="aac")
# Close the individual clip to free up resources
cut_video.close()
# Close the main video clip to free up resources
video.close()
Step 3: Modify and Run
-
Modify:
- Update the
input_video_path to point to your video file.
- Adjust the
time_slices list as needed to include all the segments you want to extract. Each tuple in the list represents the start and end times in seconds.
-
Run the Script:
- Save the modified code in a
.py file and execute it using Python, just like you did before.
Important Notes:
- The output files will be named
clip_1.mp4, clip_2.mp4, and so on, based on their order in the time_slices list. You can change the naming format if desired.
- If the clips you create cover large portions of the input video, this script could take some time to process, especially if the video is high resolution. Consider optimizing your time slices if performance becomes an issue.
- Each cut video clip is closed after writing to the file to free up resources. Make sure to do this, especially if creating a large number of clips.