|
|
|
|
|
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 |