| |
| from moviepy.editor import VideoFileClip |
| import os |
|
|
| def split_video(input_path, output_dir, segment_duration=60): |
| if not os.path.exists(input_path): |
| raise ValueError("輸入影片不存在") |
|
|
| video = VideoFileClip(input_path) |
| total_duration = video.duration |
| num_segments = int(total_duration / segment_duration) + (1 if total_duration % segment_duration > 0 else 0) |
| os.makedirs(output_dir, exist_ok=True) |
| outputs = [] |
|
|
| for i in range(num_segments): |
| start = i * segment_duration |
| end = min((i + 1) * segment_duration, total_duration) |
| subclip = video.subclip(start, end) |
| output_file = os.path.join(output_dir, f"segment_{i+1}.mp4") |
| subclip.write_videofile(output_file, codec='libx264', audio_codec='aac', verbose=False, logger=None) |
| outputs.append(output_file) |
| subclip.close() |
|
|
| video.close() |
| return outputs |