The gTTS library, which stands for Google Text-to-Speech, is a versatile Python tool that interfaces with Google Translate's text-to-speech API. It allows users to transform textual content into audible speech, making it especially useful for applications requiring auditory outputs, like reading assistants, reminders, or voice-enabled interfaces. gTTS supports a multitude of languages, which is one of its prime advantages, providing a global reach for developers. The process of converting text to speech using this library is simple and efficient. Users input the desired text, choose a language, and the library produces an audio file. This audio can then be played back or stored for later use. One notable feature of gTTS is its ability to control the speed of speech, making it adaptable for different use cases. Given its straightforward integration and ease of use, it has become a popular choice among developers looking to incorporate text-to-speech functionality without the need for intricate configurations or hefty resources.
Introduction
This program utilizes the `gTTS` (Google Text-to-Speech) library for Python, which converts text into natural-sounding speech. The speech can then be saved as an audio file. The `gTTS` library interfaces with Google Translate's text-to-speech API, enabling it to support multiple languages.
The additional options provided include: 1. Language selection 2. Slow playback option
Configure
To use this program, you'll first need to install the necessary package:
pip install gtts
Code Explanation
1. Importing necessary libraries
from gtts import gTTS
import os
- `from gtts import gTTS`: This imports the `gTTS` class from the `gtts` library, which is used to convert text to speech.
- `import os`: This imports the `os` module, allowing the program to interact with the operating system. In this code, it's mainly used for running system commands (commented out), but it can be utilized for various OS-level operations.
2. Defining the `text_to_speech` function
def text_to_speech(text, lang='en', slow=False, output_file='output.mp3'):
- `text`: The actual text you want to convert to speech.
- `lang`: The language code for the text (defaults to English
- 'en').
- `slow`: A boolean to determine if the playback should be slower than normal.
- `output_file`: The name of the output audio file where the speech will be saved.
3. Docstring
"""
Convert text to speech and save it to an output file.
...
"""
4. The main logic inside the `text_to_speech` function
try:
# Create the TTS object
tts = gTTS(text=text, lang=lang, slow=slow)
# Save the converted audio to a file
tts.save(output_file)
# Optionally: Play the audio file (requires an external tool like mpg321)
# os.system(f"mpg321 {output_file}")
print(f"Audio saved as {output_file}")
except Exception as e:
print(f"Error: {e}")
- The `try` block is used to catch any potential errors that might occur during the text-to-speech conversion process.
- `tts = gTTS(text=text, lang=lang, slow=slow)`: Creates a TTS object using the provided parameters.
- `tts.save(output_file)`: Saves the converted speech as an audio file.
- The commented-out line `# os.system(f"mpg321 {output_file}")` can be used to play the audio file immediately after creation, but this requires an external tool called `mpg321`.
- Any errors caught during the process will be printed out for debugging purposes.
5. Execution when script is run as the main module
if __name__ == "__main__":
6. Gathering user input and calling the function
text = input("Enter the text you want to convert to speech: ")
lang = input("Enter the language code (default 'en' for English): ")
slow_option = input("Do you want slow playback? (yes/no): ").strip().lower()
slow = True if slow_option == 'yes' else False
text_to_speech(text, lang, slow)
- The program prompts the user to enter text, choose a language, and decide if they want slower playback.
- The input for slow playback is then converted into a boolean.
- Finally, the `text_to_speech` function is called with the user's inputs.
Full Code:
from gtts import gTTS
import os
def text_to_speech(text, lang='en', slow=False, output_file='output.mp3'):
"""
Convert text to speech and save it to an output file.
:param text: Text to be converted to speech
:param lang: Language of the text ('en' for English by default)
:param slow: Boolean to playback speech more slowly
:param output_file: Name of the output audio file
"""
try:
# Create the TTS object
tts = gTTS(text=text, lang=lang, slow=slow)
# Save the converted audio to a file
tts.save(output_file)
# Optionally: Play the audio file (requires an external tool like mpg321)
# os.system(f"mpg321 {output_file}")
print(f"Audio saved as {output_file}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
text = input("Enter the text you want to convert to speech: ")
lang = input("Enter the language code (default 'en' for English): ")
slow_option = input("Do you want slow playback? (yes/no): ").strip().lower()
slow = True if slow_option == 'yes' else False
text_to_speech(text, lang, slow)
To use the program, run the code and follow the on-screen prompts. After you provide the necessary input, the program will generate an audio file named `output.mp3` (by default) containing the spoken version of your text.
For different languages, you can change the `lang` parameter using [ISO language codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) (like 'en' for English, 'es' for Spanish, etc.). Note that not all languages are supported, and the availability can vary based on Google's API.
The "slow" option, when enabled, will make the speech play more slowly than usual, which can be helpful for clearer understanding or specific use cases.