Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def edit_video(input_video):
|
| 6 |
if not input_video:
|
| 7 |
-
return "請上傳影片檔案。"
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
import shutil
|
| 6 |
|
| 7 |
def edit_video(input_video):
|
| 8 |
if not input_video:
|
| 9 |
+
return "請上傳影片檔案。", None
|
| 10 |
|
| 11 |
+
try:
|
| 12 |
+
# 創建臨時輸出檔案
|
| 13 |
+
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as tmp_file:
|
| 14 |
+
output_path = tmp_file.name
|
| 15 |
+
|
| 16 |
+
# 使用 subprocess 調用 auto-editor 命令
|
| 17 |
+
cmd = [
|
| 18 |
+
'auto-editor',
|
| 19 |
+
input_video,
|
| 20 |
+
'--margin', '0.2sec',
|
| 21 |
+
'--output', output_path
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# 執行命令
|
| 25 |
+
result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
|
| 26 |
+
|
| 27 |
+
if result.returncode == 0:
|
| 28 |
+
return "編輯完成!", output_path
|
| 29 |
+
else:
|
| 30 |
+
return f"錯誤:{result.stderr}", None
|
| 31 |
+
|
| 32 |
+
except subprocess.TimeoutExpired:
|
| 33 |
+
return "處理超時(5分鐘限制)", None
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"處理錯誤:{str(e)}", None
|
| 36 |
|
| 37 |
+
# 創建 Gradio 介面
|
| 38 |
+
with gr.Blocks(title="Auto-Editor Demo") as iface:
|
| 39 |
+
gr.Markdown("# Auto-Editor 影片編輯工具")
|
| 40 |
+
gr.Markdown("上傳影片,自動移除靜音部分。處理時間視檔案大小而定。")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
with gr.Column():
|
| 44 |
+
input_video = gr.File(
|
| 45 |
+
label="上傳影片檔案",
|
| 46 |
+
file_types=['video']
|
| 47 |
+
)
|
| 48 |
+
edit_btn = gr.Button("開始編輯", variant="primary")
|
| 49 |
+
|
| 50 |
+
with gr.Column():
|
| 51 |
+
status_text = gr.Textbox(label="狀態")
|
| 52 |
+
output_file = gr.File(label="下載編輯後的檔案")
|
| 53 |
+
|
| 54 |
+
edit_btn.click(
|
| 55 |
+
fn=edit_video,
|
| 56 |
+
inputs=[input_video],
|
| 57 |
+
outputs=[status_text, output_file]
|
| 58 |
+
)
|
| 59 |
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
iface.launch()
|