Ryanus commited on
Commit
9b530be
·
verified ·
1 Parent(s): d68023d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -15
app.py CHANGED
@@ -1,19 +1,81 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import CLIPProcessor, CLIPModel
 
 
4
 
5
- # 載入 FunCLIP (假設 FunCLIP 是基於 CLIP 結構衍生)
6
- model_name = "modelscope/funclip" # 可替換為真正FunCLIP的模型名稱
7
- device = "cpu"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- processor = CLIPProcessor.from_pretrained(model_name)
10
- model = CLIPModel.from_pretrained(model_name).to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def funclip_inference(image):
13
- inputs = processor(images=image, return_tensors="pt").to(device)
14
- outputs = model.get_image_features(**inputs)
15
- # 這裡可替換為 funclip 具體後處理
16
- return outputs.detach().cpu().numpy()
17
-
18
- iface = gr.Interface(fn=funclip_inference, inputs=gr.Image(type="pil"), outputs=gr.Textbox())
19
- iface.launch()
 
1
  import gradio as gr
2
+ import auto_editor
3
+ import os
4
+ import shutil
5
+ import subprocess
6
 
7
+ def edit_video(input_video, margin=0.2, min_clip=2, min_cut=2):
8
+ """
9
+ 使用 Auto-Editor 編輯影片,移除靜音部分並生成輸出。
10
+
11
+ 參數:
12
+ - input_video: 上傳的影片檔案路徑
13
+ - margin: 靜音檢測的邊距(秒)
14
+ - min_clip: 最小保留片段長度(秒)
15
+ - min_cut: 最小移除片段長度(秒)
16
+
17
+ 返回:
18
+ - 處理後的影片路徑
19
+ """
20
+ try:
21
+ # 確保輸出目錄存在
22
+ output_dir = "outputs"
23
+ os.makedirs(output_dir, exist_ok=True)
24
+
25
+ # 定義輸出檔案路徑
26
+ output_path = os.path.join(output_dir, "edited_video.mp4")
27
+
28
+ # 確保 Auto-Editor 和 FFmpeg 可執行
29
+ subprocess.run(["ffmpeg", "-version"], check=True)
30
+
31
+ # 執行 Auto-Editor,傳遞參數
32
+ auto_editor_args = [
33
+ input_video,
34
+ "--output_file", output_path,
35
+ "--margin", str(margin),
36
+ "--min_clip_length", str(min_clip),
37
+ "--min_cut_length", str(min_cut),
38
+ "--no_open" # 避免打開檔案
39
+ ]
40
+ auto_editor.main(auto_editor_args)
41
+
42
+ # 檢查輸出檔案是否存在
43
+ if not os.path.exists(output_path):
44
+ raise Exception("Auto-Editor 未能生成輸出檔案")
45
+
46
+ return output_path
47
+
48
+ except Exception as e:
49
+ return f"錯誤:{str(e)}"
50
 
51
+ # 定義 Gradio 介面
52
+ def create_gradio_interface():
53
+ with gr.Blocks() as interface:
54
+ gr.Markdown("# Auto-Editor on Hugging Face Spaces")
55
+ gr.Markdown("上傳影片,自動移除靜音部分並生成編輯後的影片。")
56
+
57
+ with gr.Row():
58
+ with gr.Column():
59
+ input_video = gr.Video(label="上傳影片")
60
+ margin = gr.Slider(minimum=0, maximum=1, value=0.2, step=0.01, label="靜音邊距 (秒)")
61
+ min_clip = gr.Slider(minimum=0, maximum=10, value=2, step=0.1, label="最小保留片段 (秒)")
62
+ min_cut = gr.Slider(minimum=0, maximum=10, value=2, step=0.1, label="最小移除片段 (秒)")
63
+ submit_button = gr.Button("開始編輯")
64
+
65
+ with gr.Column():
66
+ output_video = gr.Video(label="編輯後的影片")
67
+ output_text = gr.Textbox(label="狀態訊息")
68
+
69
+ # 綁定按鈕事件
70
+ submit_button.click(
71
+ fn=edit_video,
72
+ inputs=[input_video, margin, min_clip, min_cut],
73
+ outputs=[output_video, output_text]
74
+ )
75
+
76
+ return interface
77
 
78
+ # 啟動 Gradio 應用
79
+ if __name__ == "__main__":
80
+ interface = create_gradio_interface()
81
+ interface.launch()