From 571c78202168eece080597e366d0347580caff7b Mon Sep 17 00:00:00 2001 From: Josh Greenwood <31244208+joshgreenwood2003@users.noreply.github.com> Date: Sat, 23 Jul 2022 17:00:34 +0100 Subject: [PATCH 1/4] process split text before checking if it is empty --- TTS/engine_wrapper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TTS/engine_wrapper.py b/TTS/engine_wrapper.py index 267f47a..354f467 100644 --- a/TTS/engine_wrapper.py +++ b/TTS/engine_wrapper.py @@ -94,7 +94,7 @@ class TTSEngine: offset = 0 for idy, text_cut in enumerate(split_text): # print(f"{idx}-{idy}: {text_cut}\n") - if not text_cut or text_cut.isspace(): + if not process_text(text_cut) or process_text(text_cut.isspace()): offset += 1 continue From 9065f563f17a5d5a0cd8372d7eb3bce9c2da2cb9 Mon Sep 17 00:00:00 2001 From: Josh Greenwood <31244208+joshgreenwood2003@users.noreply.github.com> Date: Sat, 23 Jul 2022 23:33:18 +0100 Subject: [PATCH 2/4] improved efficiency of empty text check --- TTS/engine_wrapper.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/TTS/engine_wrapper.py b/TTS/engine_wrapper.py index 354f467..a415680 100644 --- a/TTS/engine_wrapper.py +++ b/TTS/engine_wrapper.py @@ -94,11 +94,12 @@ class TTSEngine: offset = 0 for idy, text_cut in enumerate(split_text): # print(f"{idx}-{idy}: {text_cut}\n") - if not process_text(text_cut) or process_text(text_cut.isspace()): + new_text = process_text(text_cut) + if not new_text or new_text.isspace(): offset += 1 continue - self.call_tts(f"{idx}-{idy - offset}.part", text_cut) + self.call_tts(f"{idx}-{idy - offset}.part", new_text) split_files.append( AudioFileClip(f"{self.path}/{idx}-{idy - offset}.part.mp3") ) @@ -119,7 +120,7 @@ class TTSEngine: def call_tts(self, filename: str, text: str): self.tts_module.run( - text=process_text(text), filepath=f"{self.path}/{filename}.mp3" + text, filepath=f"{self.path}/{filename}.mp3" ) # try: # self.length += MP3(f"{self.path}/{filename}.mp3").info.length From 53986c72ee9fb6154ac69784f4b3e4f1286b8fca Mon Sep 17 00:00:00 2001 From: Josh Greenwood <31244208+joshgreenwood2003@users.noreply.github.com> Date: Sun, 24 Jul 2022 01:37:10 +0100 Subject: [PATCH 3/4] process text before all calls to call_tts Process_text now called before call_tts to ensure it is called minimally while allowing for verification that text is not empty --- TTS/engine_wrapper.py | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/TTS/engine_wrapper.py b/TTS/engine_wrapper.py index a415680..7eb0501 100644 --- a/TTS/engine_wrapper.py +++ b/TTS/engine_wrapper.py @@ -13,7 +13,7 @@ from utils.console import print_step, print_substep from utils.voice import sanitize_text from utils import settings -DEFAULT_MAX_LENGTH: int = 50 # video length variable +DEFUALT_MAX_LENGTH: int = 50 # video length variable class TTSEngine: @@ -35,15 +35,13 @@ class TTSEngine: tts_module, reddit_object: dict, path: str = "assets/temp/mp3", - max_length: int = DEFAULT_MAX_LENGTH, - last_clip_length: int = 0, + max_length: int = DEFUALT_MAX_LENGTH, ): self.tts_module = tts_module() self.reddit_object = reddit_object self.path = path self.max_length = max_length self.length = 0 - self.last_clip_length = last_clip_length def run(self) -> Tuple[int, int]: @@ -57,28 +55,25 @@ class TTSEngine: print_step("Saving Text to MP3 files...") - self.call_tts("title", self.reddit_object["thread_title"]) + self.call_tts("title", process_text(self.reddit_object["thread_title"])) + processed_text = process_text(self.reddit_object["thread_post"]) if ( - self.reddit_object["thread_post"] != "" + processed_text != "" and settings.config["settings"]["storymode"] == True ): - self.call_tts("posttext", self.reddit_object["thread_post"]) + self.call_tts("posttext", processed_text) idx = None - for idx, comment in track( - enumerate(self.reddit_object["comments"]), "Saving..." - ): + for idx, comment in track(enumerate(self.reddit_object["comments"]), "Saving..."): # ! Stop creating mp3 files if the length is greater than max length. if self.length > self.max_length: - self.length -= self.last_clip_length - idx -= 1 break if ( len(comment["comment_body"]) > self.tts_module.max_chars ): # Split the comment if it is too long self.split_post(comment["comment_body"], idx) # Split the comment else: # If the comment is not too long, just call the tts engine - self.call_tts(f"{idx}", comment["comment_body"]) + self.call_tts(f"{idx}", process_text(comment["comment_body"])) print_substep("Saved Text to MP3 files successfully.", style="bold green") return self.length, idx @@ -94,15 +89,13 @@ class TTSEngine: offset = 0 for idy, text_cut in enumerate(split_text): # print(f"{idx}-{idy}: {text_cut}\n") - new_text = process_text(text_cut) + new_text = process_text(text_cut) if not new_text or new_text.isspace(): offset += 1 continue self.call_tts(f"{idx}-{idy - offset}.part", new_text) - split_files.append( - AudioFileClip(f"{self.path}/{idx}-{idy - offset}.part.mp3") - ) + split_files.append(AudioFileClip(f"{self.path}/{idx}-{idy - offset}.part.mp3")) CompositeAudioClip([concatenate_audioclips(split_files)]).write_audiofile( f"{self.path}/{idx}.mp3", fps=44100, verbose=False, logger=None @@ -119,17 +112,13 @@ class TTSEngine: # Path(f"{self.path}/{idx}-{i}.part.mp3").unlink() def call_tts(self, filename: str, text: str): - self.tts_module.run( - text, filepath=f"{self.path}/{filename}.mp3" - ) + self.tts_module.run(text, filepath=f"{self.path}/{filename}.mp3") # try: # self.length += MP3(f"{self.path}/{filename}.mp3").info.length # except (MutagenError, HeaderNotFoundError): # self.length += sox.file_info.duration(f"{self.path}/{filename}.mp3") try: clip = AudioFileClip(f"{self.path}/{filename}.mp3") - if clip.duration + self.length < self.max_length: - self.last_clip_length = clip.duration self.length += clip.duration clip.close() except: From 7df0671b70d76837da1b31c198023f88bf90f854 Mon Sep 17 00:00:00 2001 From: Josh Greenwood <31244208+joshgreenwood2003@users.noreply.github.com> Date: Sun, 24 Jul 2022 01:47:14 +0100 Subject: [PATCH 4/4] formatting --- TTS/engine_wrapper.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/TTS/engine_wrapper.py b/TTS/engine_wrapper.py index 7eb0501..6d09c7a 100644 --- a/TTS/engine_wrapper.py +++ b/TTS/engine_wrapper.py @@ -13,7 +13,7 @@ from utils.console import print_step, print_substep from utils.voice import sanitize_text from utils import settings -DEFUALT_MAX_LENGTH: int = 50 # video length variable +DEFAULT_MAX_LENGTH: int = 50 # video length variable class TTSEngine: @@ -35,13 +35,15 @@ class TTSEngine: tts_module, reddit_object: dict, path: str = "assets/temp/mp3", - max_length: int = DEFUALT_MAX_LENGTH, + max_length: int = DEFAULT_MAX_LENGTH, + last_clip_length: int = 0, ): self.tts_module = tts_module() self.reddit_object = reddit_object self.path = path self.max_length = max_length self.length = 0 + self.last_clip_length = last_clip_length def run(self) -> Tuple[int, int]: @@ -64,9 +66,13 @@ class TTSEngine: self.call_tts("posttext", processed_text) idx = None - for idx, comment in track(enumerate(self.reddit_object["comments"]), "Saving..."): + for idx, comment in track( + enumerate(self.reddit_object["comments"]), "Saving..." + ): # ! Stop creating mp3 files if the length is greater than max length. if self.length > self.max_length: + self.length -= self.last_clip_length + idx -= 1 break if ( len(comment["comment_body"]) > self.tts_module.max_chars @@ -95,7 +101,9 @@ class TTSEngine: continue self.call_tts(f"{idx}-{idy - offset}.part", new_text) - split_files.append(AudioFileClip(f"{self.path}/{idx}-{idy - offset}.part.mp3")) + split_files.append( + AudioFileClip(f"{self.path}/{idx}-{idy - offset}.part.mp3") + ) CompositeAudioClip([concatenate_audioclips(split_files)]).write_audiofile( f"{self.path}/{idx}.mp3", fps=44100, verbose=False, logger=None @@ -112,13 +120,17 @@ class TTSEngine: # Path(f"{self.path}/{idx}-{i}.part.mp3").unlink() def call_tts(self, filename: str, text: str): - self.tts_module.run(text, filepath=f"{self.path}/{filename}.mp3") + self.tts_module.run( + text, filepath=f"{self.path}/{filename}.mp3" + ) # try: # self.length += MP3(f"{self.path}/{filename}.mp3").info.length # except (MutagenError, HeaderNotFoundError): # self.length += sox.file_info.duration(f"{self.path}/{filename}.mp3") try: clip = AudioFileClip(f"{self.path}/{filename}.mp3") + if clip.duration + self.length < self.max_length: + self.last_clip_length = clip.duration self.length += clip.duration clip.close() except: