python-3.xgoogle-cloud-speechgoogle-hangouts

Speech to Text: Google API Error 400 Sync input too long. For audio longer than 1 min use LongRunningRecognize with a 'uri' parameter


I am still having trouble with transcribing a long audio file despite using gcs_uri link as described in the documentation. This is my code:

def googleAPI(self, gcs_uri): 
        client=speech.SpeechClient(credentials=CREDENTIALS)
        audio = speech.RecognitionAudio(uri=gcs_uri) 
        config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, 
        sample_rate_hertz=int(self.audio_frame), 
        language_code="fr-FR", 
        audio_channel_count=int(self.audio_channels), 
        enable_separate_recognition_per_channel=True,
        use_enhanced=True, 
        model="phone_call", 
        )
        #ADDED HERE FOR LONG AUDIO FILES 
        operation = client.long_running_recognize(config=config, audio=audio)
        print("Waiting for operation to complete...")
        response = operation.result(timeout=10000) #set timeout to a large number 
        response= client.recognize(config=config, audio=audio)
        return response

My audio files are about 30-40 minutes long. Please advise.


Solution

  • The problem in your code is that you are trying to run the Speech Recognition twice. First with the right function. Then with the function that only supports audios up to 1 min. Hence the error. Just delete this line:

    response= client.recognize(config=config, audio=audio)
    

    Response before that is the result that you need. This line will just try to do the same as $ operation = client.long_running_recognize(config=config, audio=audio) but only supporting up to 1 min.