如何对具有 2 个声道的音频使用 google 语音 API

How to use google speech API for audio having 2 channels

我们有 2 个人在不同频道上讲话的录音。我正在尝试 node.js here 的官方文档。首先,我得到一个错误,说有效负载大小超过了最大限制。

ubuntu@ip-xxxx:~/nodejs-docs-samples/speech$ node recognize.js async /home/ubuntu/output.wav
(node:18306) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Request payload size exceeds the limit: 10485760 bytes.

然而,文档只是提到了记录长度方面的限制,而不是文件大小方面的限制。这是 link

有什么解决方法吗?

此外,我尝试使用较小的文件大小,但出现配置错误:

ubuntu@ip-xxx:~/nodejs-docs-samples/speech$ node recognize.js async /home/ubuntu/output2.wav
(node:18291) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Invalid Configuration, Does not match Wav File Header.
Wav Header Contents:
Encoding: LINEAR16
Channels: 2
Sample Rate: 16000.
Request Contents:
Encoding: linear16
Channels: 1
Sample Rate: 16000.

我不确定 API 是否允许使用 2 声道音频输入,因为我在文档中找不到任何此类配置。但是,我发现 this link 建议将音频拆分为各个通道并单独使用。以编程方式执行此操作的推荐方法是什么?

我最终采用了这种方法

  • 使用 sox
  • 将文件拆分到频道
  • 将两个频道的音频上传到google云存储(本地文件,语音API如果录音长度超过1分钟将不会处理。所以如果文件很大,他们必须上传到 google 云存储)
  • 通过语音识别传递每个文件API
  • 将成绩单分开保存。我们无法合并两者,因为 google 语音 API 不提供单词
  • 的时间戳

这是一个将文件拆分到频道的辅助函数

function splitFileToChannels (fileName) {
  let output = {
    channel1: `${fileName}_channel1.wav`,
    channel2: `${fileName}_channel2.wav`
  };
  let channel1Command = `sox ${fileName} ${fileName}_channel1.wav remix 1`;
  let channel2Command = `sox ${fileName} ${fileName}_channel2.wav remix 2`;
  return Promise.all([
    childProcess.execAsync(channel1Command),
    childProcess.execAsync(channel2Command)
  ])
  .then(() => {
    return output;
  });
}

此外,在拆分为频道之前,我必须先将 mp3 文件转换为 wav 格式。