Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,81 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|