Merge branch 'develop' into feat/watermark

pull/998/head
Jason 2 years ago committed by GitHub
commit 035db66e28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -35,13 +35,15 @@ class TTSEngine:
tts_module, tts_module,
reddit_object: dict, reddit_object: dict,
path: str = "assets/temp/mp3", path: str = "assets/temp/mp3",
max_length: int = DEFAULT_MAX_LENGTH, max_length: int = DEFUALT_MAX_LENGTH,
last_clip_length: int = 0,
): ):
self.tts_module = tts_module() self.tts_module = tts_module()
self.reddit_object = reddit_object self.reddit_object = reddit_object
self.path = path self.path = path
self.max_length = max_length self.max_length = max_length
self.length = 0 self.length = 0
self.last_clip_length = last_clip_length
def run(self) -> Tuple[int, int]: def run(self) -> Tuple[int, int]:
@ -63,9 +65,13 @@ class TTSEngine:
self.call_tts("posttext", self.reddit_object["thread_post"]) self.call_tts("posttext", self.reddit_object["thread_post"])
idx = None 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. # ! Stop creating mp3 files if the length is greater than max length.
if self.length > self.max_length: if self.length > self.max_length:
self.length -= self.last_clip_length
idx -= 1
break break
if ( if (
len(comment["comment_body"]) > self.tts_module.max_chars len(comment["comment_body"]) > self.tts_module.max_chars
@ -93,7 +99,9 @@ class TTSEngine:
continue continue
self.call_tts(f"{idx}-{idy - offset}.part", text_cut) self.call_tts(f"{idx}-{idy - offset}.part", text_cut)
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( CompositeAudioClip([concatenate_audioclips(split_files)]).write_audiofile(
f"{self.path}/{idx}.mp3", fps=44100, verbose=False, logger=None f"{self.path}/{idx}.mp3", fps=44100, verbose=False, logger=None
@ -110,13 +118,17 @@ class TTSEngine:
# Path(f"{self.path}/{idx}-{i}.part.mp3").unlink() # Path(f"{self.path}/{idx}-{i}.part.mp3").unlink()
def call_tts(self, filename: str, text: str): def call_tts(self, filename: str, text: str):
self.tts_module.run(text=process_text(text), filepath=f"{self.path}/{filename}.mp3") self.tts_module.run(
text=process_text(text), filepath=f"{self.path}/{filename}.mp3"
)
# try: # try:
# self.length += MP3(f"{self.path}/{filename}.mp3").info.length # self.length += MP3(f"{self.path}/{filename}.mp3").info.length
# except (MutagenError, HeaderNotFoundError): # except (MutagenError, HeaderNotFoundError):
# self.length += sox.file_info.duration(f"{self.path}/{filename}.mp3") # self.length += sox.file_info.duration(f"{self.path}/{filename}.mp3")
try: try:
clip = AudioFileClip(f"{self.path}/{filename}.mp3") 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 self.length += clip.duration
clip.close() clip.close()
except: except:

Loading…
Cancel
Save