diff --git a/main.py b/main.py index 9aabe78..de31f71 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ from video_creation.screenshot_downloader import download_screenshots_of_reddit_ from video_creation.final_video import make_final_video from utils.loader import Loader from dotenv import load_dotenv +import pyttsx3 console = Console() from dotenv import load_dotenv @@ -89,9 +90,27 @@ except: exit() console.log("[bold green]Enviroment Variables are set! Continuing...") + if configured: + engine = pyttsx3.init() + + voices = engine.getProperty("voices") + + array_of_voices = [] + + print("Available voices:") + + + for voice in voices: + array_of_voices.append(voice) + + for i in range(len(array_of_voices)): + print(f"[{i}] {array_of_voices[i].name}\n") + + selected_voice = int(input(f"Choose voice (0-{len(array_of_voices) - 1}): ")) + reddit_object = get_subreddit_threads() - length, number_of_comments = save_text_to_mp3(reddit_object) + length, number_of_comments = save_text_to_mp3(reddit_object, voices, selected_voice) download_screenshots_of_reddit_posts( reddit_object, number_of_comments, os.getenv("THEME", "light") ) diff --git a/requirements.txt b/requirements.txt index fd28b7f..ff27cc4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ -gTTS==2.2.4 +audioread==2.1.9 moviepy==1.0.3 -mutagen==1.45.1 playwright==1.22.0 praw==7.6.0 python-dotenv==0.20.0 +pyttsx3==2.91 rich==12.4.4 -yt_dlp==2022.5.18 +yt_dlp==2022.5.18 \ No newline at end of file diff --git a/video_creation/voices.py b/video_creation/voices.py index c0df8b7..d891e8c 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -1,11 +1,11 @@ -from gtts import gTTS +# from gtts import gTTS +import pyttsx3 from pathlib import Path -from mutagen.mp3 import MP3 from utils.console import print_step, print_substep from rich.progress import track +import audioread - -def save_text_to_mp3(reddit_obj): +def save_text_to_mp3(reddit_obj, voices, selected_voice): """Saves Text to MP3 files. Args: @@ -14,12 +14,22 @@ def save_text_to_mp3(reddit_obj): print_step("Saving Text to MP3 files...") length = 0 + if not selected_voice: + selected_voice = 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.save(f"assets/mp3/title.mp3") - length += MP3(f"assets/mp3/title.mp3").info.length + # tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False) + # tts.save(f"assets/mp3/title.mp3") + engine = pyttsx3.init() + engine.setProperty("rate", 180) + engine.setProperty("voice", voices[selected_voice].id) + engine.save_to_file(reddit_obj["thread_title"], f"assets/mp3/title.mp3") + engine.runAndWait() + + # length += MP3(f"assets/mp3/title.mp3").info.length + length += audioread.audio_open(f"assets/mp3/title.mp3").duration try: Path(f"assets/mp3/posttext.mp3").unlink() @@ -27,17 +37,32 @@ def save_text_to_mp3(reddit_obj): pass if reddit_obj["thread_post"] != "": - tts = gTTS(text=reddit_obj["thread_post"], lang="en", slow=False) - tts.save(f"assets/mp3/posttext.mp3") - length += MP3(f"assets/mp3/posttext.mp3").info.length + # tts = gTTS(text=reddit_obj["thread_post"], lang="en", slow=False) + # tts.save(f"assets/mp3/posttext.mp3") + engine = pyttsx3.init() + engine.setProperty("rate", 180) + engine.setProperty("voice", voices[selected_voice].id) + engine.save_to_file(reddit_obj["thread_post"], f"assets/mp3/posttext.mp3") + engine.runAndWait() + + + # length += MP3(f"assets/mp3/posttext.mp3").info.length + length += audioread.audio_open(f"assets/mp3/posttext.mp3").duration 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: 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 + # tts = gTTS(text=comment["comment_body"], lang="en", slow=False) + # tts.save(f"assets/mp3/{idx}.mp3") + engine = pyttsx3.init() + engine.setProperty("rate", 180) + engine.setProperty("voice", voices[selected_voice].id) + engine.save_to_file(comment["comment_body"], f"assets/mp3/{idx}.mp3") + engine.runAndWait() + + # length += MP3(f"assets/mp3/{idx}.mp3").info.length + length += audioread.audio_open(f"assets/mp3/{idx}.mp3").duration 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.