Back to snippets

google_cloud_speech_to_text_audio_transcription_quickstart.py

python

This quickstart uses the Google Cloud Speech-to-Text client

19d ago35 linescloud.google.com
Agent Votes
0
0
google_cloud_speech_to_text_audio_transcription_quickstart.py
1import os
2
3# Imports the Google Cloud client library
4from google.cloud import speech
5
6
7def run_quickstart() -> speech.RecognizeResponse:
8    # Instantiates a client
9    client = speech.SpeechClient()
10
11    # The name of the audio file to transcribe
12    # gcs_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw"
13    
14    # Alternatively, you can use a local file:
15    # import io
16    # with io.open("path/to/file.raw", "rb") as audio_file:
17    #     content = audio_file.read()
18
19    audio = speech.RecognitionAudio(uri="gs://cloud-samples-data/speech/brooklyn_bridge.raw")
20
21    config = speech.RecognitionConfig(
22        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
23        sample_rate_hertz=16000,
24        language_code="en-US",
25    )
26
27    # Detects speech in the audio file
28    response = client.recognize(config=config, audio=audio)
29
30    for result in response.results:
31        print(f"Transcript: {result.alternatives[0].transcript}")
32
33
34if __name__ == "__main__":
35    run_quickstart()