Google Speech API - 识别 Base64 编码的音频

lit*_*ird 5 audio-recording google-speech-api

我已经在 Google Speech API 上苦苦挣扎了一段时间,希望得到一些建议。

这就是我想做的:

  1. 在浏览器中录制音频
  2. 将录音转换为 Base64 并发送到我的服务器
  3. 在服务器上,调用Google Speech API的syncRecognize函数,传入我的base 64编码音频

我总是收到一个空的结果对象。

当我按照此入门教程进行操作时,一切正常。

我究竟做错了什么?

任何提示/想法将非常感激。

const Speech = require('@google-cloud/speech');
const SpeechV1beta1 = require('@google-cloud/speech/src/v1beta1')

module.exports = {

    syncRecognize: function(base64Encoding) {

       const speech = Speech();
       const speechV1beta1 = SpeechV1beta1();
       const client = speechV1beta1.speechClient();

       const body = {
           "config": {
               "encoding":"LINEAR16",
               "sampleRate":16000,
               "languageCode":"en-US"
           },
           "audio": {
              "content": base64Encoding
           }
       }

       return client.syncRecognize(body)
        .then((results) => {
            console.log('results', results) 
            return {transcription: results[0]};
        }).catch(function(error) {
            return {error: error};
        });;

    }
}
Run Code Online (Sandbox Code Playgroud)

Eff*_*ePi 3

您从浏览器记录的内容可能没有这些配置参数。

我遇到的另一个问题是,当音频持续时间超过 5 秒时,就会出现超时,到目前为止,我还无法有效地更改 API 截止时间。这也可能是你的情况。

为了验证网络应用程序中音频的元数据,我使用ffmpeg:我自动将文件保存到存储中并ffmpeg -i filename在脚本中运行以检索采样率、编码、通道数和持续时间。

请注意,同步语音识别请求存在音频长度限制。如果超出,我建议您使用异步方法(或将音频文件拆分为子文件)。在前一种情况下,您可能需要转换音频编码(幸运的是,FFmpeg 也可以为您做到这一点:))。在后一种情况下,您可以使用SoX在静音时分割音频并单独处理它们。