【问题标题】:Sending and handeling wav file to NodeJS向 NodeJS 发送和处理 wav 文件
【发布时间】:2019-04-22 01:56:25
【问题描述】:

有几篇关于这个主题的帖子,但我仍然无法调试我的代码,例如Sending HTML5 audio element content to ajax POST

目前我有一个允许用户使用 recorder.js 录制音频的客户端。然后我想获取存储在 blob 中的结果 wav 文件并将其发送到我的服务器端代码并​​将其存储在 mongo 中。

   rec.exportWAV(sendToBackEnd);
}


function sendToBackEnd(blob){
    var blob = blob;
    console.log(blob)
    var fd = new FormData();
    fd.append('fname', 'test.wav');
    fd.append('data', blob);
    // fd.append('data', blob);
    $.ajax({
        url: '/recordings',
        type: 'POST',
        data: fd,
        processData: false,
        // contentType: "audio/wav"
    }).done(function(data) {
       console.log(data);
    });
}

我已经阅读了几篇文章,我应该将 contentType 设置为 false 但是 req.body 是空的。

在服务器端我有以下代码。

var mongoose=       require('mongoose');
var express =       require('express');
var app     =       express();
var http    =       require('http');
var port    =       3000;
var bodyParser=     require('body-parser');


app.use(express.static('public'))
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.urlencoded({extended:true,limit:'50mb'}));
// app.use(bodyParser.json({ limit: '50mb' }));
// app.use(bodyParser.raw({ type: 'audio/wav',extended:true, limit: '50mb' }));

app.get('/',function(req,res){
    res.render('home.html')
});
app.get('/recordings/new',function(req,res){
    res.render('new.html')
});


app.post('/recordings',function(req,res){
    var recording = req.body
    console.log(req.body)
});

// app.get('/recordings',function(req,res){
//  res.render('index.html')
// });






mongoose.connect("mongodb://localhost:27017/sensor",{ useNewUrlParser: true },function(){
    console.log('database is connected');
});

app.listen(port, function(){
    console.log('sensor server is running')
    });

对此的任何帮助都将受到高度重视。谢谢!

【问题讨论】:

  • 服务器端的req.body 中有什么?根据客户端代码,音频应位于req.body.data
  • 我高度,高度不推荐urlencoded WAV二进制。这将使用大约 3 倍的必要带宽来传输文件。
  • @PatrickRoberts 那么如何将这个文件传输到 Mongo 中呢?我现在通过使用多方解析 formData 让它工作。现在我正在努力将 URL 编码字符串发送到谷歌语音到文本 API
  • 多部分表单数据格式适用于二进制文件,因为它不会像 url 编码那样使用多字节编码非 ASCII 和不可打印的代码点。但根据您问题中的代码,您似乎正在尝试使用app.use(bodyParser.urlencoded({extended:true,limit:'50mb'})); 而不是app.use(bodyParser.raw({ type: 'audio/wav',extended:true, limit: '50mb' }));
  • @PatrickRoberts 。 URLencoded 而不是 raw 更多的是什么有效的问题。我正处于成功发送、解析 URL 并将其存储在 mongo 中的位置。然后在 python 中,我将 base64 字符串解码为二进制并将其发送到谷歌文本到语音。虽然这很有效,但根据您的第一个评论,“我非常非常不推荐使用 urlencoded WAV 二进制文件。这将使用大约 3 倍的必要带宽来传输文件。”会是什么样子?

标签: node.js mongodb blob audio-streaming recorder.js


【解决方案1】:

这是我使用recorder.jsexpress 服务器进行音频录制、上传和存储工作的方法。

客户

下面我没有使用jQuery,只使用原生XMLHttpRequest

this.recorder.stop().then(({ blob }) => {
  if (blob.size === 44) { // 44 bytes means something is wrong. reload to fix
    if (window.confirm('Sorry, no audio was captured. Click OK to reload the page.'))
      window.location.reload();
    else return;
  }

  const fd = new FormData(),
    xhr = new XMLHttpRequest();

  fd.append("audioData", blob);

  xhr.onreadystatechange = r => {
    if (xhr.status !== 200) { window.alert("Sorry, something went wrong"); return }

    const response = JSON.parse(r.target.response);
    console.info(`Ok, audio uploaded!`, response);
  };
  xhr.open("POST", "https://my.audio.server/upload", true);
  xhr.send(fd);
});

服务器

在我的服务器上,我使用的是 express 而不是 MongoDB,但看起来您的服务器端问题更多地在于获取已上传的音频数据。这是我的做法:

const fs = require("fs"),
  fileUploader = require("multer")({ dest: "/tmp/" });

app.post("/upload", fileUploader.single("audioData"), (req, res) => {
  const temporaryFilename = req.file.path,
    wavData = fs.readFileSync(temporaryFilename),
    bytes = fs.statSync(temporaryFilename)["size"];

  // do something with wavData

  return res.json({ ok: true, bytes, at: new Date() });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 2013-09-22
    相关资源
    最近更新 更多