从字节读取而不是文件名以转换音频 [英] Read from bytes not filename to convert audio

查看:184
本文介绍了从字节读取而不是文件名以转换音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB中以BSON的形式存储了小型声音文件. 任务是从数据库中检索二进制数据,将其转换为适当的格式,然后发送回前端. 问题在于转换.我发现pydub可以用于此目的.

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.

我的代码如下

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

问题在于Audiosegment行,因为它不符合 AudioSegment.from_wav("/input/file.wav").export("/output/file.mp3", format="mp3") 并且仍然抛出错误'bytes' object has no attribute 'read'. pydub是否可以实现?

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?

推荐答案

AudioSegment.from_file()将文件路径或类似文件的对象作为第一个参数.假设您拥有整个wave文件的原始字节(包括wave标头,而不仅仅是音频数据),则可以:

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是每个样本中的字节数(因此,对于16位/CD音频,您将使用2)
  • frame_rate是每秒的采样数(又称采样率,对于CD音频为44100)
  • channels那里有多少个音频流,立体声是2,单声道是1,等等
    • 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
    • 这篇关于从字节读取而不是文件名以转换音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆