首页 > 解决方案 > Google Speech to Text(语音识别)仅识别音频的前几秒

问题描述

我在节点 js 中使用 Google 的 Speech-to-Text API。它返回对前几个单词的识别,但随后忽略音频文件的其余部分。任何上传文件的截止点约为 5-7 秒。

我尝试过对较短的音频文件进行同步语音识别。(使用 MP3 文件的示例如下所示)

    filename = './TEST/test.mp3';

    const client = new speech.SpeechClient();

    //configure the request:
    const config = {
        enableWordTimeOffsets: true,
        sampleRateHertz: 44100,
        encoding: 'MP3',
        languageCode: 'en-US',
    };
    const audio = {
        content: fs.readFileSync(filename).toString('base64'),
    };
    const request = {
        config: config,
        audio: audio,
    };
    
    // Detects speech in the audio file
    const [response] = await client.recognize(request);

而且我还尝试过对较长的音频文件进行异步识别 (使用如下所示的 WAV 文件的示例)

filename = './TEST/test.wav';

const client = new speech.SpeechClient();

//configure the request:
const config = {
     enableWordTimeOffsets: true,
     languageCode: 'en-US',
};
const audio = {
     content: fs.readFileSync(filename).toString('base64'),
};
const request = {
     config: config,
     audio: audio,
};

//Do a longRunningRecognize request
const [operation] = await client.longRunningRecognize(request);
const [response] = await operation.promise();

我已经使用 WAV 文件和 MP3 尝试了这些实现中的每一个。结果总是完全相同:前 5 秒识别良好,然后什么都没有。

任何帮助将不胜感激!

标签: google-cloud-platformspeech-recognitionspeech-to-textgoogle-speech-api

解决方案


@Ricco D 绝对正确,我打印的结果不正确......

当您尝试转录较长的文件时,Google Speech to Text 将根据检测到语音暂停的时间来分解您的转录。

您的 response.results[] 数组将包含多个条目,您需要循环这些条目以打印完整的成绩单。

有关更多详细信息,请参阅文档: https ://cloud.google.com/speech-to-text/docs/basics#responses


推荐阅读