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