.raw to .wav via pydub(AudioSegment) sounds noisy

2.2k Views Asked by At

I want to convert .raw audio file to .wav audio file. So, I use below code with pydub AudioSegment

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

btw, its output file 'result.wav' sounds very noisy. Actually, I'm not sure 'input.raw' file has clear sound (because it is gotten from RTP packet of VoIP phone call). So, my question is, does output(.wav) file have clear sound if input(.raw) file does not be crashed? I'm wondering what is the problem. crashed file? or not correct code?

1

There are 1 best solutions below

0
On

I ran into a similar issue when I was attempting to convert PCMU RAW audio to WAV format and I reached to the author of pydub via this issue on GitHub and here was his response:

pydub assumes any file is a raw wave if the filename ends with raw. And also doesn't have a way to inject the -ar 8000 into the conversion command (to tell ffmpeg that the audio is at 8000 samples per second)

So the workaround is to open the file manually and explicitly tell pydub what the format of the file is like so:

# 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')