Hello everyone! I'm sure each of you has a book you want to read but don't have the time.
Some points off the top of my head:
- I have some "simple" books that I can take by listening to them
- I'm blocked when I'm reading a book, because I have to put the book or smartphone somewhere, look at it, read it, and it's pretty hard to do anything else
- Buy two versions of text + audio or only the audio version might not be suitable for some reasons
After these points in my mind, I just looked around our developed IT world and found the solution with a few lines of code to transform my e-books that I already have into audio format for free.
Solution
It turns out that one of the easiest ways to create audio from a text is with the python
library gTTS
.
What is gTTS
?
gTTS
is a Python library that interfaces with Google Translate’s Text-to-Speech API to generate speech from text. It supports multiple languages, and ability to output audio files in MP3 format. gTTS
is commonly used in applications like audiobooks, automated announcements, and accessibility tools.
Key Features of gTTS
- Language Support: gTTS supports a wide range of languages, allowing users to convert text to speech in English, Spanish, French, and many others
- Regional Accents: By specifying the top-level domain (TLD) such as .com, .co.uk, or .co.in, users can influence the accent of the generated speech
- Speed Control: The slow parameter allows users to slow down the speech for clarity.
Example: Creating Speech from Text
from gtts import gTTS
# Define the text to be converted
text = "Hello, welcome to the world of text-to-speech."
# Create a gTTS object
speech = gTTS(text=text, lang='en', tld='com', slow=False)
# Save the generated speech to an MP3 file
speech.save("output.mp3")
print("Audio file 'output.mp3' has been saved.")
Ready simple solution
I created the small repo to transform .epub
and .fb2
formats to mp3 - text-to-speech
Enjoy your audiobooks! 😉
Top comments (0)