File size: 920 Bytes
cb2e520
 
 
 
 
 
 
284b0a2
cb2e520
 
 
 
 
284b0a2
cb2e520
 
 
 
 
 
 
 
284b0a2
cb2e520
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# split_video.py
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