python - .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)文件是否有清晰的声音?我想知道是什么问题。崩溃的文件?或不正确的代码?
解决方案
当我尝试将 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')
推荐阅读
- c++ - Mac VS“不支持或定义代码语言”
- assembly - 写入数据段时出现段错误,即使使用 brk() 分配了足够的空间
- javascript - 如何使用 javascript 使用 map 函数在 html 元素中呈现对象数组?纯Javascript
- javascript - 在 Azure Web 应用程序中对本地文件执行 GET 时出现“404 未找到”
- android - 在反应原生图像组件中处理个人资料图像的正确方法是什么?
- c# - C# WPF 将多个位图合并为一个
- java - DexClassLoader 和 GooglePlay 政策
- android - 颤振来自 adb 的意外失败:无效参数:源不能为空 在设备上启动应用程序时出错
- java - Clojure Java 互操作性
- r - 选择其中一行或多行满足特定条件的组