From b9b46774ff25ae256291ec7307d333ea9b585ab9 Mon Sep 17 00:00:00 2001 From: Arjun Dureja Date: Fri, 3 Jun 2022 13:57:20 -0400 Subject: [PATCH] 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