File size: 1,024 Bytes
cb2e520
51bb5d2
 
cb2e520
 
 
 
 
51bb5d2
cb2e520
51bb5d2
 
 
284b0a2
51bb5d2
 
cb2e520
51bb5d2
cb2e520
51bb5d2
 
 
 
 
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
26
# split_video.py
import subprocess
import math
import os

def split_video(input_path, output_dir, segment_duration=60):
    if not os.path.exists(input_path):
        raise ValueError("輸入影片不存在")
    
    os.makedirs(output_dir, exist_ok=True)
    cmd_duration = ['ffprobe', '-v', 'quiet', '-show_entries', 'format=duration', '-of', 'csv=p=0', input_path]
    result = subprocess.run(cmd_duration, capture_output=True, text=True)
    total_duration = float(result.stdout.strip())

    num_segments = math.ceil(total_duration / segment_duration)
    outputs = []
    for i in range(num_segments):
        start_time = i * segment_duration
        output_file = os.path.join(output_dir, f"segment_{i+1}.mp4")
        cmd_split = [
            'ffmpeg', '-i', input_path, '-ss', str(start_time), '-t', str(segment_duration),
            '-c', 'copy', '-avoid_negative_ts', 'make_zero', output_file, '-y'
        ]
        subprocess.run(cmd_split, check=True)
        outputs.append(output_file)
    return outputs