Ryanus commited on
Commit
4ff70b7
·
verified ·
1 Parent(s): e8c47c5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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(video):
11
+ # 儲存上傳影片至暫存位置
12
+ input_path = video.name
13
+ # 初始化 scenedetect
14
+ video_stream = open_video(input_path)
15
+ scene_manager = SceneManager()
16
+ scene_manager.add_detector(ContentDetector())
17
+ scene_manager.detect_scenes(video_stream)
18
+
19
+ # 取得分鏡
20
+ scene_list = scene_manager.get_scene_list()
21
+ csv_rows = [["Scene Number", "Start Time", "End Time"]]
22
+ for i, (start, end) in enumerate(scene_list):
23
+ csv_rows.append([i+1, str(start.get_timecode()), str(end.get_timecode())])
24
+
25
+ # 輸出 CSV
26
+ csv_path = os.path.join(OUTPUT_DIR, os.path.splitext(os.path.basename(input_path)) + "_scenes.csv")
27
+ with open(csv_path, "w") as f:
28
+ for row in csv_rows:
29
+ f.write(",".join(map(str, row)) + "\n")
30
+
31
+ return f"偵測到 {len(scene_list)} 段分鏡,CSV 已儲存", csv_path
32
+
33
+ iface = gr.Interface(
34
+ fn=detect_scenes,
35
+ inputs=gr.Video(label="上傳影片"),
36
+ outputs=[gr.Textbox(label="分鏡結果"), gr.File(label="分鏡 CSV")],
37
+ title="PySceneDetect 分鏡檢測 (CPU)",
38
+ description="自動分鏡檢測並可下載分鏡資訊表。"
39
+ )
40
+
41
+ iface.launch()