Update app.py
Browse files
app.py
CHANGED
|
@@ -3,39 +3,42 @@ import os
|
|
| 3 |
from scenedetect import open_video, SceneManager
|
| 4 |
from scenedetect.detectors import ContentDetector
|
| 5 |
|
| 6 |
-
# 儲存 output 路徑
|
| 7 |
OUTPUT_DIR = "outputs"
|
| 8 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 9 |
|
| 10 |
-
def detect_scenes(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=detect_scenes,
|
| 35 |
inputs=gr.Video(label="上傳影片"),
|
| 36 |
-
outputs=[gr.Textbox(label="分鏡結果"), gr.File(label="
|
| 37 |
-
title="PySceneDetect
|
| 38 |
-
description="
|
| 39 |
)
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
| 3 |
from scenedetect import open_video, SceneManager
|
| 4 |
from scenedetect.detectors import ContentDetector
|
| 5 |
|
|
|
|
| 6 |
OUTPUT_DIR = "outputs"
|
| 7 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 8 |
|
| 9 |
+
def detect_scenes(video_path):
|
| 10 |
+
if not video_path or not os.path.exists(video_path):
|
| 11 |
+
return "未找到影片檔案,請確認上傳檔案無誤", None
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
video = open_video(video_path)
|
| 15 |
+
manager = SceneManager()
|
| 16 |
+
manager.add_detector(ContentDetector())
|
| 17 |
+
manager.detect_scenes(video)
|
| 18 |
+
scene_list = manager.get_scene_list()
|
| 19 |
+
|
| 20 |
+
# 組合分鏡資料為 CSV 內容
|
| 21 |
+
rows = [["Scene Number", "Start Time", "End Time"]]
|
| 22 |
+
for i, (start, end) in enumerate(scene_list):
|
| 23 |
+
rows.append([str(i + 1), str(start.get_timecode()), str(end.get_timecode())])
|
| 24 |
+
|
| 25 |
+
fname, _ = os.path.splitext(os.path.basename(video_path))
|
| 26 |
+
csv_path = os.path.join(OUTPUT_DIR, fname + "_scenes.csv")
|
| 27 |
+
with open(csv_path, "w", encoding='utf-8') as f:
|
| 28 |
+
for row in rows:
|
| 29 |
+
f.write(",".join(row) + "\n")
|
| 30 |
+
|
| 31 |
+
return f"偵測到 {len(scene_list)} 組分鏡,CSV 可下載", csv_path
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"解析失敗:{e}", None
|
| 34 |
|
| 35 |
iface = gr.Interface(
|
| 36 |
fn=detect_scenes,
|
| 37 |
inputs=gr.Video(label="上傳影片"),
|
| 38 |
+
outputs=[gr.Textbox(label="分鏡結果"), gr.File(label="下載分鏡 CSV")],
|
| 39 |
+
title="PySceneDetect 分鏡偵測(可儲存 CSV)",
|
| 40 |
+
description="上傳影片,自動產生分鏡資訊,並可下載 CSV。支援 CPU 部署。"
|
| 41 |
)
|
| 42 |
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
iface.launch()
|