9

I have small-sized sound files stored in MongoDB as BSON. Task is to retrieve Binary data from the database, convert it to an appropriate format and send back to the front end. The problem is with the converting. I have found pydub can be used for this.

My code is as follows

 query_param = json_data['retriever']
 query_param1 = query_param.replace('"', "");
 data = db.soundData
 y = data.find_one({'name': query_param1})
 s = y['data'] // here I retrieve the binary data 
 AudioSegment.from_file(s).export(x, format="mp3")
 return send_file(x, 'audio/mp3')

The question is with Audiosegment line as it does not follow the standard of AudioSegment.from_wav("/input/file.wav").export("/output/file.mp3", format="mp3") and an error of 'bytes' object has no attribute 'read' is still thrown. Is it achievable with pydub?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
O. Barinov
  • 161
  • 1
  • 1
  • 12

1 Answers1

22

AudioSegment.from_file() takes a file path or file-like object as it's first argument. Assuming you have the raw bytes of a whole wave file (including wave headers, not just the audio data) then you can:

import io
s = io.BytesIO(y['data'])
AudioSegment.from_file(s).export(x, format='mp3')

If you only have the bytes of the audio samples you would need to know some metadata about your audio data:

AudioSegment(y['data'], sample_width=???, frame_rate=???, channels=???)
  • sample_width is the number of bytes in each sample (so for 16-bit/CD audio, you'd use 2)
  • frame_rate is number of samples/second (aka, sample rate, for CD audio it's 44100)
  • channels how many audio streams are there, stereo is 2, mono is 1, etc
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
  • 2
    You sir are legit the greatest, I've used pydub for so many different things and every time, it's working great. Thank you! – Sophie Proud Apr 23 '19 at 19:49
  • @Jiaaro sir could u pls help , i m fasing same issue - edit and pass wav file without saving https://stackoverflow.com/questions/63467345/how-to-pass-edited-wav-between-functions-without-saving-wav-in-between?noredirect=1#comment112232633_63467345 – ERJAN Aug 19 '20 at 07:29
  • 1
    Worth noting that you may need to seek to 0 before AudioSegment.from_file(). This related issue helped https://stackoverflow.com/a/65340798/1786351 – J.X.Weiss Sep 13 '22 at 17:22