Python中JS的麦克风音频

2024-04-19 22:20:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在网站上录制麦克风输入并将此音频发送到后端:

navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
  const mediaRecorder = new MediaRecorder(stream);
  mediaRecorder.start();

  const audioChunks = [];
  mediaRecorder.addEventListener("dataavailable", event => {
    audioChunks.push(event.data);
  });

  mediaRecorder.addEventListener("stop", () => {
    const audioBlob = new Blob(audioChunks);

    const data = new FormData();
    data.append('speech', audioBlob, 'command');

    axios.post('/command', data)
    .then(function (response) {
      console.log(response);
    })
    .catch(function (error) {
      console.log(error);
    });
  });
});

在我的后端(在Flask上运行),我在打开(或访问)它时遇到问题。当我使用wave模块时:

^{pr2}$

我将以错误消息wave.Error: file does not start with RIFF id结束。如何解决这个问题?我应该把它转换成其他格式吗?正确的方法是什么,如何用Python处理来自网站的麦克风音频?在


Tags: eventnewdatastream网站function音频start