Ryanus commited on
Commit
7ed4b40
·
verified ·
1 Parent(s): 0b50bc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -82
app.py CHANGED
@@ -1,89 +1,23 @@
1
  import gradio as gr
 
2
  import os
3
- import subprocess
4
- import sys
5
 
6
- def edit_video(input_video, margin=0.2, min_clip=2, min_cut=2):
7
- """
8
- 使用 Auto-Editor 編輯影片,移除靜音部分並生成輸出。
9
 
10
- 參數:
11
- - input_video: 上傳的影片檔案路徑
12
- - margin: 靜音檢測的邊距(秒)
13
- - min_clip: 最小保留片段長度(秒)
14
- - min_cut: 最小移除片段長度(秒)
15
 
16
- 返回:
17
- - (output_video_path, status_message): 處理後的影片路徑和狀態訊息
18
- """
19
- try:
20
- # 確保輸出目錄存在
21
- output_dir = "outputs"
22
- os.makedirs(output_dir, exist_ok=True)
23
-
24
- # 定義輸出檔案路徑
25
- output_path = os.path.join(output_dir, "edited_video.mp4")
26
-
27
- # 檢查 FFmpeg 是否可用
28
- result = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True)
29
- if result.returncode != 0:
30
- return None, "錯誤:FFmpeg 未正確安裝,請檢查環境配置。"
31
-
32
- # 使用 Auto-Editor 的 CLI 命令
33
- auto_editor_args = [
34
- "auto-editor",
35
- input_video,
36
- "--output_file", output_path,
37
- "--margin", f"{margin}s",
38
- "--min_clip_length", f"{min_clip}s",
39
- "--min_cut_length", f"{min_cut}s",
40
- "--no_open"
41
- ]
42
-
43
- # 執行 Auto-Editor
44
- result = subprocess.run(auto_editor_args, capture_output=True, text=True)
45
-
46
- # 檢查命令是否成功
47
- if result.returncode != 0:
48
- return None, f"錯誤:Auto-Editor 執行失敗 - {result.stderr}"
49
-
50
- # 檢查輸出檔案是否存在
51
- if not os.path.exists(output_path):
52
- return None, "錯誤:Auto-Editor 未能生成輸出檔案。"
53
-
54
- return output_path, "成功:影片已處理完成!"
55
-
56
- except Exception as e:
57
- return None, f"錯誤:{str(e)}"
58
 
59
- # 定義 Gradio 介面
60
- def create_gradio_interface():
61
- with gr.Blocks() as interface:
62
- gr.Markdown("# Auto-Editor on Hugging Face Spaces")
63
- gr.Markdown("上傳影片,自動移除靜音部分並生成編輯後的影片。")
64
-
65
- with gr.Row():
66
- with gr.Column():
67
- input_video = gr.Video(label="上傳影片")
68
- margin = gr.Slider(minimum=0, maximum=1, value=0.2, step=0.01, label="靜音邊距 (秒)")
69
- min_clip = gr.Slider(minimum=0, maximum=10, value=2, step=0.1, label="最小保留片段 (秒)")
70
- min_cut = gr.Slider(minimum=0, maximum=10, value=2, step=0.1, label="最小移除片段 (秒)")
71
- submit_button = gr.Button("開始編輯")
72
-
73
- with gr.Column():
74
- output_video = gr.Video(label="編輯後的影片")
75
- output_text = gr.Textbox(label="狀態訊息")
76
-
77
- # 綁定按鈕事件
78
- submit_button.click(
79
- fn=edit_video,
80
- inputs=[input_video, margin, min_clip, min_cut],
81
- outputs=[output_video, output_text]
82
- )
83
-
84
- return interface
85
 
86
- # 啟動 Gradio 應用
87
- if __name__ == "__main__":
88
- interface = create_gradio_interface()
89
- interface.launch()
 
1
  import gradio as gr
2
+ import autoeditor
3
  import os
 
 
4
 
5
+ def edit_video(input_video):
6
+ if not input_video:
7
+ return "請上傳影片檔案。"
8
 
9
+ output_path = "edited_video.mp4"
10
+ # 修正為 --output_file(下劃線)或 -o
11
+ autoeditor.main([input_video, "--edit", "audio", "--margin", "0.2sec", "--output_file", output_path])
 
 
12
 
13
+ return output_path # 返回可下載的檔案
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ iface = gr.Interface(
16
+ fn=edit_video,
17
+ inputs=gr.Video(label="上傳影片或音訊檔案"),
18
+ outputs=gr.File(label="下載編輯後的檔案"),
19
+ title="Auto-Editor Demo on CPU",
20
+ description="上傳影片,自動移除靜音部分。處理時間視檔案大小而定(CPU 環境下可能需數分鐘)。"
21
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ iface.launch()