一天内通过 gTTS 使用 Google 翻译的次数似乎有限制,因为 Google 不希望您过多地攻击他们的服务器。 关于如何使用谷歌翻译语音将大量文本(几百兆字节)转换为语音而无需等待数年(字面意思),有哪些潜在的想法? 只是寻找有人为我指明正确的方向。 谢谢!
请改用 Google Cloud TTS 服务 API。
有一个Python接口,并且有免费层可用。
https://cloud.google.com/text-to-speech/docs/create-audio#text-to-speech-text-python
https://cloud.google.com/text-to-speech/pricing
或者,您可以尝试使用另一个库(如 pyttsx3)。但质量没那么好。
Amazon Polly 也通过 AWS 提供类似的服务,具有类似的免费套餐和 Python 接口。
https://docs.aws.amazon.com/polly/latest/dg/get-started-what-next.html
https://aws.amazon.com/polly/pricing/
我过去曾使用过 Polly,但我的免费套餐已过期(仅限 1 年)。设置需要一些工作,但设置后,它很快且易于使用。
祝你好运。 (不过,到目前为止,您可能已经找到了解决方案......)
这里有一个解决方法:
from gtts import gTTS, gTTSError
for attempt in range(max_retries):
try:
tts = gTTS(text, lang=language)
tts.save(audio_file_path)
return audio_file_path
except gTTSError as e:
if "429 (Too Many Requests)" in str(e):
print(f"Rate limit hit, retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
retry_delay *= 2 # Exponential backoff
else:
raise
except Exception as e:
raise # Re-raise any other exceptions
raise RuntimeError("Failed to convert text to speech after retries")
参考:https://github.com/frknltrk/gTTS/blob/19e3ad555f0fc0da6ae8ff70ff72f9e801ba0da9/main.py#L33