Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,61 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from split_video import split_video
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
def process_video(input_video, segment_duration):
|
| 5 |
if not input_video:
|
| 6 |
return "請上傳影片"
|
|
|
|
|
|
|
| 7 |
output_dir = "temp_segments"
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
iface = gr.Interface(
|
| 12 |
fn=process_video,
|
| 13 |
inputs=[
|
| 14 |
gr.File(label="上傳長影片 (MP4)"),
|
| 15 |
-
gr.Number(label="每段長度 (秒)", value=60)
|
|
|
|
| 16 |
],
|
| 17 |
-
outputs=gr.Files(label="下載短影片"),
|
| 18 |
-
title="
|
| 19 |
-
description="
|
| 20 |
)
|
| 21 |
|
| 22 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from split_video import split_video
|
| 4 |
+
import os
|
| 5 |
+
import subprocess
|
| 6 |
+
import sys
|
| 7 |
|
| 8 |
+
def process_video(input_video, segment_duration, enable_mix=False):
|
| 9 |
if not input_video:
|
| 10 |
return "請上傳影片"
|
| 11 |
+
|
| 12 |
+
# 步驟 1: 切割長影片
|
| 13 |
output_dir = "temp_segments"
|
| 14 |
+
try:
|
| 15 |
+
outputs = split_video(input_video, output_dir, int(segment_duration))
|
| 16 |
+
gr.info(f"切割完成:生成 {len(outputs)} 個短片")
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"切割錯誤:{str(e)}"
|
| 19 |
+
|
| 20 |
+
# 步驟 2: 可選混剪(使用 pyToVideo2 的 main.py)
|
| 21 |
+
if enable_mix:
|
| 22 |
+
try:
|
| 23 |
+
# 假設 pyToVideo2 的 main.py 支援 --input_folder 和 --num_clips 參數
|
| 24 |
+
# 根據實際 main.py 調整命令(檢查 pyToVideo2 的 README 或代碼)
|
| 25 |
+
cmd = [
|
| 26 |
+
sys.executable, "main.py",
|
| 27 |
+
"--input_folder", output_dir,
|
| 28 |
+
"--output_folder", "mixed_output",
|
| 29 |
+
"--num_clips", "20" # 混剪成 20 個新影片
|
| 30 |
+
]
|
| 31 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 32 |
+
if result.returncode != 0:
|
| 33 |
+
gr.warning(f"混剪警告:{result.stderr}")
|
| 34 |
+
else:
|
| 35 |
+
# 添加混剪輸出
|
| 36 |
+
mixed_files = [os.path.join("mixed_output", f) for f in os.listdir("mixed_output") if f.endswith(".mp4")]
|
| 37 |
+
outputs.extend(mixed_files)
|
| 38 |
+
gr.info("混剪完成")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
gr.warning(f"混剪錯誤:{str(e)}")
|
| 41 |
+
|
| 42 |
+
# 清理臨時檔案(可選,節省空間)
|
| 43 |
+
# for f in outputs:
|
| 44 |
+
# if os.path.exists(f):
|
| 45 |
+
# os.remove(f)
|
| 46 |
+
|
| 47 |
+
return outputs # Gradio 提供下載
|
| 48 |
|
| 49 |
iface = gr.Interface(
|
| 50 |
fn=process_video,
|
| 51 |
inputs=[
|
| 52 |
gr.File(label="上傳長影片 (MP4)"),
|
| 53 |
+
gr.Number(label="每段長度 (秒)", value=60),
|
| 54 |
+
gr.Checkbox(label="啟用 pyToVideo2 混剪", value=False)
|
| 55 |
],
|
| 56 |
+
outputs=gr.Files(label="下載短影片 / 混剪結果"),
|
| 57 |
+
title="pyToVideo2 影片切割與混剪",
|
| 58 |
+
description="上傳長影片切割成短片,並可選使用 pyToVideo2 混剪成 20 個新影片。"
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|