Spaces:
Running
Running
| from pydub import AudioSegment | |
| import os | |
| import subprocess | |
| def process_audio(audio_path, bgm_path=None): | |
| """ | |
| 处理音频:混合背景音乐 | |
| """ | |
| audio = AudioSegment.from_file(audio_path) | |
| if bgm_path and os.path.exists(bgm_path): | |
| bgm = AudioSegment.from_file(bgm_path) | |
| if len(bgm) < len(audio): | |
| bgm = bgm * (len(audio) // len(bgm) + 1) | |
| bgm = bgm[:len(audio)] - 15 # 降低音量 | |
| mixed_audio = audio.overlay(bgm) | |
| else: | |
| mixed_audio = audio | |
| return mixed_audio | |
| def export_audio(audio_segment, output_path): | |
| """ | |
| 导出音频文件 | |
| """ | |
| audio_segment.export(output_path, format="wav") |