File size: 1,615 Bytes
4ff70b7
 
 
 
 
 
 
 
1bce3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ff70b7
 
 
 
1bce3e3
 
 
4ff70b7
 
1bce3e3
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import gradio as gr
import os
from scenedetect import open_video, SceneManager
from scenedetect.detectors import ContentDetector

OUTPUT_DIR = "outputs"
os.makedirs(OUTPUT_DIR, exist_ok=True)

def detect_scenes(video_path):
    if not video_path or not os.path.exists(video_path):
        return "未找到影片檔案,請確認上傳檔案無誤", None

    try:
        video = open_video(video_path)
        manager = SceneManager()
        manager.add_detector(ContentDetector())
        manager.detect_scenes(video)
        scene_list = manager.get_scene_list()

        # 組合分鏡資料為 CSV 內容
        rows = [["Scene Number", "Start Time", "End Time"]]
        for i, (start, end) in enumerate(scene_list):
            rows.append([str(i + 1), str(start.get_timecode()), str(end.get_timecode())])

        fname, _ = os.path.splitext(os.path.basename(video_path))
        csv_path = os.path.join(OUTPUT_DIR, fname + "_scenes.csv")
        with open(csv_path, "w", encoding='utf-8') as f:
            for row in rows:
                f.write(",".join(row) + "\n")

        return f"偵測到 {len(scene_list)} 組分鏡,CSV 可下載", csv_path
    except Exception as e:
        return f"解析失敗:{e}", None

iface = gr.Interface(
    fn=detect_scenes,
    inputs=gr.Video(label="上傳影片"),
    outputs=[gr.Textbox(label="分鏡結果"), gr.File(label="下載分鏡 CSV")],
    title="PySceneDetect 分鏡偵測(可儲存 CSV)",
    description="上傳影片,自動產生分鏡資訊,並可下載 CSV。支援 CPU 部署。"
)

if __name__ == "__main__":
    iface.launch()