Search code examples
pythongoogle-apispeech-recognitionspeech-to-text

Transcribing Long Audio File doesn't work


I'm trying to use the example code from the google page to transcribe a 30min .wav file. I changed the original code a bit and it's below:

from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'C:\\Users\\louie\\Desktop\\PSC.json'
gcs_uri = os.path.join('C:\\Users\\louie\\Desktop','Untitled1.wav')

client = speech.SpeechClient()

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

operation = client.long_running_recognize(config, audio)

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

# 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))

When I run it, I got the error 400 Request contains an invalid argument I'm pretty sure my pre-setings are correct since the code for short transcription works for me. Could someone help me out on this issue? Thanks!

Edit: I think this issue is related to the wrong format of gcs_uri. Is there a way of transcribing large audio files without upload it to the Google cloud storage?


Solution

  • I noticed that gcs_uri should actually refer to the directory in the Google cloud. The format should be like gs://<bucket_name>/<file_path_inside_bucket>