From 3578d904faad6f8817b3909245e76e07a8433084 Mon Sep 17 00:00:00 2001 From: dasbts <40334665+dasbts@users.noreply.github.com> Date: Sun, 10 Jul 2022 10:20:05 +0200 Subject: [PATCH] Fix FFMPG + rendered comments missing parts So, I found after some digging that the missing comments in the renders were due to regex, I believe I have fixed them after realizing it was the part after defining the max_chars, so I tested putting the "(\.|.$)" in https://regex101.com/ with the text from one of the best examples I had in my rendered results folder, then I altered it until the render was working. The closest I could get which seem to be perfect, but which might need more testing was: for x in re.finditer(rf" *((.{{0,{self.tts_module.max_chars}}})(.$| $| \n|\n)) Alternatively: for x in re.finditer(rf" *((.{{0,{self.tts_module.max_chars}}})(.$| $|\n | \n)) However, I'm still not sure if this is the best possible regex. Also, I added some new try: excepts: to solve the FFMPEG error, for some reason this just makes it work, but I would like to test it like 500 times for the same thread to see that it always gets the same amount of parts (and preferably the same total length). What I have done is to run main.py and then just let it fetch the temp .mp3 files, then restart when I see sufficient results, rather than render the whole .mp4 video. If anything this can inspire someone to improve the regex further to completely fix the missing parts in some rendered comments (in case this doesn't fully fix). --- TTS/engine_wrapper.py | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) 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"]