I am trying to transcribe an audio file, whose duration is less than a minute, using the Google cloud API. I am copying the code from official website https://cloud.google.com/speech-to-text/docs/sync-recognize but it gives no output. The script ends within 5 -7 seconds and no output displays on screen. I expect the transcribed audio at output.
import os
import io
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="D:\\Rehan\\naufil-0e289431cf2d.json"
def transcribe_file(speech_file):
"""Transcribe the given audio file."""
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
client = speech.SpeechClient()
with io.open(speech_file, 'rb') as audio_file:
content = audio_file.read()
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US')
response = client.recognize(config, audio)
# 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))
transcribe_file(r"D:\\Rehan\\hey.mp3")
One notable warning can be, visual code says [pylint] Module 'google.cloud.speech_v1.types' has no 'RecognitionAudio' member [no-member]
. It just shows a red line under types.RecognitionAudio
and types.RecognitionConfig
. Although it doesnt generate any error.