Skip to content

Commit

Permalink
Merge pull request #167 from aedocw/chapter-breaks-in-text
Browse files Browse the repository at this point in the history
Chapter breaks in text
  • Loading branch information
aedocw authored Jan 6, 2024
2 parents a3b5ad5 + ff1fad3 commit a01a02b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ This script takes an epub (or text file) and reads it to an m4b audiobook file,

I recognize this is not very user friendly, but I wanted to share in case folks thought it was useful. If there are a few more people than myself that find this is useful I will keep working on turning it into something that could be used by someone without dev experience.

**NOTE:** Latest release adds a new workflow allowing you to export the epub to text, make any necessary modifications, then read the book as a text file. Any line beginning with "# " is considered a chapter break, and will be automatically inserted during export, named "# Part 1", etc. If you replace "Part 1" with whatever you want that section to be called it will be labeled that way in the audiobook metadata.

**NOTE:** DeepSpeed support for XTTS has been added! If deepspeed is installed and you have a compatible GPU, it will be detected and used. For XTTS, this will yeild a 3x-4x speed improvement! Install deepspeed with `pip install deepspeed`.

**NOTE:** The Coqui team released their curated XTTS voice models recently, and they sound great. A recent update here
Expand All @@ -14,10 +16,6 @@ Example usage: `epub2tts my-book.epub --engine xtts --speaker "Damien Black"`

Example usage: `epub2tts my-book.epub --start 4 --end 20 --xtts shadow-1.wav,shadow-2.wav,shadow-3.wav`

**NOTE:** Now with [OpenAI TTS](https://platform.openai.com/docs/guides/text-to-speech) support! It's not free, but the average cost for a few books I tested was around $7. If you use `--openai <API key>` flag epub2tts will provide a cost estimate and prompt you to approve before continuing.

**NOTE:** HUGE thanks to a recent PR from [wonka929](https://github.com/wonka929), epub2tts now recognizes when a CUDA GPU is available and will use it automatically. In a brief test I did, the speedup was incredible!

## USAGE:
Usage:

Expand All @@ -35,6 +33,8 @@ To skip reading any links, add: `--skiplinks`

Using `--scan` will list excerpts of each chapter, then exit. This is helpful for finding which chapter to start and end on if you want to skip bibliography, TOC, etc.

Using `--export txt` will export the entire book to text file. This will honor `--start` and `--end` arguments as well.

To specify which chapter to start on (ex 3): `--start 3`

To specify which chapter to end on (ex 20): `--end 20`
Expand Down
33 changes: 24 additions & 9 deletions epub2tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(
self.output_filename = self.bookname + ".m4b"
self.chapters = []
self.chapters_to_read = []
self.section_names = []
if source.endswith(".epub"):
self.book = epub.read_epub(source)
self.sourcetype = "epub"
Expand Down Expand Up @@ -110,7 +111,10 @@ def generate_metadata(self, files, title, author):
file.write("TIMEBASE=1/1000\n")
file.write("START=" + str(start_time) + "\n")
file.write("END=" + str(start_time + duration) + "\n")
file.write("title=Part " + str(chap) + "\n")
if len(self.section_names) > 0:
file.write(f"title={self.section_names[chap-1]}\n")
else:
file.write("title=Part " + str(chap) + "\n")
chap += 1
start_time += duration

Expand Down Expand Up @@ -210,13 +214,24 @@ def get_chapters_text(self):
text = file.read()
text = self.prep_text(text)
max_len = 50000
while len(text) > max_len:
pos = text.rfind(" ", 0, max_len) # find the last space within the limit
self.chapters_to_read.append(text[:pos])
print(f"Part: {len(self.chapters_to_read)}")
print(str(self.chapters_to_read[-1])[:256])
text = text[pos + 1 :] # +1 to avoid starting the next chapter with a space
self.chapters_to_read.append(text)
lines_with_hashtag = [line for line in text.splitlines() if line.startswith("# ")]
if lines_with_hashtag:
for line in lines_with_hashtag:
self.section_names.append(line.lstrip("# ").strip())
sections = re.split(r"#\s+", text.strip())[1:]
for section in sections:
if self.sayparts == False:
lines = section.splitlines()
section = "\n".join(lines[1:])
self.chapters_to_read.append(section.strip())
else:
while len(text) > max_len:
pos = text.rfind(" ", 0, max_len) # find the last space within the limit
self.chapters_to_read.append(text[:pos])
print(f"Part: {len(self.chapters_to_read)}")
print(str(self.chapters_to_read[-1])[:256])
text = text[pos + 1 :] # +1 to avoid starting the next chapter with a space
self.chapters_to_read.append(text)
self.end = len(self.chapters_to_read)

def read_chunk_xtts(self, sentences, wav_file_path):
Expand Down Expand Up @@ -408,7 +423,7 @@ def read_book(self, voice_samples, engine, openai, model_name, speaker, bitrate)
print(f"{outputflac} exists, skipping to next chapter")
else:
tempfiles = []
if self.sayparts:
if self.sayparts and len(self.section_names) == 0:
chapter = "Part " + str(partnum + 1) + ". " + self.chapters_to_read[i]
else:
chapter = self.chapters_to_read[i]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
author_email="doc@aedo.net",
url="https://github.com/aedocw/epub2tts",
license="Apache License, Version 2.0",
version="2.3.3",
version="2.3.4",
packages=find_packages(),
install_requires=requirements,
py_modules=["epub2tts"],
Expand Down

0 comments on commit a01a02b

Please sign in to comment.