Update app.py
Browse files
app.py
CHANGED
@@ -25,6 +25,24 @@ def timestamp_to_seconds(timestamp):
|
|
25 |
h, m, s = map(float, timestamp.split(':'))
|
26 |
return 3600 * h + 60 * m + s
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
# 音频拼接函数:拼接目标音频和混合音频,返回目标音频的起始时间和结束时间作为字典
|
29 |
def combine_audio_with_time(target_audio, mixed_audio):
|
30 |
if pipeline is None:
|
@@ -36,13 +54,13 @@ def combine_audio_with_time(target_audio, mixed_audio):
|
|
36 |
|
37 |
# 加载目标说话人的样本音频
|
38 |
try:
|
39 |
-
target_audio_segment = AudioSegment.from_wav(target_audio)
|
40 |
except Exception as e:
|
41 |
return f"加载目标音频时出错: {e}"
|
42 |
|
43 |
# 加载混合音频
|
44 |
try:
|
45 |
-
mixed_audio_segment = AudioSegment.from_wav(mixed_audio)
|
46 |
except Exception as e:
|
47 |
return f"加载混合音频时出错: {e}"
|
48 |
|
|
|
25 |
h, m, s = map(float, timestamp.split(':'))
|
26 |
return 3600 * h + 60 * m + s
|
27 |
|
28 |
+
def convert_to_wav(audio_file):
|
29 |
+
try:
|
30 |
+
# 使用 pydub 打开上传的音频文件
|
31 |
+
audio = AudioSegment.from_file(audio_file)
|
32 |
+
|
33 |
+
# 创建一个 BytesIO 对象以存储转换后的音频
|
34 |
+
wav_output = BytesIO()
|
35 |
+
|
36 |
+
# 将音频转换为 wav 格式并存储在 BytesIO 对象中
|
37 |
+
audio.export(wav_output, format="wav")
|
38 |
+
|
39 |
+
# 将 BytesIO 对象的位置重置为开始位置,以便之后可以读取
|
40 |
+
wav_output.seek(0)
|
41 |
+
|
42 |
+
return wav_output # 返回转换后的 wav 音频
|
43 |
+
except Exception as e:
|
44 |
+
return f"音频转换失败: {e}"
|
45 |
+
|
46 |
# 音频拼接函数:拼接目标音频和混合音频,返回目标音频的起始时间和结束时间作为字典
|
47 |
def combine_audio_with_time(target_audio, mixed_audio):
|
48 |
if pipeline is None:
|
|
|
54 |
|
55 |
# 加载目标说话人的样本音频
|
56 |
try:
|
57 |
+
target_audio_segment = convert_to_wav(AudioSegment.from_wav(target_audio))
|
58 |
except Exception as e:
|
59 |
return f"加载目标音频时出错: {e}"
|
60 |
|
61 |
# 加载混合音频
|
62 |
try:
|
63 |
+
mixed_audio_segment = convert_to_wav(AudioSegment.from_wav(mixed_audio))
|
64 |
except Exception as e:
|
65 |
return f"加载混合音频时出错: {e}"
|
66 |
|