首页 > 解决方案 > Google Cloud Speech-to-Text:“INVALID_ARGUMENT:无效识别‘配置’:错误编码..”编解码器音频编码错误

问题描述

我正在使用 Chrome 在 Chrome 中录制短音频文件(几秒钟)mediaDevices.getUserMedia(),将文件保存到 Firebase 存储,然后尝试将文件从 Firebase 云功能发送到 Google Cloud Speech-to-Text。我收到此错误消息:

INVALID_ARGUMENT: Invalid recognition 'config': bad encoding.

谷歌的文档说这个错误信息意味着

您的音频数据可能未正确编码或使用不同于您在 RecognitionConfig 中声明的编解码器进行编码。检查音频输入并确保您已正确设置编码字段。

在浏览器中,我设置了麦克风:

navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(stream => {

var options = {
   audioBitsPerSecond : 128000,
   mimeType : 'audio/webm;codecs=opus'
};

const mediaRecorder = new MediaRecorder(stream, options);
mediaRecorder.start();
...

根据this answer Chrome仅支持两种编解码器:

audio/webm
audio/webm;codecs=opus

实际上,这是一种媒体格式和一种编解码器。这篇博文还说 Chrome 只支持 Opus 编解码器。

我设置了我的 Firebase 云功能:

// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();

const gcsUri = 'gs://my-app.appspot.com/my-file';
const encoding = 'Opus';
const sampleRateHertz = 128000;
const languageCode = 'en-US';

const config = {
   encoding: encoding,
   sampleRateHertz: sampleRateHertz,
   languageCode: languageCode,
};
const audio = {
   uri: gcsUri,
};

const request = {
   config: config,
   audio: audio,
};

// Detects speech in the audio file
return response = client.recognize(request) // square brackets in ES6 construct an array
.then(function(response) {
console.log(response);
...

浏览器和 Google Speech-to-Text 请求之间的音频编码匹配。为什么 Google Speech 告诉我音频编码不好?

我还尝试在浏览器中使用默认选项,但出现相同的错误消息:

navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(stream => {

const mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start();

在 Firebase Cloud Function 中,我尝试省略该行const encoding = 'Opus';,这导致了错误encoding is not defined。我尝试了const encoding = '';导致INVALID_ARGUMENT: Invalid recognition 'config': bad encoding..错误的这条线。

我从 IBM Watson Speech-to-Text收到类似的错误消息。文件播放没有问题。

标签: getusermediagoogle-cloud-speech

解决方案


推荐阅读