我正在尝试使用Google云API转录持续时间少于一分钟的音频文件。我正在从官方网站复制代码 https://cloud.google.com/speech-to-text/docs/sync-recognize 但它没有输出。该脚本会在5 -7秒内结束,并且屏幕上不会显示任何输出。我希望在输出时转录音频。
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")
一个明显的警告可能是,可视代码显示为[pylint] Module 'google.cloud.speech_v1.types' has no 'RecognitionAudio' member [no-member]
。它仅在types.RecognitionAudio
和types.RecognitionConfig
下显示一条红线。尽管它不会产生任何错误。