here is my Python code for combining multiple short videos into one video in ascending order of the filename.
from moviepy.editor import VideoFileClip, concatenate_videoclips
import os
# Directory containing the video files
directory = "C:/Users/ASUS/Desktop/FYP_image_photo/RecFiles/20240507/h00"
video_files = [f for f in os.listdir(directory) if f.endswith(('.mp4', '.avi', '.mov'))]
video_files.sort()
# Check each file for compatibility issues
for file in video_files:
filepath = os.path.join(directory, file)
clip = VideoFileClip(filepath)
video_duration = clip.duration
audio_duration = clip.audio.duration if clip.audio is not None else 0
print(f"File: {file} - Video Duration: {video_duration}, Audio Duration: {audio_duration}")
# Close the clip to free resources
clip.close()
# Sort files alphabetically and numerically
video_files.sort()
print(f"Files to concatenate: {video_files}")
# Create a list of VideoFileClip objects
clips = [VideoFileClip(os.path.join(directory, file)) for file in video_files]
# Concatenate the video clips
final_clip = concatenate_videoclips(clips)
# Write the result to a new file
final_output = os.path.join(directory, "combined_video.mp4")
final_clip.write_videofile(final_output, codec='libx264')
# Close all the clips
for clip in clips:
clip.close()
print("Video has been successfully created at:", final_output)
But I encountered error: IndexError: index -100001 is out of bounds for axis 0 with size 0. I know ‘IndexError: index 0 is out of bounds for axis 0 with size 0’, but for index -100001, I have never encountered before, are they the same? because I cannot solve it using the same way.
New contributor
user24966183 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.