Read from bytes not filename for audio conversion
I have small sound files stored in MongoDB
as BSON
. The challenge is to extract the binary data from the database, convert it to the appropriate format, and send it back to the fore. The problem is the conversion. I found it pydub
for this.
My code looks like this
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 issue is the Audiosegment lineup as it is out of standard
AudioSegment.from_wav("/input/file.wav").export("/output/file.mp3", format="mp3")
and the error 'bytes' object has no attribute 'read'
is still being thrown. Is this achievable with pydub
?
source to share
AudioSegment.from_file()
accepts a file path or file-like object as its first argument. Assuming you have the raw bytes of the entire wave file (including wave headers, not just audio data), you can:
import io
s = io.BytesIO(y['data'])
AudioSegment.from_file(s).export(x, format='mp3')
If you only have bytes of audio recordings, you need to know some metadata about your audio data:
AudioSegment(y['data'], sample_width=???, frame_rate=???, channels=???)
-
sample_width
- the number of bytes in each example (so for 16 bit / CD audio you use2
) -
frame_rate
- number of samples / sec (aka, sampling rate, for CD audio it44100
) -
channels
how many audio streams are there, stereo -2
, mono -1
etc.
source to share