问题:
我从浏览器中录制音频,录制完成后会给我一个BLOB:
let blob = new Blob(chunks, { 'type' : 'audio/webm;codecs=opus' });
在这里更改mime类型不会有帮助,因为这些块已经带有其MIME类型,几乎所有浏览器都为audio/webm;codecs=opus
。所以在这里什么也不能做。
通过XHR将此Blob发送到node.js服务器将导致从该Blob接收缓冲区:
客户:
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:3000/audio', true);
xhr.send(blob);
服务器:
app.post('/audio' , (req, res) =>{
req.on('readable',()=>{
let buffer = req.read();
// sending this buffer to the external API results in error
// since it expects the mime-type audio/wav
});
res.send({msg: 'success'});
});
大多数解决方案都要求您将文件写入磁盘并进行转换(ffmpeg)。 其他人则使用实验性或与旧版浏览器不兼容的浏览器功能... 我也尝试使用wavfile npm包,但是如果我尝试使用该webm格式的缓冲区中的UInt8Array写入文件(可播放文件,但它仅包含噪音,并且比实际录制的时间短得多),则会创建损坏的文件是)
必须有一个简单的解决方案来转换二进制数据服务器端,对吗?我最希望的是函数convertWebmBufferToWavBuffer
。