Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 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 |
"""
|
|
@@ -15,7 +14,7 @@ def edit_video(input_video, margin=0.2, min_clip=2, min_cut=2):
|
|
| 15 |
- min_cut: 最小移除片段長度(秒)
|
| 16 |
|
| 17 |
返回:
|
| 18 |
-
-
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
# 確保輸出目錄存在
|
|
@@ -25,28 +24,37 @@ def edit_video(input_video, margin=0.2, min_clip=2, min_cut=2):
|
|
| 25 |
# 定義輸出檔案路徑
|
| 26 |
output_path = os.path.join(output_dir, "edited_video.mp4")
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
subprocess.run(["ffmpeg", "-version"],
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
#
|
| 32 |
auto_editor_args = [
|
|
|
|
| 33 |
input_video,
|
| 34 |
"--output_file", output_path,
|
| 35 |
-
"--margin",
|
| 36 |
-
"--min_clip_length",
|
| 37 |
-
"--min_cut_length",
|
| 38 |
-
"--no_open"
|
| 39 |
]
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# 檢查輸出檔案是否存在
|
| 43 |
if not os.path.exists(output_path):
|
| 44 |
-
|
| 45 |
|
| 46 |
-
return output_path
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
-
return f"錯誤:{str(e)}"
|
| 50 |
|
| 51 |
# 定義 Gradio 介面
|
| 52 |
def create_gradio_interface():
|
|
|
|
| 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 |
"""
|
|
|
|
| 14 |
- min_cut: 最小移除片段長度(秒)
|
| 15 |
|
| 16 |
返回:
|
| 17 |
+
- (output_video_path, status_message): 處理後的影片路徑和狀態訊息
|
| 18 |
"""
|
| 19 |
try:
|
| 20 |
# 確保輸出目錄存在
|
|
|
|
| 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():
|