首页 > 解决方案 > google cloud speech to text rest api return empty response

问题描述

I tried to use google cloud speech to text API, by using the sample page (https://cloud.google.com/speech-to-text/). It works fine after I upload my audio file (.m4a)

Then I try to call these services using rest API and the same audio file, it does not work.

I get 200 with an empty response.

I call rest API with this info

POST https://speech.googleapis.com/v1p1beta1/speech:recognize?key=SOME_KEY

body:

{
  "audio": {
    "content": "base64 audio content"
  },
  "config": {
    "sampleRateHertz": 16000,
    "encoding": "LINEAR16",
    "languageCode": "en-US"
  }
}

but on https://cloud.google.com/speech-to-text/, the body is different a little bit like this

{
  "audio": {
    "content": "/* Your audio */"
  },
  "config": {
    "enableAutomaticPunctuation": true,
    "encoding": "LINEAR16",
    "languageCode": "en-US",
    "model": "default"
  }
}

I don't know why it does not work when calling rest API.

Please help

Updated =>

I read audio file by this code (example from google doc)

const file = fs.readFileSync('FILE_LOCATION')
const audioBytes = file.toString('base64')

标签: google-speech-apigoogle-cloud-speech

解决方案


阅读文档,我找不到 Google Text To Speech 支持的“.m4a”(Apple Lossless)文件的引用。事实上,恰恰相反。

我的建议是,至少尝试将您的文件转换为 WAV 格式文件并提供该文件。如果可行,请查看是否无法以这种格式获取原始音频。

看:


推荐阅读