How to turn your tales into mp3 files so you can listen to them with your kids!

StackZero
CodeX
Published in
3 min readSep 15, 2022
Photo by Nong V on Unsplash

Hi reader, I have another simple proposal for you totally, again unrelated to cybersecurity!

Here we made an AI which writes tales for us, but now we want to further amaze our kids, so let’s extend the project and make our script generate an mp3 with the generated tales.

I am an advocate of learning by doing and this is a nice practical project for beginners where you can learn:

  • How to import and use and install an external module
  • How to read files
  • How to get arguments from the command line

So let’s extend our Tale Generator!

Step #1: Install the dependencies

The first step is obvious, we need to install an external TTS library:

  • gTTS: We will use it to Convert the text to speech (TTS) and store the result in a mp3 file.

I assume you have python and pip installed and configured, so go to your OS command line and write:

pip install gTTS

It will install the Google TTS library.

The library in question is free but has a daily limit on requests, so you cannot use it for an entire long book but just for short texts (I’ll show you another solution for that in a future article)!

Step #2: Write the code

Photo by Markus Spiske on Unsplash

The first step is to import the libraries we need.

We have already seen gTTS but now I just want to describe how we will use two other core libraries (they don’t need to be installed).

  • pathlib: we are going to use it to get the file name without extension.
  • sys: in our project will be the library in charge of managing the CLI arguments.

So let’s import them all:

from gtts import gTTS
from pathlib import Path
import sys

Now it’s time to write our main method:

if __name__ == "__main__":
filename = sys.argv[1]
with open(filename, "r") as f:
text = f.read()
tts = gTTS(text)
name = Path(filename).stem
tts.save(f"name.mp3")

Let’s analyze what the code is doing:

  • Saves the 2nd argument in a variable (the first argument has index 0 and is the script name)
  • Opens a file in reading mode
  • Reads a file and put the content into a variable called “text”
  • Creates a gTTS object, you can check here the documentation
  • Gets the name of the file without the extension
  • Saves the file in the same directory of the script by giving it the same name with the “mp3” extension.

Step #3: Look at the results

Photo by Ryan Klaus on Unsplash

Now it’s time to enjoy our work and test it.

Let’s imagine having a text file named “My Story.txt” inside the same directory of the script (which is named main.py)!

We want to run that, and the way we are doing that is by typing this on our command line:

python main.py "My Story.txt"

The result will be an mp3 file named: “My Story.mp3” containing the entire speech!

Conclusion

That was a very basic project, but I found it very useful in many cases (I can follow a text better when someone reads it).
I hope will be the same for you!

If you liked this content you can follow me on Medium and maybe take a look at my blog here

Thank you a lot for your attention!

--

--

StackZero
CodeX
Writer for

I have a passion for sharing my knowledge and helping others stay safe online. I just want to share tips and advice useful for me.