使用 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...