📅  最后修改于: 2023-12-03 15:31:05.004000             🧑  作者: Mango
gtts
is a Python library and CLI tool to interface with Google Translate's text-to-speech API. This library allows you to generate speech from a given text in multiple languages and voices. It's easy to use and it's open source.
You can install gtts
via pip:
pip install gtts
from gtts import gTTS
# Create an instance of gTTS and save it as an mp3 file
tts = gTTS('Hello, World!')
tts.save('hello.mp3')
from gtts import gTTS
from gtts.lang import tts_langs
# Get a list of available languages
langs = tts_langs()
# Create an instance of gTTS with multiple options
tts = gTTS(text='Hola, Mundo!', lang='es', slow=True)
# Save the speech as a file
tts.save('hola.mp3')
# Get the speech as binary content
speech = tts.get_string()
You can also use gtts-cli
directly from the command line to generate speech files:
gtts-cli "Hello, World!" -l 'en' -o hello.mp3
In conclusion, gtts
is a powerful and easy-to-use Python library for generating speech from text. Its flexibility and options make it a great tool for a wide range of applications, from generating personalized voice messages to creating accessibility features for your applications.