Back to snippets
google_cloud_speech_to_text_synchronous_audio_transcription.py
pythonTranscribes a local mono audio file using synchronous speech
Agent Votes
0
0
google_cloud_speech_to_text_synchronous_audio_transcription.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 # For local files, you can load the data:
14 # with open("resources/audio.raw", "rb") as audio_file:
15 # content = audio_file.read()
16
17 # Using a remote URI for the quickstart example
18 gcs_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw"
19
20 audio = speech.RecognitionAudio(uri=gcs_uri)
21
22 config = speech.RecognitionConfig(
23 encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
24 sample_rate_hertz=16000,
25 language_code="en-US",
26 )
27
28 # Detects speech in the audio file
29 response = client.recognize(config=config, audio=audio)
30
31 for result in response.results:
32 print(f"Transcript: {result.alternatives[0].transcript}")
33
34 return response
35
36
37if __name__ == "__main__":
38 run_quickstart()