From d4bdad4d0f04a75e47bcd0af4e3465b242648866 Mon Sep 17 00:00:00 2001 From: reoccurcat Date: Thu, 2 Jun 2022 16:16:28 -0500 Subject: [PATCH 1/4] checks off the custom background todo --- .env.template | 1 - .gitignore | 2 +- README.md | 2 +- main.py | 7 +++-- reddit/subreddit.py | 6 ++-- video_creation/background.py | 40 +++++++++++++++++++------ video_creation/screenshot_downloader.py | 2 +- 7 files changed, 42 insertions(+), 18 deletions(-) diff --git a/.env.template b/.env.template index f00c2ac..bed59c6 100644 --- a/.env.template +++ b/.env.template @@ -6,5 +6,4 @@ REDDIT_PASSWORD="" # Valid options are "yes" and "no" for the variable below REDDIT_2FA="" - SUBREDDIT="" diff --git a/.gitignore b/.gitignore index b541305..ed5aa04 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ assets/ .env reddit-bot-351418-5560ebc49cac.json -__pycache__ \ No newline at end of file +__pycache__/ \ No newline at end of file diff --git a/README.md b/README.md index 00e30da..cfe46c0 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ In its current state, this bot does exactly what it needs to do. However, lots o I have tried to simplify the code so anyone can read it and start contributing at any skill level. Don't be shy :) contribute! - [ ] Allowing users to choose a reddit thread instead of being randomized. -- [ ] Allowing users to choose a background that is picked instead of the Minecraft one. +- [x] Allowing users to choose a background that is picked instead of the Minecraft one. - [x] Allowing users to choose between any subreddit. - [ ] Allowing users to change voice. - [ ] Creating better documentation and adding a command line interface. diff --git a/main.py b/main.py index a5b64c6..7d280ed 100644 --- a/main.py +++ b/main.py @@ -18,6 +18,9 @@ reddit_object = get_subreddit_threads() 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) +while True: + vidpath = download_background(length) + noerror = chop_background_video(length, vidpath) + if noerror is True: + break final_video = make_final_video(number_of_comments) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 5d020fe..75f1a0b 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -8,12 +8,12 @@ import os def get_subreddit_threads(): """ - Returns a list of threads from the AskReddit subreddit. + Returns a list of threads from the provided subreddit. """ load_dotenv() - print_step("Getting AskReddit threads...") + print_step("Getting subbreddit threads...") if os.getenv("REDDIT_2FA").lower() == "yes": print( @@ -50,7 +50,7 @@ def get_subreddit_threads(): threads = subreddit.hot(limit=25) submission = list(threads)[random.randrange(0, 25)] - print_substep(f"Video will be: {submission.title} :thumbsup:") + print_substep(f"Video will be: {submission.title}") try: content["thread_url"] = submission.url diff --git a/video_creation/background.py b/video_creation/background.py index dce46bd..99a2d96 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -7,45 +7,67 @@ from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip from moviepy.editor import VideoFileClip from utils.console import print_step, print_substep +import datetime + def get_start_and_end_times(video_length, length_of_clip): random_time = randrange(180, int(length_of_clip) - int(video_length)) return random_time, random_time + video_length -def download_background(): +def download_background(video_length): """Downloads the background video from youtube. Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8) """ - if not Path("assets/mp4/background.mp4").is_file(): + print_substep("\nPut the URL of the video you want in the background.\nThe default video is a Minecraft parkour video.\n" + "Leave the input field blank to use the default.") + print_substep(f"Make sure the video is longer than {str(datetime.timedelta(seconds=round(video_length + 180)))}!\n", style="red") + + inp = input("URL: ") + + if not inp: + vidurl = "https://www.youtube.com/watch?v=n_Dv4JMiwK8" + else: + vidurl = inp + + vidpath = vidurl.split("v=")[1] + + if not Path(f"assets/mp4/{vidpath}.mp4").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 may be fairly large but it's only done once per background." ) print_substep("Downloading the background video... please be patient.") ydl_opts = { - "outtmpl": "assets/mp4/background.mp4", + "outtmpl": f"assets/mp4/{vidpath}.mp4", "merge_output_format": "mp4", } with YoutubeDL(ydl_opts) as ydl: - ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") + ydl.download(vidurl) print_substep("Background video downloaded successfully!", style="bold green") + + return vidpath -def chop_background_video(video_length): +def chop_background_video(video_length, vidpath): print_step("Finding a spot in the background video to chop...") - background = VideoFileClip("assets/mp4/background.mp4") - + background = VideoFileClip(f"assets/mp4/{vidpath}.mp4") + if background.duration < video_length + 180: + print_substep("This video is too short.", style="red") + noerror = False + return noerror start_time, end_time = get_start_and_end_times(video_length, background.duration) ffmpeg_extract_subclip( - "assets/mp4/background.mp4", + f"assets/mp4/{vidpath}.mp4", start_time, end_time, targetname="assets/mp4/clip.mp4", ) print_substep("Background video chopped successfully!", style="bold green") + noerror = True + return noerror diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 91fed8a..bc5c640 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -11,7 +11,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): reddit_object: The Reddit Object you received in askreddit.py screenshot_num: The number of screenshots you want to download. """ - print_step("Downloading Screenshots of Reddit Posts 📷") + print_step("Downloading screenshots of reddit posts...") # ! Make sure the reddit screenshots folder exists Path("assets/png").mkdir(parents=True, exist_ok=True) From d746fabb6bfc23f110386426c3cd89a922795c6d Mon Sep 17 00:00:00 2001 From: reoccurcat Date: Fri, 3 Jun 2022 11:57:04 -0500 Subject: [PATCH 2/4] idk what happened here but fixed it --- .env.template | 3 --- 1 file changed, 3 deletions(-) diff --git a/.env.template b/.env.template index 602769c..5ab4ae4 100644 --- a/.env.template +++ b/.env.template @@ -6,9 +6,6 @@ REDDIT_PASSWORD="" # Valid options are "yes" and "no" for the variable below REDDIT_2FA="" -<<<<<<< HEAD -======= THEME="" ->>>>>>> 64fa33fd482ef63e8a75cca1a438a37e078aee48 SUBREDDIT="" From 77f68b847c400409a2f19d37cdc1108b4f1ac0bb Mon Sep 17 00:00:00 2001 From: reoccurcat Date: Fri, 3 Jun 2022 12:31:05 -0500 Subject: [PATCH 3/4] fixed dark mode bug --- video_creation/screenshot_downloader.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 9b06ae0..4b07bd7 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -22,11 +22,14 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): browser = p.chromium.launch() context = browser.new_context() - - if theme.casefold() == "dark": - cookie_file = open('video_creation/cookies.json') - cookies = json.load(cookie_file) - context.add_cookies(cookies) + + try: + if theme.casefold() == "dark": + cookie_file = open('video_creation/cookies.json') + cookies = json.load(cookie_file) + context.add_cookies(cookies) + except AttributeError: + pass # Get the thread screenshot page = context.new_page() From 46aedb6afa1958ec43a6113c10bd6809c36243fd Mon Sep 17 00:00:00 2001 From: Hayden Date: Fri, 3 Jun 2022 13:35:31 -0500 Subject: [PATCH 4/4] Update subreddit.py --- reddit/subreddit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 75f1a0b..c3350bc 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -13,7 +13,7 @@ def get_subreddit_threads(): load_dotenv() - print_step("Getting subbreddit threads...") + print_step("Getting subreddit threads...") if os.getenv("REDDIT_2FA").lower() == "yes": print( @@ -31,7 +31,7 @@ def get_subreddit_threads(): reddit = praw.Reddit( client_id=os.getenv("REDDIT_CLIENT_ID"), client_secret=os.getenv("REDDIT_CLIENT_SECRET"), - user_agent="Accessing AskReddit threads", + user_agent="Accessing subreddit threads", username=os.getenv("REDDIT_USERNAME"), password=passkey, )