Rimo
Rimo

Reputation: 565

Google Speech API Python Exception: Specify FLAC encoding to match file header?

I'm running the example code for Google Speech API Python published here: https://googlecloudplatform.github.io/google-cloud-python/stable/speech-usage.html

I'm going for the Asynchronous Recognition method (which only allows the use LINEAR16 encoding):

Imports the Google Cloud client library

  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)

This is the error I'm getting: google.gax.errors.RetryError: GaxError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Specify FLAC encoding to match file header.)>)

How can I solve this? I'm not getting this issue when using the Synchronous Method.

Upvotes: 4

Views: 3276

Answers (1)

blambert
blambert

Reputation: 1350

From your code (and the Google code you linked to) it looks like you're specifying the encoding as LINEAR16 but using a FLAC file. If you need to use the async API then you'll have to convert the .flac file to a raw LINEAR PCM file. So the second line should look more like this:

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

To convert from FLAC to LINEAR16 you'll need to use another tool, like sox. See this page for more information on converting the file format. The command is probably something like:

sox example.flac example.raw

Upvotes: 4

Related Questions