首页 > 解决方案 > Google Cloud Speech-to-Text API - 无限等待

问题描述

我正在尝试使用 Google Cloud Speech-to-Text API。

根据 API 文档的理解,我将 mp3 音频文件格式转换为 .raw,然后上传到存储桶。

这是我的代码:

def transcribe_gcs(gcs_uri):
    """Asynchronously transcribes the audio file specified by the gcs_uri."""
    from google.cloud import speech
    from google.cloud.speech import enums
    from google.cloud.speech import types
    client = speech.SpeechClient()

    audio = types.RecognitionAudio(uri=gcs_uri)
    config = types.RecognitionConfig(
        encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
        sample_rate_hertz=16000,
        language_code='en-US')

    operation = client.long_running_recognize(config, audio)

    print('Waiting for operation to complete...')
    response = operation.result()

    # Each result is for a consecutive portion of the audio. Iterate through
    # them to get the transcripts for the entire audio file.
    for result in response.results:
        # The first alternative is the most likely one for this portion.
        print(u'Transcript: {}'.format(result.alternatives[0].transcript))
        print('Confidence: {}'.format(result.alternatives[0].confidence))

transcribe_gcs("gs://cloudh3-200314.appspot.com/cs.raw")

我做错了什么?

标签: pythongoogle-app-enginegoogle-cloud-platformgoogle-speech-api

解决方案


我遇到了类似的问题,这与可接受的格式有关。即使您可能已经转换为 RAW,但格式仍然可能有问题,如果无法读取文件,它不会给您输出。

我最近处理了一个 56 分钟的音频,花了 17 分钟,所以这应该让你知道它应该多长时间。

使用 sox 处理您的文件,我找到了使用命令工作的转换参数 -

sox basefile.mp3 -r 16000 -c 1 newfile.flac

推荐阅读