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

2024-05-15 01:18:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行发布在这里的googlespeechapi Python的示例代码:https://googlecloudplatform.github.io/google-cloud-python/stable/speech-usage.html

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

导入Google云客户端库

  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)

这是我得到的错误: 谷歌.gax.errors.RetryError:GaxError(在未分类为瞬态的重试方法中发生异常,这是由终止于的RPC的<;\u会合引起的(StatusCode.INVALID_参数,指定FLAC编码以匹配文件头。)>;)

我怎么解决这个问题?我在使用同步方法时不会遇到这个问题。在


Tags: sampleclientcloud编码forcountgoogleoperation
1条回答
网友
1楼 · 发布于 2024-05-15 01:18:55

从您的代码(以及链接到的Google代码)来看,您将编码指定为LINEAR16,但使用的是FLAC文件。如果您需要使用async API,那么您必须将.flac文件转换为原始线性PCM文件。所以第二行应该更像这样:

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

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

^{pr2}$

相关问题 更多 >

    热门问题