检索音频-二进制文件-存储在我的Mlab中 [英] Retrieve audio - binary file- stored in my Mlab

查看:60
本文介绍了检索音频-二进制文件-存储在我的Mlab中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我已经使用MediaDevices.getUserMedia WebRTC API设置了一个应用程序.我已经录制了音频,并且所有东西在理论上都很好.但是我现在必须检索音频以收听它们.

Basically, I have set an application with the MediaDevices.getUserMedia WebRTC API. I have recorded the audios and all, all is good in theory. But I got to retrieve the audio now to listen to them.

我现在想知道如何在MongoDB数据库中下载音频Post.我尝试导出MongoDB数据库,但仅收到JSON或CSV文件,而没有收到音频文件.

I wonder now how to download audio Post in my MongoDB database. I have tried to export my MongoDB database but I receive only JSON or CSV file, not my audios files.

我听说过gridFS,但仅在图像处理上下文中.在深入探讨gridFS之前,我将带您考虑使用Mlab进行音频检索的想法.和MongoDB更广泛.另外,gridFS似乎是为大文件而设计的,但就我而言,我只想存储数百字节的小文件,所以gridFS似乎过分了吗?也许有一个更有效的解决方案?

I have heard about gridFS but only in image-handling context. Before dive deeply into gridFS I would get your thinks about audio retrieving with Mlab. and MongoDB more widely. Also, gridFS seems to be designed for large files, but in my case, I just want to store ridiculously little files of some hundreds of bytes, so gridFS seems maybe overkilled? Maybe there is a more efficient solution?

我很难转换我的数据以便将其存储在数据库中.

EDIT : I struggle to translate my data in order to store it in my database.

到目前为止,我的控制台返回了我:

So far, my console return me :

XML Parsing Error: syntax error

这是我的App.js:

Here my App.js :

// post section

async handleSubmit(e){

e.preventDefault();

Axios.post("/api/words",{

"sound":this.state.blob

})

//.then((res) => res.json())

.then((data) => console.log(data))

//pass submitted value to true in order to declench allDelete function

}


(...) 


// blob formatting section :

saveAudio() {

// convert saved chunks to blob

const blob = new Blob(this.chunks, {type: audioType});

this.setState({blob : blob})

谢谢.

推荐答案

对于小于16 MB的文件,不需要GridFS.您可以使用 BSON的二进制类型将二进制数据直接存储在MongoDB中.您的二进制数据并将其存储为字符串.将二进制数据存储为字符串时,Base64是常见的编码选择.

You don't need GridFS for files smaller than 16 MB. You can either store binary data directly in MongoDB using BSON's binary type or encode your binary data and store it as a string. Base64 is a common encoding choice when storing binary data as strings.

从数据库中检索数据后,可以使用

Once you retrieve the data from the database, you can write it to a file using fs.writeFile.

如果您将数据保存为二进制类型,则可以传递缓冲区 fs.writeFile .如果您将数据另存为编码字符串,则可以传递字符串和 encoding 选项.

If you saved your data as a binary type, you can pass a buffer to fs.writeFile. If you saved your data as an encoded string, you can pass a string and an encoding option.

((如果您想通过Express提供文件,则可以设置内容类型并使用 res.send )

(If you wanted to serve the file with Express, you can set the content type and send the data using res.send)

这是NodeJS中的一个小例子.这将从磁盘读取音频文件,并将其作为二进制类型保存到MongoDB数据库中(使用MongoDB驱动程序的

Here's a small example in NodeJS. This reads an audio file from disk, saves it to a MongoDB database as a binary type (using the MongoDB driver's Binary class), retrieves it back from the database, and writes it to a new file on disk.

const mongodb = require('mongodb')
const util = require('util')
const fs = require('fs')
const readFile = util.promisify(fs.readFile)
const writeFile = util.promisify(fs.writeFile)

async function main() {
  const client = await mongodb.MongoClient.connect(process.env.MONGO_URI)
  console.log('connected')

  let db = await client.db('dbname')

  // Reading in binary data from a file. data is a buffer.
  let data = await readFile(__dirname + '/sample.mp3')

  // Insert binary data to the database
  let res = await db.collection('coll').insert({data: new mongodb.Binary(data)})
  console.log(res)
  let objectId = res.ops[0]._id

  // Retrieve binary data from the database
  let obj = await db.collection('coll').findOne({_id: objectId})
  console.log(obj)

  // *** This is the key part ***
  // use obj.data.read to get a buffer from the binary data and write that buffer to a file
  await writeFile(__dirname + '/out.mp3', obj.data.read(0, obj.data.length()))

  console.log('done')
}

main()

尽管人们确实将二进制数据存储在数据库中,但对于人们来说,将文件存储在文件系统或对象存储(例如Amazon S3)中可能是最常见的.然后,他们只需将指向该文件的链接存储在数据库中,然后使用该链接检索该文件.您可以选择任何自己喜欢的方式.

Although people do store binary data in the database, it's probably most common for people to store files in a file system or in object storage (such as Amazon S3). Then they would just store a link to that file in the database and retrieve that file using the link. You can do whichever you feel more comfortable with.

这篇关于检索音频-二进制文件-存储在我的Mlab中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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