使用 PyDub 转换并保存音频文件

Python 语音语言处理

Daniel Bourke

Machine Learning Engineer/YouTube Creator

导出音频文件

from pydub import AudioSegment

# 导入音频文件
wav_file = AudioSegment.from_file("wav_file.wav")

# 增加 10 分贝 louder_wav_file = wav_file + 10
# 导出增益后的音频 louder_wav_file.export(out_f="louder_wav_file.wav", format="wav")
<_io.BufferedRandom name='louder_wav_file.wav'>
Python 语音语言处理

批量重新格式化并导出音频

def make_wav(wrong_folder_path, right_folder_path):

# 遍历格式不正确的文件 for file in os.scandir(wrong_folder_path):
# 仅处理需修复的音频扩展名 if file.path.endswith(".mp3") or file.path.endswith(".flac"):
# 生成新的 .wav 文件名 out_file = right_folder_path + os.path.splitext(os.path.basename(file.path))[0] + ".wav"
# 读取音频并导出为 wav AudioSegment.from_file(file.path).export(out_file, format="wav")
print(f"Creating {out_file}")
Python 语音语言处理

批量重新格式化并导出音频

# 调用新函数
make_wav("data/wrong_formats/", "data/right_format/")
Creating data/right_types/wav_file.wav
Creating data/right_types/flac_file.wav
Creating data/right_types/mp3_file.wav
Python 语音语言处理

处理并导出

def make_no_static_louder(static_quiet, louder_no_static):
    # 遍历含噪且音量低的 wav 文件
    for file in os.scandir(static_quiet_folder_path):

# 生成新路径 out_file = louder_no_static + os.path.splitext(os.path.basename(file.path))[0] + ".wav"
# 读取音频 audio_file = AudioSegment.from_file(file.path)
# 去除前 3 秒,+10 分贝并导出 audio_file = (audio_file[3100:] + 10).export(out_file, format="wav") print(f"Creating {out_file}")
Python 语音语言处理

处理并导出

# 去噪并增益
make_no_static_louder("data/static_quiet/", "data/louder_no_static/")
Creating data/louder_no_static/speech-recognition-services.wav
Creating data/louder_no_static/order-issue.wav
Creating data/louder_no_static/help-with-acount.wav
Python 语音语言处理

轮到您了!

Python 语音语言处理

Preparing Video For Download...