| |
| import gradio as gr |
| from split_video import split_video |
| import os |
| import subprocess |
| import sys |
|
|
| def process_video(input_video, segment_duration, enable_mix=False): |
| if not input_video: |
| return "請上傳影片" |
|
|
| |
| output_dir = "temp_segments" |
| try: |
| outputs = split_video(input_video, output_dir, int(segment_duration)) |
| gr.info(f"切割完成:生成 {len(outputs)} 個短片") |
| except Exception as e: |
| return f"切割錯誤:{str(e)}" |
|
|
| |
| if enable_mix: |
| try: |
| cmd = [ |
| sys.executable, "main.py", |
| "--input_folder", output_dir, |
| "--output_folder", "mixed_output", |
| "--num_clips", "20" |
| ] |
| result = subprocess.run(cmd, capture_output=True, text=True) |
| if result.returncode != 0: |
| gr.warning(f"混剪警告:{result.stderr}") |
| else: |
| mixed_files = [os.path.join("mixed_output", f) for f in os.listdir("mixed_output") if f.endswith(".mp4")] |
| outputs.extend(mixed_files) |
| gr.info("混剪完成") |
| except Exception as e: |
| gr.warning(f"混剪錯誤:{str(e)}") |
|
|
| return outputs |
|
|
| iface = gr.Interface( |
| fn=process_video, |
| inputs=[ |
| gr.File(label="上傳長影片 (MP4)"), |
| gr.Number(label="每段長度 (秒)", value=60), |
| gr.Checkbox(label="啟用 pyToVideo2 混剪", value=False) |
| ], |
| outputs=gr.Files(label="下載短影片 / 混剪結果"), |
| title="pyToVideo2 影片切割與混剪", |
| description="上傳長影片切割成短片,並可選使用 pyToVideo2 混剪成 20 個新影片。" |
| ) |
|
|
| if __name__ == "__main__": |
| iface.launch() |