Spaces:
Running
Running
''' | |
import os | |
import shutil | |
from moviepy.editor import VideoFileClip, AudioFileClip | |
from gradio_client import Client | |
def process_videos(input_folder, output_folder): | |
# 确保输出文件夹存在 | |
if not os.path.exists(output_folder): | |
os.makedirs(output_folder) | |
# 初始化Gradio客户端 | |
client = Client("http://127.0.0.1:7861") | |
# 遍历输入文件夹中的所有文件 | |
for filename in os.listdir(input_folder): | |
if filename.endswith('.mp4'): | |
# 构建完整的文件路径 | |
mp4_path = os.path.join(input_folder, filename) | |
txt_path = os.path.join(input_folder, filename.replace('.mp4', '.txt')) | |
try: | |
# 使用上下文管理器确保资源释放 | |
with VideoFileClip(mp4_path) as video: | |
# 将MP4转换为WAV | |
temp_wav_path = os.path.join(input_folder, filename.replace('.mp4', '.wav')) | |
video.audio.write_audiofile(temp_wav_path, logger=None) # 禁用进度条避免冲突[8](@ref) | |
# 调用API处理音频 | |
result = client.predict(temp_wav_path, api_name="/predict") | |
if len(result) >= 2: | |
processed_wav_path = result[1] | |
# 关键修复:加载为音频对象而非字符串 | |
with AudioFileClip(processed_wav_path) as processed_audio: | |
# 用处理后的音频替换原始视频的音频 | |
video_with_new_audio = video.set_audio(processed_audio) | |
# 保存处理后的视频到输出文件夹 | |
output_mp4_path = os.path.join(output_folder, filename) | |
# 写入视频文件(禁用进度条) | |
video_with_new_audio.write_videofile( | |
output_mp4_path, | |
codec='libx264', | |
audio_codec='aac', | |
logger=None # 避免进度条导致的冲突[8](@ref) | |
) | |
# 复制对应的TXT文件 | |
if os.path.exists(txt_path): | |
output_txt_path = os.path.join(output_folder, filename.replace('.mp4', '.txt')) | |
shutil.copy2(txt_path, output_txt_path) | |
else: | |
print(f"API返回结果不足,无法处理文件: {filename}") | |
# 删除临时文件 | |
if os.path.exists(temp_wav_path): | |
os.remove(temp_wav_path) | |
except Exception as e: | |
print(f"处理文件 {filename} 时出错: {str(e)}") | |
# 使用示例 | |
input_folder = "wan_gen_videos_HunyuanVideo_Foley_sound_captioned" | |
output_folder = "wan_gen_videos_HunyuanVideo_Foley_no_vocal_sound_captioned" | |
process_videos(input_folder, output_folder) | |
''' | |
import os | |
import gradio as gr | |
from scipy.io.wavfile import write | |
def inference(audio): | |
os.makedirs("out", exist_ok=True) | |
write('test.wav', audio[0], audio[1]) | |
os.system("python3 -m demucs.separate -n htdemucs --two-stems=vocals test.wav -o out") | |
return "./out/htdemucs/test/vocals.wav","./out/htdemucs/test/no_vocals.wav" | |
title = "Demucs Music Source Separation (v4)" | |
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1911.13254' target='_blank'>Music Source Separation in the Waveform Domain</a> | <a href='https://github.com/facebookresearch/demucs' target='_blank'>Github Repo</a> | <a href='https://www.thafx.com' target='_blank'>//THAFX</a></p>" | |
gr.Interface( | |
inference, | |
gr.Audio(type="numpy", label="Input"), | |
[gr.Audio(type="filepath", label="Vocals"),gr.Audio(type="filepath", label="No Vocals / Instrumental")], | |
title=title, | |
article=article, | |
).launch(share = True) |