Update app.py
Browse files
app.py
CHANGED
|
@@ -7,16 +7,16 @@ import sys
|
|
| 7 |
|
| 8 |
def process_video(input_video, segment_duration, enable_mix=False):
|
| 9 |
if not input_video:
|
| 10 |
-
return
|
| 11 |
-
|
| 12 |
-
# 步驟 1: 切割長影片
|
| 13 |
-
output_dir = "temp_segments"
|
| 14 |
try:
|
|
|
|
|
|
|
| 15 |
outputs = split_video(input_video, output_dir, int(segment_duration))
|
| 16 |
-
|
| 17 |
except Exception as e:
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
# 步驟 2: 可選混剪(使用 pyToVideo2 的 main.py)
|
| 21 |
if enable_mix:
|
| 22 |
try:
|
|
@@ -28,26 +28,26 @@ def process_video(input_video, segment_duration, enable_mix=False):
|
|
| 28 |
]
|
| 29 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 30 |
if result.returncode != 0:
|
| 31 |
-
|
| 32 |
else:
|
| 33 |
mixed_files = [os.path.join("mixed_output", f) for f in os.listdir("mixed_output") if f.endswith(".mp4")]
|
| 34 |
outputs.extend(mixed_files)
|
| 35 |
-
|
| 36 |
except Exception as e:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return outputs
|
| 40 |
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=process_video,
|
| 43 |
inputs=[
|
| 44 |
-
gr.File(label="上傳長影片 (MP4)"),
|
| 45 |
gr.Number(label="每段長度 (秒)", value=60),
|
| 46 |
gr.Checkbox(label="啟用 pyToVideo2 混剪", value=False)
|
| 47 |
],
|
| 48 |
outputs=gr.Files(label="下載短影片 / 混剪結果"),
|
| 49 |
title="pyToVideo2 影片切割與混剪",
|
| 50 |
-
description="
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
|
|
|
| 7 |
|
| 8 |
def process_video(input_video, segment_duration, enable_mix=False):
|
| 9 |
if not input_video:
|
| 10 |
+
return [] # 返回空列表,避免無效輸入
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
+
# 步驟 1: 切割長影片
|
| 13 |
+
output_dir = "temp_segments"
|
| 14 |
outputs = split_video(input_video, output_dir, int(segment_duration))
|
| 15 |
+
print(f"切割完成:生成 {len(outputs)} 個短片") # 改用 print
|
| 16 |
except Exception as e:
|
| 17 |
+
print(f"切割錯誤:{str(e)}") # 改用 print
|
| 18 |
+
return [] # 返回空列表,防止錯誤訊息被誤解析
|
| 19 |
+
|
| 20 |
# 步驟 2: 可選混剪(使用 pyToVideo2 的 main.py)
|
| 21 |
if enable_mix:
|
| 22 |
try:
|
|
|
|
| 28 |
]
|
| 29 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 30 |
if result.returncode != 0:
|
| 31 |
+
print(f"混剪警告:{result.stderr}") # 改用 print
|
| 32 |
else:
|
| 33 |
mixed_files = [os.path.join("mixed_output", f) for f in os.listdir("mixed_output") if f.endswith(".mp4")]
|
| 34 |
outputs.extend(mixed_files)
|
| 35 |
+
print("混剪完成") # 改用 print
|
| 36 |
except Exception as e:
|
| 37 |
+
print(f"混剪錯誤:{str(e)}") # 改用 print
|
| 38 |
+
|
| 39 |
+
return outputs if outputs else [] # 確保返回檔案路徑列表或空列表
|
| 40 |
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=process_video,
|
| 43 |
inputs=[
|
| 44 |
+
gr.File(label="上傳長影片 (MP4)"),
|
| 45 |
gr.Number(label="每段長度 (秒)", value=60),
|
| 46 |
gr.Checkbox(label="啟用 pyToVideo2 混剪", value=False)
|
| 47 |
],
|
| 48 |
outputs=gr.Files(label="下載短影片 / 混剪結果"),
|
| 49 |
title="pyToVideo2 影片切割與混剪",
|
| 50 |
+
description="上傳長影片切割成短片(使用 FFmpeg),並可選使用 pyToVideo2 混剪成 20 個新影片。"
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|