From f5560cae6999e6a44491c85024aca68653d1831a Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Wed, 6 Jul 2022 00:26:00 +0100 Subject: [PATCH] refactor: background credit is now a variable --- main.py | 4 ++-- utils/videos.py | 6 ++---- video_creation/background.py | 7 ++++--- video_creation/final_video.py | 12 ++++++------ 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 7b7c726..f98e8db 100755 --- a/main.py +++ b/main.py @@ -38,8 +38,8 @@ def main(POST_ID=None): length = math.ceil(length) download_screenshots_of_reddit_posts(reddit_object, number_of_comments) download_background() - chop_background_video(length) - make_final_video(number_of_comments, length, reddit_object) + credit = chop_background_video(length) + make_final_video(number_of_comments, length, reddit_object, credit) def run_many(times): diff --git a/utils/videos.py b/utils/videos.py index 6377256..ec09362 100755 --- a/utils/videos.py +++ b/utils/videos.py @@ -34,7 +34,7 @@ def check_done( return redditobj -def save_data(filename: str, reddit_title: str, reddit_id: str): +def save_data(filename: str, reddit_title: str, reddit_id: str, credit: str): """Saves the videos that have already been generated to a JSON file in video_creation/data/videos.json Args: @@ -50,9 +50,7 @@ def save_data(filename: str, reddit_title: str, reddit_id: str): payload = { "id": reddit_id, "time": str(int(time.time())), - "background_credit": settings.config["settings"]["background_credit"] - if "background_credit" in settings.config["settings"] - else "", + "background_credit": credit, "reddit_title": reddit_title, "filename": filename, } diff --git a/video_creation/background.py b/video_creation/background.py index 2654499..9fd68a1 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -1,5 +1,5 @@ import random -from os import listdir, environ +from os import listdir from pathlib import Path from random import randrange from typing import Tuple @@ -55,7 +55,7 @@ def download_background(): print_substep("Background videos downloaded successfully! 🎉", style="bold green") -def chop_background_video(video_length: int): +def chop_background_video(video_length: int) -> str: """Generates the background footage to be used in the video and writes it to assets/temp/background.mp4 Args: @@ -63,7 +63,7 @@ def chop_background_video(video_length: int): """ print_step("Finding a spot in the backgrounds video to chop...✂️") choice = random.choice(listdir("assets/backgrounds")) - environ["background_credit"] = choice.split("-")[0] + credit = choice.split("-")[0] background = VideoFileClip(f"assets/backgrounds/{choice}") @@ -81,3 +81,4 @@ def chop_background_video(video_length: int): new = video.subclip(start_time, end_time) new.write_videofile("assets/temp/background.mp4") print_substep("Background video chopped successfully!", style="bold green") + return credit diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 84698c5..9b85545 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -20,13 +20,13 @@ from rich.console import Console from utils.cleanup import cleanup from utils.console import print_step, print_substep from utils.videos import save_data - +from utils import settings console = Console() W, H = 1080, 1920 -def make_final_video(number_of_clips: int, length: int, reddit_obj: dict): +def make_final_video(number_of_clips: int, length: int, reddit_obj: dict, background_credit: str): """Gathers audio clips, gathers all screenshots, stitches them together and saves the final video to assets/temp Args: @@ -37,7 +37,7 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict): print_step("Creating the final video 🎥") VideoFileClip.reW = lambda clip: clip.resize(width=W) VideoFileClip.reH = lambda clip: clip.resize(width=H) - opacity = os.getenv("OPACITY") + opacity = settings.config["settings"]["opacity"] background_clip = ( VideoFileClip("assets/temp/background.mp4") .without_audio() @@ -91,9 +91,9 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict): title = re.sub(r"[^\w\s-]", "", reddit_obj["thread_title"]) idx = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) filename = f"{title}.mp4" - subreddit = os.getenv("SUBREDDIT") + subreddit = settings.config["reddit"]["thread"]["subreddit"] - save_data(filename, title, idx) + save_data(filename, title, idx, credit) if not exists(f"./results/{subreddit}"): print_substep("The results folder didn't exist so I made it") @@ -118,5 +118,5 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict): print_substep("See result in the results folder!") print_step( - f'Reddit title: {reddit_obj["thread_title"]} \n Background Credit: {os.getenv("background_credit")}' + f'Reddit title: {reddit_obj["thread_title"]} \n Background Credit: {background_credit}' )