Read from bytes not filename to convert audio

大城市里の小女人 提交于 2019-12-05 20:19:16

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!