Skip to content

Instantly share code, notes, and snippets.

Last active February 25, 2023 06:10
What would you like to do?
Converts an epub or text file to audiobook via Google Cloud TTS
#!/usr/bin/env python3
To use:
1. install/set-up the google cloud api and dependencies listed on
2. install pandoc and pypandoc, also tqdm
3. create and download a service_account.json ("Service account key") from
4. run GOOGLE_APPLICATION_CREDENTIALS=service_account.json python book_name.epub
import re
import sys
import time
from datetime import datetime as dt
from pathlib import Path
from import texttospeech
from tqdm import tqdm
import pypandoc
# see
def book_to_text(book_file):
return pypandoc.convert_file(book_file, "plain", extra_args=["--wrap=none"])
except RuntimeError:
print("Format not recognized. Treating as plain text...")
with open(book_file, encoding="utf-8") as book:
def clean_text_chunk(text_chunk):
# remove _italics_
text_chunk = re.sub(r"_", " ", text_chunk)
# remove --- hyphens for footnotes
text_chunk = re.sub(r"(\-{3,})", "Footnote:", text_chunk)
return text_chunk
class Narrator:
def __init__(self, voice_name="en-US-Wavenet-F"):
self.client = texttospeech.TextToSpeechClient()
self.voice = texttospeech.types.VoiceSelectionParams(
language_code="en-US", name=voice_name
self.audio_config = texttospeech.types.AudioConfig(
# rate limit stuff
self._minute = -1
self._requests_this_minute = 0
self._chars_this_minute = 0
def print_voice_names(self, lang="en"):
print("Available voices for language {}:".format(lang))
for voice in self.client.list_voices().voices:
def _rate_limit(self):
if (
self._requests_this_minute > MAX_REQUESTS_PER_MINUTE
or self._chars_this_minute > MAX_CHARS_PER_MINUTE
while == self._minute:
if != self._minute:
self._minute =
self._requests_this_minute = 0
self._chars_this_minute = 0
def _text_chunk_to_audio_chunk(self, text_chunk):
input_text = texttospeech.types.SynthesisInput(text=text_chunk)
response = self.client.synthesize_speech(
input_text, self.voice, self.audio_config
self._requests_this_minute += 1
self._chars_this_minute += len(text_chunk)
return response.audio_content
def text_to_mp3(self, text, file_dest):
assert file_dest.suffix == ".mp3"
lines = text.splitlines()
with"wb") as out:
for i, text_chunk in enumerate(tqdm(lines, desc=file_dest.stem)):
# skip empty lines
if text_chunk:
text_chunk = clean_text_chunk(text_chunk)
audio_chunk = self._text_chunk_to_audio_chunk(text_chunk)
# this is fine because mp3s can be concatenated naively and still work
def main():
if not sys.argv[1:]:
"Usage: GOOGLE_APPLICATION_CREDENTIALS=service_account.json {} book_name.epub".format(
narrator = Narrator()
# narrator.print_voice_names()
for book_file in sys.argv[1:]:
text = book_to_text(book_file)
mp3_path = Path(book_file).with_suffix(".mp3")
narrator.text_to_mp3(text, mp3_path)
print("Generated mp3", mp3_path)
# I have another script that uploads to overcast...
# import subprocess as sp
#" '" + str(mp3_path) + "'", shell=True)
if __name__ == "__main__":
Copy link

AtilioA commented Nov 17, 2019

Using OGG_OPUS as audio encoding yields significantly better audio quality but messes with duration/seeking, don't know why.
EDIT: that must be by the fact that "mp3s can be concatenated naively and still work" but ogg files can't... maybe using ffmpeg? We can also produce tiny tracks for every text_chunk and append them all in the end.
Also, I had to change os.path.join("/tmp/" to os.path.join("tmp/" on Windows, and you should put "encoding="UTF8" when treating the files as plain text (I wish we could open pull requests on gists)

Copy link

madebyollin commented Nov 17, 2019

Yeah, mp3 concatenation via cat is a hack that happens to work for the mp3 format. You should use sox or ffmpeg to do proper concatenation. And the os.path stuff should all be using pathlib instead; I'm not sure about the state of temp directories on windows so probably that needs to be fixed too.

Copy link

mysticaltech commented Apr 2, 2020

Amazing script, does it work with pdf? It would be great to turn this into a full blown project!

Copy link

It won't work with a PDF out of the box (since pandoc does not import PDFs). But you can run pdftotext to extract the text first and then run this script on the extracted text. In general, extracting comprehensible text from a PDF is a nightmare, but for simple documents it should work.

Copy link

Ok, thanks for the info and the script, I appreciate it! 🙏🏻

Copy link

The script is awesome and works well! I want to modify the pitch in voice. Can you suggest me some code for that?

Copy link

madebyollin commented Sep 15, 2020

The script is awesome and works well! I want to modify the pitch in voice. Can you suggest me some code for that?

you can add pitch / rate parameters to the audio config (line 48 of the script) as per their documentation, e.g.


Copy link

kerenon commented Nov 8, 2020

Damn this is awesome. Thank you. I think I'm gonna try to turn this into a small personal project. I spent the last hour throwing code at the wall to see what sticks, and it already can create opus albums (1 track / chapter).

Copy link

Nice script! Though if we split the file based on lines, the audio will get jagged. I have made some changes like splitting the file based on "." (periods). If anyone wants to check out my version (based on this script), they can head over to this repo and any improvements or suggestions are welcome too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment