mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-18 12:40:06 +08:00
refactor(audio_convert.py): remove redundant functions
This commit is contained in:
@@ -62,52 +62,14 @@ def any_to_sil(any_path, sil_path):
|
|||||||
):
|
):
|
||||||
shutil.copy2(any_path, sil_path)
|
shutil.copy2(any_path, sil_path)
|
||||||
return 10000
|
return 10000
|
||||||
if any_path.endswith(".wav"):
|
audio = AudioSegment.from_file(any_path)
|
||||||
return pcm_to_sil(any_path, sil_path)
|
|
||||||
if any_path.endswith(".mp3"):
|
|
||||||
return mp3_to_sil(any_path, sil_path)
|
|
||||||
raise NotImplementedError("Not support file type: {}".format(any_path))
|
|
||||||
|
|
||||||
|
|
||||||
def mp3_to_wav(mp3_path, wav_path):
|
|
||||||
"""
|
|
||||||
把mp3格式转成pcm文件
|
|
||||||
"""
|
|
||||||
audio = AudioSegment.from_mp3(mp3_path)
|
|
||||||
audio.export(wav_path, format="wav")
|
|
||||||
|
|
||||||
|
|
||||||
def pcm_to_sil(pcm_path, silk_path):
|
|
||||||
"""
|
|
||||||
wav 文件转成 silk
|
|
||||||
return 声音长度,毫秒
|
|
||||||
"""
|
|
||||||
audio = AudioSegment.from_wav(pcm_path)
|
|
||||||
rate = find_closest_sil_supports(audio.frame_rate)
|
rate = find_closest_sil_supports(audio.frame_rate)
|
||||||
# Convert to PCM_s16
|
# Convert to PCM_s16
|
||||||
pcm_s16 = audio.set_sample_width(2)
|
pcm_s16 = audio.set_sample_width(2)
|
||||||
pcm_s16 = pcm_s16.set_frame_rate(rate)
|
pcm_s16 = pcm_s16.set_frame_rate(rate)
|
||||||
wav_data = pcm_s16.raw_data
|
wav_data = pcm_s16.raw_data
|
||||||
silk_data = pysilk.encode(wav_data, data_rate=rate, sample_rate=rate)
|
silk_data = pysilk.encode(wav_data, data_rate=rate, sample_rate=rate)
|
||||||
with open(silk_path, "wb") as f:
|
with open(sil_path, "wb") as f:
|
||||||
f.write(silk_data)
|
|
||||||
return audio.duration_seconds * 1000
|
|
||||||
|
|
||||||
|
|
||||||
def mp3_to_sil(mp3_path, silk_path):
|
|
||||||
"""
|
|
||||||
mp3 文件转成 silk
|
|
||||||
return 声音长度,毫秒
|
|
||||||
"""
|
|
||||||
audio = AudioSegment.from_mp3(mp3_path)
|
|
||||||
rate = find_closest_sil_supports(audio.frame_rate)
|
|
||||||
# Convert to PCM_s16
|
|
||||||
pcm_s16 = audio.set_sample_width(2)
|
|
||||||
pcm_s16 = pcm_s16.set_frame_rate(rate)
|
|
||||||
wav_data = pcm_s16.raw_data
|
|
||||||
silk_data = pysilk.encode(wav_data, data_rate=rate, sample_rate=rate)
|
|
||||||
# Save the silk file
|
|
||||||
with open(silk_path, "wb") as f:
|
|
||||||
f.write(silk_data)
|
f.write(silk_data)
|
||||||
return audio.duration_seconds * 1000
|
return audio.duration_seconds * 1000
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user