DEV Community

Sebastian Leukhin
Sebastian Leukhin

Posted on

Text to speech (book to audiobook)

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:

  1. I have some "simple" books that I can take by listening to them
  2. 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
  3. 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

  1. Language Support: gTTS supports a wide range of languages, allowing users to convert text to speech in English, Spanish, French, and many others
  2. 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
  3. 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.")
Enter fullscreen mode Exit fullscreen mode

Ready simple solution

I created the small repo to transform .epub and .fb2 formats to mp3 - text-to-speech

Enjoy your audiobooks! 😉

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay