3

I have audio recordings saved on a server as base64, webm format and want to decode them with python into a wav file. I tried both suggested ways from a simliar question found here: How to decode base64 String directly to binary audio format. But I'm facing different problems with both suggestions:

The version using file.write resulted in a wav file that I could play with the VLC player and which included the expected content. But I got an error message when I tried to read it with matlab or python saying "unknown format" or "missing riff".

fin = open(dirName + file, "r") 
b64_str = fin.read()
fin.close()
# decode base64 string to original binary sound object
decodedData = base64.b64decode(b64_str)

webmfile = (outdir + file.split('.')[0] + ".webm")
wavfile = (outdir + file.split('.')[0] + ".wav")

with open(webmfile , 'wb') as wm:
    wm.write(decodedData)
with open(webmfile, 'rb') as wm:
    webmdata = pcm.read()
with open(wavfile, 'wb') as file:
        file.write(webmdata)

The version using writeframes with setting the parameters result in a file I could read with matlab or python but this one does not contain the expected content and is way shorter than expected.

with wave.open(wavfile, 'wb') as wav:
        wav.setparams((1, 2, 48000, 0, 'NONE', 'NONE'))
        wav.writeframes(webmdata)

Any ideas on how to solve this problem? The file itself is fine. Converting it with an online converter worked.

Marie
  • 41
  • 1
  • 3

1 Answers1

1

In case someone has the same problem at some point, here is the solution which worked for me: The following code creates a webm file from the base64 str:

import base64

decodedData = base64.b64decode(b64_str)
webmfile = (outdir + file.split('.')[0] + ".webm")
with open(webmfile, 'wb') as file:
       file.write(decodedData)

And for the conversion I used ffmpy:

from ffmpy import FFmpeg

ff = FFmpeg(
        executable = 'C:/Program Files/ffmpeg-2020/bin/ffmpeg.exe',
        inputs={file:None},
        outputs = {outfile:'-c:a pcm_f32le'})
ff.cmd
ff.run()

After those two steps, I was able to read the resulting wav file with matlab or any other program.

Marie
  • 41
  • 1
  • 3