首页 文章

Google Speech API Python异常:指定FLAC编码以匹配文件头?

提问于
浏览
2

我正在运行此处发布的Google Speech API Python的示例代码:https://googlecloudplatform.github.io/google-cloud-python/stable/speech-usage.html

我要去异步识别方法(只允许使用LINEAR16编码):

导入Google Cloud客户端库

from google.cloud import speech

  client = speech.Client() 
  sample = client.sample(source_uri='gs://my-bucket/example.flac',
                    encoding=speech.Encoding.LINEAR16,
                    sample_rate=44100)
  operation = sample.async_recognize(language_code='es-CL',max_alternatives=2)

   retry_count = 100
   while retry_count > 0 and not operation.complete:
   retry_count -= 1
   time.sleep(10)
   operation.poll()  # API call

   operation.complete

   for result in operation.results:
        for alternative in result.alternatives:
        print('=' * 20)
        print(alternative.transcript)
        print(alternative.confidence)

这是我得到的错误:google.gax.errors.RetryError:GaxError(重试方法中发生异常,未被归类为瞬态,由终止于的<_Rendezvous的RPC引起)(StatusCode.INVALID_ARGUMENT,指定FLAC编码以匹配文件头 . )>)

我怎么解决这个问题?使用同步方法时,我没有遇到这个问题 .

1 回答

  • 2

    从您的代码(以及您链接到的Google代码)看起来您必须将 .flac 文件转换为原始的LINEAR PCM文件 . 所以第二行看起来应该更像这样:

    sample = client.sample(source_uri='gs://my-bucket/example.raw',
                    encoding=speech.Encoding.LINEAR16,
                    sample_rate=44100)
    

    要从FLAC转换为LINEAR16,您需要使用其他工具,例如 sox . 有关转换文件格式的更多信息,请参见this page . 该命令可能类似于:

    sox example.flac example.raw
    

相关问题