我正在使用mediaDevices.getUserMedia()
在Chrome中录制简短的音频文件(几秒钟),将文件保存到Firebase存储,然后尝试将文件从Firebase云发送到Google Cloud语音转文本功能。我回来了此错误消息:
INVALID_ARGUMENT: Invalid recognition 'config': bad encoding.
Google的documentation说此错误消息的意思是
您的音频数据可能未正确编码或使用 编解码器与您在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
实际上,这是一种媒体格式和一种编解码器。 This blog post还说Chrome仅支持Opus编解码器。
我设置了Firebase Cloud功能:
// 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语音转文本请求之间的音频编码匹配。为什么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得到了similar error message。该文件可以正常播放。