This script allows you to convert text into speech and save the audio file in the MP3 format. It supports English and Spanish languages with both male and female voices.
- Convert text to speech in English or Spanish
- Choose between male or female voices
- Generate a random ID for each conversion
- Save the input text, language, gender, and ID in a JSON log file
- Create an "Outputs" folder to store the generated audio files
- Print the path to the saved audio file
- Python 3.x
gTTS
(Google Text-to-Speech) library
You can install the gTTS
library using pip:
pip install gTTS
- Run the script.
- Select the language and gender for the voice by entering the corresponding number:
- English (Male)
- English (Female)
- Spanish (Male)
- Spanish (Female)
- Enter the text you want to convert to speech.
- The script will convert the text to speech and save the audio file in the "Outputs" folder.
- The path to the saved audio file will be printed in the console.
main.py
: The main script file containing the text-to-speech conversion logic.log.json
: A JSON file that stores the log of converted texts, their IDs, languages, and genders.Outputs/
: A directory where the generated audio files are saved in MP3 format with a random ID in the filename (e.g.,output_abcd12.mp3
).
The script uses the gTTS
(Google Text-to-Speech) library to convert text to speech. It generates a random ID for each conversion and saves the input text, ID, language, and gender in a JSON log file (log.json
).
The text_to_speech
function performs the following steps:
- Creates the "Outputs" directory if it doesn't exist.
- Generates a random ID for the conversion.
- Saves the input text, ID, language, and gender in the
log.json
file. - Creates a
gTTS
object with the specified language, gender, and voice settings. - Saves the generated speech as an MP3 file in the "Outputs" folder, using the random ID in the filename.
- Prints the path to the saved audio file.
The script prompts the user to select the language and gender for the voice, and then prompts for the input text to be converted to speech.
Contributions, issues, and feature requests are welcome! Feel free to fork the repository and submit pull requests.
This project is open-source and available under the MIT License. See the LICENSE file for more details.