📜  gTTs Basic - Python (1)

📅  最后修改于: 2023-12-03 15:01:05.295000             🧑  作者: Mango

gTTs Basic - Python

Introduction

gTTS (Google Text-to-Speech) is a Python library and CLI tool to interface with Google Translate's text-to-speech API. This API converts written text into spoken words and returns an audio file as a result.

Features
  • Supports multiple languages
  • Easy to install and use
  • Can save speech as an mp3 file
  • Can play speech directly from the command line
Installation

gTTS can be installed using pip. Open a command prompt or terminal and run the following command:

pip install gTTS
Usage
  1. Import the gTTS module:
from gtts import gTTS
  1. Create a gTTS object with the text to be spoken:
tts = gTTS('Hello, World!')
  1. Save the speech as an mp3 file:
tts.save('hello.mp3')
  1. Play the speech directly from the command line:
gtts-cli 'Hello, World!' | mpg123 -
Code Example
from gtts import gTTS

tts = gTTS('Hello, World!')

# Save the speech as an mp3 file
tts.save('hello.mp3')

# Play the speech directly from the command line
!gtts-cli 'Hello, World!' | mpg123 -
Conclusion

gTTS is a simple and effective tool to convert text to speech using Python. It offers great functionality in an easy-to-use package. With gTTS, you can save speech as an mp3 file or play it directly from the command line.