Spaces:
Running
Running
Create modules/audio.py
Browse files- modules/audio.py +26 -0
modules/audio.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydub import AudioSegment
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
|
| 5 |
+
def process_audio(audio_path, bgm_path=None):
|
| 6 |
+
"""
|
| 7 |
+
处理音频:混合背景音乐
|
| 8 |
+
"""
|
| 9 |
+
audio = AudioSegment.from_file(audio_path)
|
| 10 |
+
|
| 11 |
+
if bgm_path and os.path.exists(bgm_path):
|
| 12 |
+
bgm = AudioSegment.from_file(bgm_path)
|
| 13 |
+
if len(bgm) < len(audio):
|
| 14 |
+
bgm = bgm * (len(audio) // len(bgm) + 1)
|
| 15 |
+
bgm = bgm[:len(audio)] - 15 # 降低音量
|
| 16 |
+
mixed_audio = audio.overlay(bgm)
|
| 17 |
+
else:
|
| 18 |
+
mixed_audio = audio
|
| 19 |
+
|
| 20 |
+
return mixed_audio
|
| 21 |
+
|
| 22 |
+
def export_audio(audio_segment, output_path):
|
| 23 |
+
"""
|
| 24 |
+
导出音频文件
|
| 25 |
+
"""
|
| 26 |
+
audio_segment.export(output_path, format="wav")
|