Ryanus commited on
Commit
1bce3e3
·
verified ·
1 Parent(s): d8af90e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
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(video):
11
- # 儲存上傳影片至暫存位置
12
- input_path = video
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()
 
 
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()