diff --git a/TTS/engine_wrapper.py b/TTS/engine_wrapper.py index a171db7..7d29003 100644 --- a/TTS/engine_wrapper.py +++ b/TTS/engine_wrapper.py @@ -2,7 +2,8 @@ from pathlib import Path from typing import Tuple import re - +import time +import os # import sox # from mutagen import MutagenError # from mutagen.mp3 import MP3, HeaderNotFoundError @@ -70,7 +71,16 @@ class TTSEngine: if not self.tts_module.max_chars: self.call_tts(f"{idx}", comment["comment_body"]) else: - self.split_post(comment["comment_body"], idx) + # THESE TRY EXCEPTS SOMEHOW SOLVES FFMPEG ERROR # + try: + self.split_post(comment["comment_body"], idx) + except: + print('Error, removing '+f"{self.path}/{idx}-0.part.mp3") # It still continues + try: + os.remove(f"{self.path}/{idx}-0.part.mp3") + except: + pass + print_substep("Saved Text to MP3 files successfully.", style="bold green") return self.length, idx @@ -79,18 +89,29 @@ class TTSEngine: split_files = [] split_text = [ x.group().strip() - for x in re.finditer(rf" *((.{{0,{self.tts_module.max_chars}}})(\.|.$))", text) + for x in re.finditer(rf" *((.{{0,{self.tts_module.max_chars}}})(.$| $|\n | \n))", text) ] idy = None for idy, text_cut in enumerate(split_text): # print(f"{idx}-{idy}: {text_cut}\n") self.call_tts(f"{idx}-{idy}.part", text_cut) - split_files.append(AudioFileClip(f"{self.path}/{idx}-{idy}.part.mp3")) + # THESE TRY EXCEPTS SOMEHOW SOLVES FFMPEG ERROR # + try: + split_files.append(AudioFileClip(f"{self.path}/{idx}-{idy}.part.mp3")) + except: + print('Error, removing '+f"{self.path}/{idx}-{idy}.part.mp3") + try: + os.remove(f"{self.path}/{idx}-{idy}.part.mp3") + except: + pass + ### !!! Without the following sleep (preferably 1sec), sometimes a part seem to be missing, causing errors or bad renders, + #feel free to try for example same thread 500 times and see how many times it renders the same amount of .mp3 files (not parts), + #can probably build a tracker for this to count how many times each part renders !!! ### + time.sleep(0.5) CompositeAudioClip([concatenate_audioclips(split_files)]).write_audiofile( f"{self.path}/{idx}.mp3", fps=44100, verbose=False, logger=None ) - for i in split_files: name = i.filename i.close() @@ -107,10 +128,12 @@ class TTSEngine: # self.length += MP3(f"{self.path}/{filename}.mp3").info.length # except (MutagenError, HeaderNotFoundError): # self.length += sox.file_info.duration(f"{self.path}/{filename}.mp3") - clip = AudioFileClip(f"{self.path}/{filename}.mp3") - self.length += clip.duration - clip.close() - + try: + clip = AudioFileClip(f"{self.path}/{filename}.mp3") + self.length += clip.duration + clip.close() + except: + self.length = 0 def process_text(text: str): lang = settings.config["reddit"]["thread"]["post_lang"]