有什么方法可以将来自 twilio 的 mulaw 音频流保存在文件中 [英] Is there any way to save mulaw audio stream from twilio in a file

查看:51
本文介绍了有什么方法可以将来自 twilio 的 mulaw 音频流保存在文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Twilio 语音流功能,但我不想使用 Twilio 录音功能.当 Twilio 开始向我的服务器发送语音流时,我想将其作为音频文件实时存储到磁盘中.

I am using Twilio voice stream feature and i don't want to use Twilio record functionality. When Twilio starts sending voice stream to my server i want to store it into disk as an audio file in realtime.

推荐答案

我今天遇到了同样的问题,并想出了一种为 mu-law 标头生成 WAVE 标头的方法:

I was running into the same issue today and figured a way to generate a WAVE Header for the mu-law header:

如果您关注 Twilio 的博客文章,这是我结束实施的代码:

If you're following Twilio's blog post, that's the code I ended implementing:

wss.on('connection', (socket) => {
  socket.on('message', (msg) => {
    const { event, ...message } = JSON.parse(msg);
    switch (event) {
      case 'start':
        let streamSid = message.start.streamSid;
        socket.wstream = fs.createWriteStream(__dirname + `/${Date.now()}.wav`, { encoding: 'binary' });
        // This is a mu-law header for a WAV-file compatible with twilio format
        socket.wstream.write(Buffer.from([
          0x52,0x49,0x46,0x46,0x62,0xb8,0x00,0x00,0x57,0x41,0x56,0x45,0x66,0x6d,0x74,0x20,
          0x12,0x00,0x00,0x00,0x07,0x00,0x01,0x00,0x40,0x1f,0x00,0x00,0x80,0x3e,0x00,0x00,
          0x02,0x00,0x04,0x00,0x00,0x00,0x66,0x61,0x63,0x74,0x04,0x00,0x00,0x00,0xc5,0x5b,
          0x00,0x00,0x64,0x61,0x74,0x61,0x00,0x00,0x00,0x00, // Those last 4 bytes are the data length
        ]));
        break;
      case 'media':
        // decode the base64-encoded data and write to stream
        socket.wstream.write(Buffer.from(message.media.payload, 'base64'));
        break;
      case 'stop':
        // Now the only thing missing is to write the number of data bytes in the header
        socket.wstream.write("", () => {
          let fd = fs.openSync(socket.wstream.path, 'r+'); // `r+` mode is needed in order to write to arbitrary position
          let count = socket.wstream.bytesWritten;
          count -= 58; // The header itself is 58 bytes long and we only want the data byte length
          console.log(count)
          fs.writeSync(
            fd,
            Buffer.from([
              count % 256,
              (count >> 8) % 256,
              (count >> 16) % 256,
              (count >> 24) % 256,
            ]),
            0,
            4, // Write 4 bytes
            54, // starts writing at byte 54 in the file
          );
        });
        break;
    }
  });
});

这篇关于有什么方法可以将来自 twilio 的 mulaw 音频流保存在文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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