首页 > 解决方案 > .raw 到 .wav 通过 pydub(AudioSegment) 听起来很吵

问题描述

我想将 .raw 音频文件转换为 .wav 音频文件。所以,我使用下面的代码和 pydub AudioSegment

final = AudioSegment.from_file('input.raw', format='raw', frame_rate=8000, channels=1, sample_width=1).export('result.wav', format='wav')

顺便说一句,它的输出文件“result.wav”听起来很吵。实际上,我不确定“input.raw”文件的声音是否清晰(因为它是从 VoIP 电话的 RTP 数据包中获得的)。所以,我的问题是,如果输入(.raw)文件没有崩溃,输出(.wav)文件是否有清晰的声音?我想知道是什么问题。崩溃的文件?或不正确的代码?

标签: pythonvoiprtppydubaudiosegment

解决方案


当我尝试将 PCMU RAW 音频转换为 WAV 格式时遇到了类似的问题,我pydub通过GitHub 上的这个问题联系了作者,他的回复如下:

如果文件名以 raw 结尾,pydub 假定任何文件都是原始波形。而且也没有办法将 -ar 8000 注入转换命令(告诉 ffmpeg 音频是每秒 8000 个样本)

所以解决方法是手动打开文件,并明确告诉pydub文件的格式是这样的:

# open the file ourselves so that pydub doesn't try to inspect the file name
with open('input.raw', 'rb') as raw_audio_f:

    # explicitly tell pydub the format for your file
    # use ffmpeg -i format | grep PCM to figure out what to string value to use
    sound = AudioSegment.from_file(raw_audio_f, format="mulaw")

    # override the default sample rate with the rate we know is correct
    sound.frame_rate = 8000

    # then export it
    sound.export('result.wav')

推荐阅读