PYTHON PROJECT("MAKING A TEXT TO SPEECH CONVERTER IN A MP3 FILE USING 'gtts and os module' "):
![]() |
Text To Voice Converter in Python |
CODE DESCRIPTION:
Importing Libraries:
The code begins by importing the necessary libraries. It includes the gTTS library for text-to-speech conversion and the os library to interact with the operating system.
Defining text_to_speech() Function:
The code defines a function named text_to_speech(text, filename) that takes two parameters: the text to be converted and the desired filename for the generated audio file.
Text-to-Speech Conversion:
Inside the text_to_speech() function:
The gTTS object is created with the input text. This object represents the text-to-speech conversion process.
The save() method of the gTTS object is called with the provided filename. This method generates an audio file with the spoken version of the input text and saves it with the specified name.
Playing the Audio:
After saving the audio file, the os. system() function is used to execute a system command.
The command "start" followed by the filename is used to open the generated audio file using the default audio player of the system.
Main Execution:
Within the if __name__ == "__main__": block:
The user is prompted to input the text they want to convert to speech.
The desired filename for the generated audio file is set as "output.mp3".
The text_to_speech() function is called with the provided input text and filename, initiating the text-to-speech conversion and audio file creation.
The resulting audio file is then automatically played using the default audio player.
DOWNLOAD SOURCE CODE:
import os
def text_to_speech(text, filename):
tts = gTTS(text)
tts.save(filename)
os.system("start " + filename) # Open the audio file using the default player
if __name__ == "__main__":
input_text = input("Enter the text to convert to speech: ")
output_filename = "output.mp3"
text_to_speech(input_text, output_filename)
0 Comments