Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
from scipy.io.wavfile import write
|
|
|
1 |
+
'''
|
2 |
+
import os
|
3 |
+
import shutil
|
4 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
5 |
+
from gradio_client import Client
|
6 |
+
|
7 |
+
def process_videos(input_folder, output_folder):
|
8 |
+
# 确保输出文件夹存在
|
9 |
+
if not os.path.exists(output_folder):
|
10 |
+
os.makedirs(output_folder)
|
11 |
+
|
12 |
+
# 初始化Gradio客户端
|
13 |
+
client = Client("http://127.0.0.1:7861")
|
14 |
+
|
15 |
+
# 遍历输入文件夹中的所有文件
|
16 |
+
for filename in os.listdir(input_folder):
|
17 |
+
if filename.endswith('.mp4'):
|
18 |
+
# 构建完整的文件路径
|
19 |
+
mp4_path = os.path.join(input_folder, filename)
|
20 |
+
txt_path = os.path.join(input_folder, filename.replace('.mp4', '.txt'))
|
21 |
+
|
22 |
+
try:
|
23 |
+
# 使用上下文管理器确保资源释放
|
24 |
+
with VideoFileClip(mp4_path) as video:
|
25 |
+
# 将MP4转换为WAV
|
26 |
+
temp_wav_path = os.path.join(input_folder, filename.replace('.mp4', '.wav'))
|
27 |
+
video.audio.write_audiofile(temp_wav_path, logger=None) # 禁用进度条避免冲突[8](@ref)
|
28 |
+
|
29 |
+
# 调用API处理音频
|
30 |
+
result = client.predict(temp_wav_path, api_name="/predict")
|
31 |
+
|
32 |
+
if len(result) >= 2:
|
33 |
+
processed_wav_path = result[1]
|
34 |
+
|
35 |
+
# 关键修复:加载为音频对象而非字符串
|
36 |
+
with AudioFileClip(processed_wav_path) as processed_audio:
|
37 |
+
# 用处理后的音频替换原始视频的音频
|
38 |
+
video_with_new_audio = video.set_audio(processed_audio)
|
39 |
+
|
40 |
+
# 保存处理后的视频到输出文件夹
|
41 |
+
output_mp4_path = os.path.join(output_folder, filename)
|
42 |
+
|
43 |
+
# 写入视频文件(禁用进度条)
|
44 |
+
video_with_new_audio.write_videofile(
|
45 |
+
output_mp4_path,
|
46 |
+
codec='libx264',
|
47 |
+
audio_codec='aac',
|
48 |
+
logger=None # 避免进度条导致的冲突[8](@ref)
|
49 |
+
)
|
50 |
+
|
51 |
+
# 复制对应的TXT文件
|
52 |
+
if os.path.exists(txt_path):
|
53 |
+
output_txt_path = os.path.join(output_folder, filename.replace('.mp4', '.txt'))
|
54 |
+
shutil.copy2(txt_path, output_txt_path)
|
55 |
+
|
56 |
+
else:
|
57 |
+
print(f"API返回结果不足,无法处理文件: {filename}")
|
58 |
+
|
59 |
+
# 删除临时文件
|
60 |
+
if os.path.exists(temp_wav_path):
|
61 |
+
os.remove(temp_wav_path)
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
print(f"处理文件 {filename} 时出错: {str(e)}")
|
65 |
+
|
66 |
+
# 使用示例
|
67 |
+
input_folder = "wan_gen_videos_HunyuanVideo_Foley_sound_captioned"
|
68 |
+
output_folder = "wan_gen_videos_HunyuanVideo_Foley_no_vocal_sound_captioned"
|
69 |
+
process_videos(input_folder, output_folder)
|
70 |
+
'''
|
71 |
+
|
72 |
import os
|
73 |
import gradio as gr
|
74 |
from scipy.io.wavfile import write
|