严格使用作为示例提供的代码来运行谷歌云文本到语音 API:
def synthesize_text(text):
"""Synthesizes speech from the input string of text."""
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.types.SynthesisInput(text=text)
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
response = client.synthesize_speech(input_text, voice, audio_config)
# The response's audio_content is binary.
with open('output.mp3', 'wb') as out:
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
我收到上述错误消息 google.api_core.exceptions.MethodNotImplemented 501 未找到方法
。
这似乎是 Google 内部错误。我已经仔细检查过我的凭据。
错误来自此特定行:response = client.synthesize_speech(input_text, voice, audio_config)
请帮忙。
最佳答案
此时,由于某种原因,python 客户端库有 code for v1 and v1beta1 API versions 。但是,检查 the dicovery service for Google APIs ,我们可以看到唯一可用的端点适用于 v1beta1。
如果您关注this library docs并使用 from google.cloud import texttospeech_v1
,您的客户端将尝试使用 https://texttospeech.googleapis.com/v1/
而不是 https://texttospeech.googleapis.com/v1beta1/
。事实上,错误不是 4XX 而是 5XX,这让我认为端点本身可能存在,但它们尚未实现,或者不允许您发现它们(因此它们看起来没有实现)。
尝试将您的导入更改为 v1beta1 doc 中的导入(from google.cloud import texttospeech_v1beta1
),看看这是否有任何区别。
关于python - 谷歌云合成语音501方法未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50932363/