Skip to content

Instantly share code, notes, and snippets.

@adamlaz
Forked from madebyollin/make_audiobook.py
Created March 28, 2018 14:25
Show Gist options
  • Save adamlaz/299ea7bb53041ef2fd7910b0b80b8c51 to your computer and use it in GitHub Desktop.
Save adamlaz/299ea7bb53041ef2fd7910b0b80b8c51 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
To use:
1. install/set-up the google cloud api and dependencies listed on https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/texttospeech/cloud-client
2. install pandoc and pypandoc
3. create and download a service_account.json ("Service account key") from https://console.cloud.google.com/apis/credentials
4. run GOOGLE_APPLICATION_CREDENTIALS=service_account.json python make_audiobook.py book_name.epub
"""
import sys
import os.path
import pypandoc
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US', name='en-US-Wavenet-E')
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
def book_to_text(book_file):
try:
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) as book:
return book.read()
def text_to_mp3(text, file_dest):
lines = text.splitlines()
with open(file_dest, 'wb') as out:
for (i, text_chunk) in enumerate(lines):
# skip empty lines
if (len(text_chunk) > 0):
input_text = texttospeech.types.SynthesisInput(text=text_chunk)
response = client.synthesize_speech(input_text, voice, audio_config)
# this is fine because mp3s can be concatenated naively and still work
out.write(response.audio_content)
# print progress
ticks = min(48, round(i / len(lines) * 48))
bar = "=" * ticks + "." * (48 - ticks)
sys.stdout.write("\r" + "[" + bar + "]")
sys.stdout.flush()
print()
if __name__ == "__main__":
books = sys.argv[1:]
for book_file in books:
print("Processing", book_file)
text = book_to_text(book_file)
mp3_file = os.path.join("/tmp/", os.path.splitext(os.path.basename(book_file))[0] + ".mp3")
text_to_mp3(text, mp3_file)
print("Generated mp3", mp3_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment