如何使用谷歌语音API有2个频道的音频

时间:2017-02-02 07:45:37

标签: google-cloud-speech

我们有两个人在不同频道讲话的录音。我正在尝试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建议将音频拆分为各个频道并单独使用。以编程方式执行此操作的推荐方法是什么?

1 个答案:

答案 0 :(得分:3)

我最终采用了这种方法

  • 使用sox
  • 将文件拆分为频道
  • 将两个频道音频上传到谷歌云存储(对于本地文件,如果录制长度超过1分钟,语音API将不会处理。因此,如果文件很大,则必须将其上传到谷歌云存储)
  • 通过语音识别API传递每个文件
  • 将成绩单分开。我们无法合并这两者,因为谷歌语音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格式。