Tutorial: Narrate an Article into One Audio File
Build a small Python script that turns a multi-paragraph article into one WAV file, step by step.
In this tutorial you build narrate.py, a short Python script that reads an article from a text file and saves it as one narrated audio file.
This is what your script makes by the end:
You get there in five steps. You run the script at the end of every step, so you always have something that works:
- Set up a project folder.
- Turn one sentence into audio.
- Split the article into short pieces.
- Turn every piece into audio.
- Join the pieces into one file.
It takes about 15 minutes. The script uses only the Python standard library, so there is nothing to install.
Before you start
You need:
- Python 3.9 or later. Check with
python3 --version. - A Smallest AI API key. If you don’t have one yet, follow Step 1 of the Quickstart, then come back.
- About 1,600 characters of TTS credit. That’s all the text the tutorial sends, from start to finish.
Step 1: Set up a project folder
Make a folder for the tutorial and move into it:
Export your API key in the same terminal. The script reads it from there:
Save the text below as article.txt in the folder. Keep the empty lines between the paragraphs:
Your folder has one file, article.txt, and SMALLEST_API_KEY is exported in this terminal.
Step 2: Turn one sentence into audio
Start with the smallest thing that works: one sentence, one audio file.
Create a file called narrate.py in the folder and paste in this code:
Run it:
You should see one line. Your byte count will be a little different:
Open test.wav and listen. The voice meher reads the first sentence of the article.
test.wav plays the sentence “Every lighthouse has its own signature.”
The synthesize function does one job: it sends one piece of text and saves the audio it gets back. You’ll use it, unchanged, in every step from here on.
Got an error instead?
Step 3: Split the article into short pieces
One request can carry up to 8,000 characters, and we recommend about 250 per request. Real articles are often longer, so the script first splits the text into pieces of 250 characters or less. Each piece ends on a full sentence, so no word is cut in half.
This step makes no audio, so it costs nothing. You check the pieces before you pay for them.
Replace everything in narrate.py with this code. The highlighted lines are new:
What changed:
import reat the top, for splitting text.- A new
split_into_chunksfunction belowsynthesize. - The bottom of the file now prints the pieces instead of making audio.
Run it:
You should see five numbered pieces:
Five pieces, each 250 characters or less, each ending with a full stop.
Look at pieces 1 and 2. They are both from the first paragraph. All three of its sentences together come to more than 250 characters, so the paragraph was split in two. A new paragraph always starts a new piece.
Step 4: Turn every piece into audio
Now send each piece to the API and save it as its own numbered file in a parts folder.
Replace everything in narrate.py with this code. The highlighted lines are new:
What changed: only the bottom of the file. It makes a parts folder and calls synthesize once for each piece.
Run it:
The script sends five requests, one after another, in about 10 seconds. Your byte counts will be a little different:
The numbers in the file names have two digits (01, 02), so the files stay in reading order when you list them.
The parts folder has five files, part-01.wav to part-05.wav. Play part-03.wav to hear the whole second paragraph.
Step 5: Join the pieces into one file
The last step joins the five parts, in order, into article.wav. Python’s built-in wave module can do this, because every part has the same format: 24,000 Hz, 16-bit, mono WAV.
Replace everything in narrate.py with this code. The highlighted lines are new:
What changed:
import waveat the top, for reading and writing WAV files.- A new
join_wavsfunction belowsplit_into_chunks. - The bottom of the file now keeps a list of the parts and joins them at the end.
Run it:
The script makes the five parts again, then joins them. Your numbers will be a little different:
Play article.wav. You hear the whole article, from “Every lighthouse has its own signature” to “a hundred years ago”, in one file. It should sound like the sample at the top of this page.
Listen for the places where one part ends and the next begins. Each part ends with a short silence, so the joins sound like the ordinary pause between two sentences.
article.wav holds about 40 seconds of audio: the whole article, read in one voice.
Your folder now looks like this:
What you built
You have a script that narrates a text file. Along the way you:
- Sent a text to speech request using only the Python standard library.
- Split an article into pieces of 250 characters or less that end on full sentences.
- Saved each piece as its own WAV file, in reading order.
- Joined the pieces into one WAV file.
Try it on your own writing: put your text in article.txt and run python3 narrate.py again.