通过pydub从.raw到.wav(音频段)听起来很嘈杂



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

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(文件是否有清晰的声音?我想知道问题出在哪里。崩溃的文件?或者代码不正确?

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

pydub假设任何文件都是原始波形,如果文件名以raw结尾。而且也没有办法将-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')

最新更新