From c03bd2ae82f9b19b8f9bfd039b06dedbac657c7b Mon Sep 17 00:00:00 2001 From: Arjun Dureja Date: Wed, 1 Jun 2022 13:42:22 -0400 Subject: [PATCH 1/3] Add male voice --- .env.template | 3 ++- main.py | 5 ++++- video_creation/voices.py | 29 ++++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.env.template b/.env.template index e00c242..baf9d46 100644 --- a/.env.template +++ b/.env.template @@ -1,4 +1,5 @@ REDDIT_CLIENT_ID="" REDDIT_CLIENT_SECRET="" REDDIT_USERNAME="" -REDDIT_PASSWORD="" \ No newline at end of file +REDDIT_PASSWORD="" +VOICE="female" \ No newline at end of file diff --git a/main.py b/main.py index 8fb3d50..a394dc0 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,8 @@ from video_creation.background import download_background, chop_background_video from video_creation.voices import save_text_to_mp3 from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts from video_creation.final_video import make_final_video +from dotenv import load_dotenv +import os print_markdown( "### Thanks for using this tool! 😊 [Feel free to contribute to this project on GitHub!](https://lewismenelaws.com). If you have any questions, feel free to reach out to me on Twitter or submit a GitHub issue." @@ -15,7 +17,8 @@ time.sleep(3) reddit_object = get_askreddit_threads() -length, number_of_comments = save_text_to_mp3(reddit_object) +load_dotenv() +length, number_of_comments = save_text_to_mp3(reddit_object, os.getenv("VOICE")) download_screenshots_of_reddit_posts(reddit_object, number_of_comments) download_background() chop_background_video(length) diff --git a/video_creation/voices.py b/video_creation/voices.py index d719ff9..4000245 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -3,9 +3,10 @@ from pathlib import Path from mutagen.mp3 import MP3 from utils.console import print_step, print_substep from rich.progress import track +import requests -def save_text_to_mp3(reddit_obj): +def save_text_to_mp3(reddit_obj, voice): """Saves Text to MP3 files. Args: @@ -17,16 +18,34 @@ def save_text_to_mp3(reddit_obj): # 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, tld="co.uk") - tts.save(f"assets/mp3/title.mp3") + if voice == "female": + tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False, tld="co.uk") + tts.save(f"assets/mp3/title.mp3") + elif voice == "male": + url = 'https://streamlabs.com/polly/speak' + body = {'voice': 'Brian', 'text': reddit_obj["thread_title"]} + response = requests.post(url, data = body) + voice_data = requests.get(response.json()['speak_url']) + f = open('assets/mp3/title.mp3', 'wb') + f.write(voice_data.content) + 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: break - tts = gTTS(text=comment["comment_body"], lang="en") - tts.save(f"assets/mp3/{idx}.mp3") + + if voice == "female": + tts = gTTS(text=comment["comment_body"], lang="en") + tts.save(f"assets/mp3/{idx}.mp3") + elif voice == "male": + body = {'voice': 'Brian', 'text': comment["comment_body"]} + response = requests.post(url, data = body) + voice_data = requests.get(response.json()['speak_url']) + f = open(f"assets/mp3/{idx}.mp3", 'wb') + f.write(voice_data.content) + length += MP3(f"assets/mp3/{idx}.mp3").info.length print_substep("Saved Text to MP3 files Successfully.", style="bold green") From 3f32feaa2466cd1136e6c2a1875654032e6ee14c Mon Sep 17 00:00:00 2001 From: Arjun Dureja Date: Fri, 3 Jun 2022 13:51:43 -0400 Subject: [PATCH 2/3] add character limit for male voice --- main.py | 4 ++-- reddit/askreddit.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index a394dc0..872362c 100644 --- a/main.py +++ b/main.py @@ -15,9 +15,9 @@ print_markdown( time.sleep(3) -reddit_object = get_askreddit_threads() - load_dotenv() +reddit_object = get_askreddit_threads(os.getenv("VOICE")) + length, number_of_comments = save_text_to_mp3(reddit_object, os.getenv("VOICE")) download_screenshots_of_reddit_posts(reddit_object, number_of_comments) download_background() diff --git a/reddit/askreddit.py b/reddit/askreddit.py index 7c7110a..1e3b3b3 100644 --- a/reddit/askreddit.py +++ b/reddit/askreddit.py @@ -5,7 +5,7 @@ from dotenv import load_dotenv import os -def get_askreddit_threads(): +def get_askreddit_threads(voice): """ Returns a list of threads from the AskReddit subreddit. """ @@ -32,6 +32,8 @@ def get_askreddit_threads(): content["comments"] = [] for top_level_comment in submission.comments: + if voice == "male" and len(top_level_comment.body) > 550: + continue content["comments"].append( { "comment_body": top_level_comment.body, From b9b46774ff25ae256291ec7307d333ea9b585ab9 Mon Sep 17 00:00:00 2001 From: Arjun Dureja Date: Fri, 3 Jun 2022 13:57:20 -0400 Subject: [PATCH 3/3] Create function for generating and saving TTS --- video_creation/voices.py | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/video_creation/voices.py b/video_creation/voices.py index 4000245..27e5c6d 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -18,17 +18,7 @@ def save_text_to_mp3(reddit_obj, voice): # Create a folder for the mp3 files. Path("assets/mp3").mkdir(parents=True, exist_ok=True) - if voice == "female": - tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False, tld="co.uk") - tts.save(f"assets/mp3/title.mp3") - elif voice == "male": - url = 'https://streamlabs.com/polly/speak' - body = {'voice': 'Brian', 'text': reddit_obj["thread_title"]} - response = requests.post(url, data = body) - voice_data = requests.get(response.json()['speak_url']) - f = open('assets/mp3/title.mp3', 'wb') - f.write(voice_data.content) - + generate_and_save_tts(voice, reddit_obj["thread_title"], "assets/mp3/title.mp3") length += MP3(f"assets/mp3/title.mp3").info.length for idx, comment in track(enumerate(reddit_obj["comments"]), "Saving..."): @@ -36,18 +26,21 @@ def save_text_to_mp3(reddit_obj, voice): if length > 50: break - if voice == "female": - tts = gTTS(text=comment["comment_body"], lang="en") - tts.save(f"assets/mp3/{idx}.mp3") - elif voice == "male": - body = {'voice': 'Brian', 'text': comment["comment_body"]} - response = requests.post(url, data = body) - voice_data = requests.get(response.json()['speak_url']) - f = open(f"assets/mp3/{idx}.mp3", 'wb') - f.write(voice_data.content) - + generate_and_save_tts(voice, comment["comment_body"], 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 + +def generate_and_save_tts(voice, text, file_name): + if voice == "female": + tts = gTTS(text=text, lang="en") + tts.save(file_name) + elif voice == "male": + url = 'https://streamlabs.com/polly/speak' + body = {'voice': 'Brian', 'text': text} + response = requests.post(url, data = body) + voice_data = requests.get(response.json()['speak_url']) + f = open(file_name, 'wb') + f.write(voice_data.content) \ No newline at end of file