From 2bf2ccd7da8cfc8f3b497ae42a547991f6b71604 Mon Sep 17 00:00:00 2001 From: Christopher Walley Date: Wed, 1 Jun 2022 16:11:27 +0200 Subject: [PATCH] Added optional arguments for specifying AskReddit threads and YouTube videos to be used --- main.py | 12 ++++++++---- reddit/askreddit.py | 11 +++++++---- video_creation/background.py | 24 ++++++++++++++++-------- video_creation/voices.py | 2 +- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 8fb3d50..1874c01 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ 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 +import argparse 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." @@ -12,11 +13,14 @@ print_markdown( time.sleep(3) +parser = argparse.ArgumentParser() +parser.add_argument("--RedditID", help="ID of AskReddit thread to fetch") #This is an optional argument for specifiying the Reddit thread that you want +parser.add_argument("--VideoID", help="ID of Youtube video to fetch") #This is an optional argument for specifiying the YouTube video that you want +args = parser.parse_args() -reddit_object = get_askreddit_threads() - +reddit_object = get_askreddit_threads(args.RedditID) length, number_of_comments = save_text_to_mp3(reddit_object) download_screenshots_of_reddit_posts(reddit_object, number_of_comments) -download_background() -chop_background_video(length) +download_background(args.VideoID) +chop_background_video(length, args.VideoID) final_video = make_final_video(number_of_comments) diff --git a/reddit/askreddit.py b/reddit/askreddit.py index 7c7110a..952c14a 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(threadID): """ Returns a list of threads from the AskReddit subreddit. """ @@ -21,9 +21,12 @@ def get_askreddit_threads(): username=os.getenv("REDDIT_USERNAME"), password=os.getenv("REDDIT_PASSWORD"), ) - askreddit = reddit.subreddit("askreddit") - threads = askreddit.hot(limit=25) - submission = list(threads)[random.randrange(0, 25)] + if threadID: + submission = reddit.submission(threadID) + else: + askreddit = reddit.subreddit("askreddit") + threads = askreddit.hot(limit=25) + submission = list(threads)[random.randrange(0, 25)] print_substep(f"Video will be: {submission.title} :thumbsup:") try: diff --git a/video_creation/background.py b/video_creation/background.py index 20dbe73..1f6fba7 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -12,33 +12,41 @@ def get_start_and_end_times(video_length, length_of_clip): return random_time, random_time + video_length -def download_background(): +def download_background(videoID): """Downloads the background video from youtube. Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8) """ + if not videoID: + print_step( + "Shoutout to bbswitzer for his Minecraft parkour " + ) + videoID = "n_Dv4JMiwK8" + url = "https://www.youtube.com/watch?v={}".format(videoID) - if not Path("assets/mp4/background.mp4").is_file(): + if not Path("assets/mp4/{}.mp4".format(videoID)).is_file(): print_step( - "We need to download the Minecraft background video. This is fairly large but it's only done once. 😎" + "We need to download the background video. This is fairly large but it's only done once. 😎" ) print_substep("Downloading the background video... please be patient 🙏") - YouTube("https://www.youtube.com/watch?v=n_Dv4JMiwK8").streams.filter( + YouTube(url).streams.filter( res="720p" ).first().download( "assets/mp4", - filename="background.mp4", + filename="{}.mp4".format(videoID), ) print_substep("Background video downloaded successfully! 🎉", style="bold green") -def chop_background_video(video_length): +def chop_background_video(video_length, videoID): print_step("Finding a spot in the background video to chop...✂️") - background = VideoFileClip("assets/mp4/background.mp4") + if not videoID: + videoID = "n_Dv4JMiwK8" + background = VideoFileClip("assets/mp4/{}.mp4".format(videoID)) start_time, end_time = get_start_and_end_times(video_length, background.duration) ffmpeg_extract_subclip( - "assets/mp4/background.mp4", + "assets/mp4/{}.mp4".format(videoID), start_time, end_time, targetname="assets/mp4/clip.mp4", diff --git a/video_creation/voices.py b/video_creation/voices.py index d719ff9..9687a37 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -17,7 +17,7 @@ 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 = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False, tld="com.au") tts.save(f"assets/mp3/title.mp3") length += MP3(f"assets/mp3/title.mp3").info.length