From 81f0604335c17e8c95bcf5a5a20e875358ceea5d Mon Sep 17 00:00:00 2001 From: Jacob Chambers Date: Sat, 4 Jun 2022 01:20:40 +0100 Subject: [PATCH] Adjustments to video length limiter for YouTube shorts --- video_creation/voices.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/video_creation/voices.py b/video_creation/voices.py index e8d8e43..555a682 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -4,6 +4,13 @@ from mutagen.mp3 import MP3 from utils.console import print_step, print_substep from rich.progress import track +import os + +def total_length(largest_file_index): + length = MP3(f"assets/mp3/title.mp3").info.length + for index in range(largest_file_index + 1): + length += MP3(f"assets/mp3/{index}.mp3").info.length + return length def save_text_to_mp3(reddit_obj): """Saves Text to MP3 files. @@ -12,23 +19,22 @@ def save_text_to_mp3(reddit_obj): reddit_obj : The reddit object you received from the reddit API in the askreddit.py file. """ print_step("Saving Text to MP3 files...") - length = 0 + total_files = 0 # Create a folder for the mp3 files. Path("assets/mp3").mkdir(parents=True, exist_ok=True) - tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False) + tts = gTTS(text=reddit_obj["thread_title"], tld=os.getenv("ACCENT"), slow=False) tts.save(f"assets/mp3/title.mp3") - length += MP3(f"assets/mp3/title.mp3").info.length - for idx, comment in track(enumerate(reddit_obj["comments"]), "Saving..."): - # ! Stop creating mp3 files if the length is greater than 50 seconds. This can be longer, but this is just a good starting point - if length > 50: + for comment_num, comment in track(enumerate(reddit_obj["comments"]), "Saving..."): + tts = gTTS(text=comment["comment_body"], tld=os.getenv("ACCENT"), slow=False) + tts.save(f"assets/mp3/{total_files}.mp3") + if total_length(total_files) < 60: + total_files += 1 + else: break - tts = gTTS(text=comment["comment_body"], lang="en", slow=False) - tts.save(f"assets/mp3/{idx}.mp3") - length += MP3(f"assets/mp3/{idx}.mp3").info.length print_substep("Saved Text to MP3 files successfully.", style="bold green") # ! Return the index so we know how many screenshots of comments we need to make. - return length, idx + return total_length(total_files - 1), total_files