From cdb6f85d98c7f3b342d0730428020c154828c884 Mon Sep 17 00:00:00 2001 From: 5vl Date: Fri, 3 Jun 2022 22:39:39 +0200 Subject: [PATCH] Added pyttsx3 instead of gTTS, now has a male voice. You can probably do more with this than me - I'm new to python. I just preferred a male voice over the old (annoying) female voice! --- .env.template | 3 +++ main.py | 4 ++-- requirements.txt | 2 +- video_creation/final_video.py | 4 ++-- video_creation/voices.py | 30 ++++++++++++++++-------------- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/.env.template b/.env.template index 5ab4ae4..570d04c 100644 --- a/.env.template +++ b/.env.template @@ -9,3 +9,6 @@ REDDIT_2FA="" THEME="" SUBREDDIT="" + +# Set the talk speed for TTS. Recommended option is 180 +TALK_SPEED= \ No newline at end of file diff --git a/main.py b/main.py index 8cc3c9a..1a7428f 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import time from reddit.subreddit import get_subreddit_threads from video_creation.background import download_background, chop_background_video -from video_creation.voices import save_text_to_mp3 +from video_creation.voices import save_text_to_wav 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 @@ -19,7 +19,7 @@ time.sleep(3) reddit_object = get_subreddit_threads() load_dotenv() -length, number_of_comments = save_text_to_mp3(reddit_object) +length, number_of_comments = save_text_to_wav(reddit_object) download_screenshots_of_reddit_posts(reddit_object, number_of_comments, os.getenv("THEME")) download_background() chop_background_video(length) diff --git a/requirements.txt b/requirements.txt index fd28b7f..4670478 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -gTTS==2.2.4 moviepy==1.0.3 mutagen==1.45.1 playwright==1.22.0 @@ -6,3 +5,4 @@ praw==7.6.0 python-dotenv==0.20.0 rich==12.4.4 yt_dlp==2022.5.18 +pyttsx3==2.90 \ No newline at end of file diff --git a/video_creation/final_video.py b/video_creation/final_video.py index e1f71ff..0afac4f 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -27,8 +27,8 @@ def make_final_video(number_of_clips): # Gather all audio clips audio_clips = [] for i in range(0, number_of_clips): - audio_clips.append(AudioFileClip(f"assets/mp3/{i}.mp3")) - audio_clips.insert(0, AudioFileClip(f"assets/mp3/title.mp3")) + audio_clips.append(AudioFileClip(f"assets/wav/{i}.wav")) + audio_clips.insert(0, AudioFileClip(f"assets/wav/title.wav")) audio_concat = concatenate_audioclips(audio_clips) audio_composite = CompositeAudioClip([audio_concat]) diff --git a/video_creation/voices.py b/video_creation/voices.py index e8d8e43..340a7f5 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -1,34 +1,36 @@ -from gtts import gTTS +import pyttsx3 from pathlib import Path -from mutagen.mp3 import MP3 +from mutagen.wave import WAVE from utils.console import print_step, print_substep from rich.progress import track -def save_text_to_mp3(reddit_obj): - """Saves Text to MP3 files. +def save_text_to_wav(reddit_obj): + """Saves Text to WAV files. Args: reddit_obj : The reddit object you received from the reddit API in the askreddit.py file. """ - print_step("Saving Text to MP3 files...") + print_step("Saving Text to WAV files...") length = 0 # Create a folder for the mp3 files. - Path("assets/mp3").mkdir(parents=True, exist_ok=True) + Path("assets/wav").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 + engine = pyttsx3.init() + engine.setProperty("rate", 180) + engine.save_to_file(reddit_obj["thread_title"], f"assets/wav/title.wav") + engine.runAndWait() + length += WAVE(f"assets/wav/title.wav").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 + # ! Stop creating wav 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 + engine.save_to_file(comment["comment_body"], f"assets/wav/{idx}.wav") + engine.runAndWait() + length += WAVE(f"assets/wav/{idx}.wav").info.length - print_substep("Saved Text to MP3 files successfully.", style="bold green") + print_substep("Saved Text to WAV files successfully.", style="bold green") # ! Return the index so we know how many screenshots of comments we need to make. return length, idx