from gtts import gTTS
import os
def create_audiobook(text_file, output_file):
with open(text_file, 'r', encoding='utf-8') as file:
text = file.read()
tts = gTTS(text=text, lang='en')
tts.save(output_file)
print(f"Audiobook saved as {output_file}")
text_file = "clcoding_hindi.txt"
output_file = "clcoding_hindi.mp3"
create_audiobook(text_file, output_file)
os.system(f"start {output_file}")
#source code --> clcoding.com
Code Explanation:
1. Importing Required Libraries:
from gtts import gTTS
import os
from gtts import gTTS:
Imports the gTTS class from the gtts library, which is used to convert text to speech.
gTTS allows you to specify the text, language, and other options for generating the speech.
import os:
Imports the os module, which provides functions to interact with the operating system, such as running commands (os.system) to play the generated MP3 file.
2. Defining the create_audiobook Function:
def create_audiobook(text_file, output_file):
What It Does:
Defines a function named create_audiobook that takes two arguments:
text_file: Path to the input text file containing the content to be converted into speech.
output_file: Name of the MP3 file to save the audiobook.
3. Reading the Text File:
with open(text_file, 'r', encoding='utf-8') as file:
text = file.read()
open(text_file, 'r', encoding='utf-8'):
Opens the specified text_file in read mode ('r') with UTF-8 encoding to handle special characters if present.
with Statement:
Ensures the file is properly closed after it is read.
text = file.read():
Reads the entire content of the file and stores it in the variable text.
4. Converting Text to Speech:
tts = gTTS(text=text, lang='en')
gTTS(text=text, lang='en'):
Converts the text to speech using the gTTS library.
Arguments:
text: The text content to convert.
lang='en': Specifies the language for the speech (English in this case).
tts:
A gTTS object that contains the generated speech.
5. Saving the Audiobook:
tts.save(output_file)
tts.save(output_file):
Saves the generated speech to an MP3 file specified by output_file.
6. Confirmation Message:
print(f"Audiobook saved as {output_file}")
print(...):
Prints a confirmation message to inform the user that the audiobook has been saved successfully.
7. Specifying the Input and Output Files:
text_file = "clcoding_hindi.txt"
output_file = "clcoding_hindi.mp3"
text_file:
Specifies the name of the input text file that contains the content to be converted to speech.
output_file:
Specifies the name of the output MP3 file to save the audiobook.
8. Calling the Function:
create_audiobook(text_file, output_file)
create_audiobook(...):
Calls the create_audiobook function with the text_file and output_file arguments to generate the audiobook.
9. Playing the Audiobook:
os.system(f"start {output_file}")
os.system(f"start {output_file}"):
Executes an operating system command to play the generated MP3 file.
Platform-Specific:
On Windows: start opens the file in the default media player.
On Linux or macOS, you might use commands like xdg-open or open instead.
0 Comments:
Post a Comment