Search code examples
pythonffmpegpydub

Converting wav to wav ulaw from Python


Trying to convert a wav file to a wav uLaw in python.

Using pydub's AudioSegment I am able to convert to mp3 using the following :

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

What would be the equivalent for wav uLaw using the ffmpeg pcm_mulaw codec and specifying 8bit, 8kHz?

The command using ffmpeg directly is :

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

Can't find a reference to how to use codecs specifically in Python with pydub. Have found some examples but they mention a few specific cases and then mention you can use anything ffmpeg can handle but without any reference to how to reference codecs.


Solution

  • Was a question of inspecting the pydub API Documentation closer, with the ffmpeg command at hand.

    Specifying the export format as wav, the codec can be specified, given that there are several available codecs for 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 can then be passed to further tailor the output.

    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).

    So outputing a 8kHz mulaw WAV file, using a WAV file, would be achieved with the following code

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