从 Python 将 wav 转换为 wav ulaw

Converting wav to wav ulaw from Python

正在尝试将 wav 文件转换为 python 中的 wav uLaw。

使用 pydub 的 AudioSegment,我可以使用以下方法转换为 mp3:

AudioSegment.from_wav(fromFile).export(toFile, format="mp3", bitrate="128k")

使用 ffmpeg pcm_mulaw 编解码器并指定 8 位、8kHz 的 wav uLaw 等效项是什么?

直接使用ffmpeg的命令是:

ffmpeg -i 1.wav -c:a pcm_mulaw -ar 8000 1ulaw.wav

在 Python 中找不到关于如何使用 pydub 的编解码器的参考。找到了一些例子,但他们提到了一些具体情况,然后提到你可以使用 ffmpeg 可以处理的任何东西,但没有提到如何引用编解码器。

问题是检查 pydub API Documentation 更接近,手边有 ffmpeg 命令。

指定导出格式为wav,可以指定编解码器,因为wav有多种可用的编解码器。

codec | example: "libvorbis" For formats that may contain content encoded with different codecs, you can specify the codec you'd like the encoder to use. For example, the "ogg" format is often used with the "libvorbis" codec. (requires ffmpeg)

然后可以传递参数以进一步定制输出。

parameters | example: ["-ac", "2"] Pass additional command line parameters to the ffmpeg call. These are added to the end of the call (in the output file section).

所以输出一个8kHz的mulaw WAV文件,使用一个WAV文件,可以用下面的代码实现

AudioSegment.from_wav(fromFile).export(toFile, format="wav", codec="pcm_mulaw", parameters=["-ar","8000"])