Instantly share code, notes, and snippets.

Embed
What would you like to do?
Converting epub file into mp3 audiobook. Example for my blog post (in Polish) https://abcserverless.pl/polly-zrobi-nam-audiobooka/
import textwrap
from epub_conversion.utils import convert_epub_to_lines, convert_lines_to_text, open_book
import boto3
from contextlib import closing
from pydub import AudioSegment
import os
def split_epub(book_name):
book = open_book(book_name)
if book is not None:
str = ""
lines = convert_epub_to_lines(book)
for sentence in lines:
for text in convert_lines_to_text(sentence, 'a'):
text = text.replace('\n', '')
text = text.replace('& # 160 ;', '')
str += text
lines = textwrap.wrap(str, 1500, break_long_words=False)
return lines
def create_audio_files(lines, output_title):
client = boto3.client('polly',
aws_access_key_id='',
aws_secret_access_key='' )
output_file = AudioSegment.silent(duration=2000)
for l in lines:
try:
response = client.synthesize_speech(
OutputFormat='mp3',
Text=l,
TextType='text',
VoiceId='Maja'
)
except ClientError as e:
print(e)
sys.exit(-1)
print("{} {}".format(len(l), l))
print(response['ResponseMetadata']['HTTPStatusCode'])
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
output = "part.mp3"
try:
with open(output, "wb") as file:
file.write(stream.read())
slice = AudioSegment.from_mp3(output)
output_file = output_file + slice
os.remove(output) # remove part file
except IOError as e:
print(e)
sys.exit(-1)
output_file.export(output_title, format="mp3")
lines = split_epub('2.epub')
create_audio_files(lines, "full2.mp3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment