From 55d8145d70d032ce1271a656f94947626a43a701 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 31 May 2022 22:36:39 -0400 Subject: [PATCH] I hope i don't get a virus --- main.py | 12 +++- requirements.txt | 3 +- video_creation/TTSwrapper.py | 78 +++++++++++++++++++++++++ video_creation/screenshot_downloader.py | 2 +- video_creation/voices.py | 26 ++++++--- 5 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 video_creation/TTSwrapper.py diff --git a/main.py b/main.py index 72a2d6a..b6a9d1c 100644 --- a/main.py +++ b/main.py @@ -15,8 +15,18 @@ time.sleep(2) def main(): - reddit_object = get_subreddit_threads() + cleanup() + def get_obj(): + reddit_obj = get_subreddit_threads() + for comment in (reddit_obj["comments"]): + if len(comment) > 250: + print(comment) + reddit_obj["comments"].remove(comment) + print(reddit_obj["comments"]) + return reddit_obj + + reddit_object = get_obj() length, number_of_comments = save_text_to_mp3(reddit_object) download_screenshots_of_reddit_posts(reddit_object, number_of_comments) download_background() diff --git a/requirements.txt b/requirements.txt index 3589ada..5744dee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,9 @@ praw~=7.6.0 moviepy~=1.0.3 rich~=12.4.4 -gTTS~=2.2.4 mutagen~=1.45.1 pytube~=12.1.0 playwright~=1.22.0 python-dotenv==0.20.0 +typed-ast~=1.5.4 +requests~=2.27.1 diff --git a/video_creation/TTSwrapper.py b/video_creation/TTSwrapper.py new file mode 100644 index 0000000..130f2de --- /dev/null +++ b/video_creation/TTSwrapper.py @@ -0,0 +1,78 @@ +import requests, base64, random, os + +# https://twitter.com/scanlime/status/1512598559769702406 +voices = [ # DISNEY VOICES + 'en_us_ghostface', # Ghost Face + 'en_us_chewbacca', # Chewbacca + 'en_us_c3po', # C3PO + 'en_us_stitch', # Stitch + 'en_us_stormtrooper', # Stormtrooper + 'en_us_rocket', # Rocket + + # ENGLISH VOICES + 'en_au_001', # English AU - Female + 'en_au_002', # English AU - Male + 'en_uk_001', # English UK - Male 1 + 'en_uk_003', # English UK - Male 2 + 'en_us_001', # English US - Female (Int. 1) + 'en_us_002', # English US - Female (Int. 2) + 'en_us_006', # English US - Male 1 + 'en_us_007', # English US - Male 2 + 'en_us_009', # English US - Male 3 + 'en_us_010', # English US - Male 4 + + # EUROPE VOICES + 'fr_001', # French - Male 1 + 'fr_002', # French - Male 2 + 'de_001', # German - Female + 'de_002', # German - Male + 'es_002', # Spanish - Male + + # AMERICA VOICES + 'es_mx_002', # Spanish MX - Male + 'br_001', # Portuguese BR - Female 1 + 'br_003', # Portuguese BR - Female 2 + 'br_004', # Portuguese BR - Female 3 + 'br_005', # Portuguese BR - Male + + # ASIA VOICES + 'id_001', # Indonesian - Female + 'jp_001', # Japanese - Female 1 + 'jp_003', # Japanese - Female 2 + 'jp_005', # Japanese - Female 3 + 'jp_006', # Japanese - Male + 'kr_002', # Korean - Male 1 + 'kr_003', # Korean - Female + 'kr_004', # Korean - Male 2 +] +good_voices = {'good': ['en_us_002', 'en_us_006'], + 'ok': ['en_au_002', 'en_uk_001']} # less en_us_stormtrooper more less en_us_rocket en_us_ghostface + + +class TTTTSWrapper: # TikTok Text-to-Speech Wrapper + def __init__(self): + self.URI_BASE = 'https://api16-normal-useast5.us.tiktokv.com/media/api/text/speech/invoke/?text_speaker=' + + def tts(self, req_text: str = "TikTok Text To Speech", filename: str = 'title.mp3', random_speaker: bool = False): + if len(req_text) > 299: + return ValueError("Text too long must be under 299 characters") + if random_speaker: + req_text = req_text.replace("+", "plus").replace(" ", "+").replace("&", "and") + voice = self.randomvoice() if random_speaker else 'en_us_002' + r = requests.post(f"{self.URI_BASE}{voice}&req_text={req_text}&speaker_map_type=0") + + vstr = [r.json()["data"]["v_str"]][0] + + b64d = base64.b64decode(vstr) + + out = open(filename, "wb") + out.write(b64d) + out.close() + + @staticmethod + def randomvoice(): + ok_or_good = random.randrange(1, 10) + if ok_or_good == 1: # 1/10 chance of ok voice + return random.choice(good_voices['ok']) + return random.choice(good_voices['good']) # 9/10 chance of good voice + diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 9023d82..fec1ada 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -32,7 +32,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): if getenv("ALLOW_NSFW").casefold() == "false": print_substep("NSFW Post Detected. Skipping...") from subprocess import call - call(["python", "main.py"]) + call(["python3", "main.py"]) exit(1) print_substep("Post is NSFW. You are spicy... :fire:") diff --git a/video_creation/voices.py b/video_creation/voices.py index 70f8a17..2ef0bcf 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -1,9 +1,11 @@ -from gtts import gTTS +import sox from pathlib import Path -from mutagen.mp3 import MP3 +from mutagen.mp3 import MP3, HeaderNotFoundError from utils.console import print_step, print_substep from rich.progress import track +from video_creation.TTSwrapper import TTTTSWrapper + def save_text_to_mp3(reddit_obj): """Saves Text to MP3 files. @@ -17,17 +19,23 @@ def save_text_to_mp3(reddit_obj): # Create a folder for the mp3 files. Path("assets/temp/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/temp/mp3/title.mp3") - length += MP3(f"assets/temp/mp3/title.mp3").info.length + ttttsw = TTTTSWrapper() # tiktok text to speech wrapper + ttttsw.tts(reddit_obj["thread_title"], filename=f"assets/temp/mp3/title.mp3", random_speaker=True) + try: + length += MP3(f"assets/temp/mp3/title.mp3").info.length + except HeaderNotFoundError: + length = sox.file_info.duration(f"assets/temp/mp3/title.mp3") 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 mp3 files if the length is greater than 50 seconds. This can be longer, but this is just a good_voices starting point if length > 50: break - tts = gTTS(text=comment["comment_body"], lang="en") - tts.save(f"assets/temp/mp3/{idx}.mp3") - length += MP3(f"assets/temp/mp3/{idx}.mp3").info.length + + ttttsw.tts(comment["comment_body"], filename=f"assets/temp/mp3/{idx}.mp3", random_speaker=False) + try: + length += MP3(f"assets/temp/mp3/{idx}.mp3").info.length + except HeaderNotFoundError: + length = sox.file_info.duration(f"assets/temp/mp3/{idx}.mp3") 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.