diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 24ee6d5..ce7d661 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -1,8 +1,9 @@ -import multiprocessing import os import re +import multiprocessing from os.path import exists from typing import Tuple, Any, Final +import translators as ts from moviepy.audio.AudioClip import concatenate_audioclips, CompositeAudioClip from moviepy.audio.io.AudioFileClip import AudioFileClip @@ -29,23 +30,19 @@ def name_normalize(name: str) -> str: name = re.sub(r"(\d+)\s?\/\s?(\d+)", r"\1 of \2", name) name = re.sub(r"(\w+)\s?\/\s?(\w+)", r"\1 or \2", name) name = re.sub(r"\/", r"", name) - name[:30] lang = settings.config["reddit"]["thread"]["post_lang"] if lang: - import translators as ts - print_substep("Translating filename...") translated_name = ts.google(name, to_language=lang) return translated_name - else: - return name + return name -def prepare_background(id: str, W: int, H: int) -> VideoFileClip: +def prepare_background(reddit_id: str, W: int, H: int) -> VideoFileClip: clip = ( - VideoFileClip(f"assets/temp/{id}/background.mp4") + VideoFileClip(f"assets/temp/{reddit_id}/background.mp4") .without_audio() .resize(height=H) ) @@ -84,7 +81,7 @@ def make_final_video( # print('No background audio volume found in config.toml. Using default value of 1.') # VOLUME_MULTIPLIER = 1 - id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) + reddit_id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) print_step("Creating the final video 🎥") VideoFileClip.reW = lambda clip: clip.resize(width=W) @@ -93,13 +90,14 @@ def make_final_video( opacity = settings.config["settings"]["opacity"] transition = settings.config["settings"]["transition"] - background_clip = prepare_background(id, W=W, H=H) + background_clip = prepare_background(reddit_id, W=W, H=H) # Gather all audio clips audio_clips = [ - AudioFileClip(f"assets/temp/{id}/mp3/{i}.mp3") for i in range(number_of_clips) + AudioFileClip(f"assets/temp/{reddit_id}/mp3/{i}.mp3") + for i in range(number_of_clips) ] - audio_clips.insert(0, AudioFileClip(f"assets/temp/{id}/mp3/title.mp3")) + audio_clips.insert(0, AudioFileClip(f"assets/temp/{reddit_id}/mp3/title.mp3")) audio_concat = concatenate_audioclips(audio_clips) audio_composite = CompositeAudioClip([audio_concat]) @@ -114,7 +112,7 @@ def make_final_video( screenshow_width = int((W * 90) // 100) image_clips.insert( 0, - ImageClip(f"assets/temp/{id}/png/title.png") + ImageClip(f"assets/temp/{reddit_id}/png/title.png") .set_duration(audio_clips[0].duration) .resize(width=screenshow_width) .set_opacity(new_opacity) @@ -124,7 +122,7 @@ def make_final_video( for i in range(0, number_of_clips): image_clips.append( - ImageClip(f"assets/temp/{id}/png/comment_{i}.png") + ImageClip(f"assets/temp/{reddit_id}/png/comment_{i}.png") .set_duration(audio_clips[i + 1].duration) .resize(width=screenshow_width) .set_opacity(new_opacity) @@ -173,7 +171,7 @@ def make_final_video( redditid=reddit_obj, ) final.write_videofile( - f"assets/temp/{id}/temp.mp4", + f"assets/temp/{reddit_id}/temp.mp4", fps=30, audio_codec="aac", audio_bitrate="192k", @@ -181,14 +179,14 @@ def make_final_video( threads=multiprocessing.cpu_count(), ) ffmpeg_extract_subclip( - f"assets/temp/{id}/temp.mp4", + f"assets/temp/{reddit_id}/temp.mp4", 0, length, targetname=f"results/{subreddit}/{filename}", ) save_data(subreddit, filename, title, idx, background_config[2]) print_step("Removing temporary files 🗑") - cleanups = cleanup(id) + cleanups = cleanup(reddit_id) print_substep(f"Removed {cleanups} temporary files 🗑") print_substep("See result in the results folder!") diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index ea655a0..a92bca1 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -28,9 +28,9 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in storymode: Final[bool] = settings.config["settings"]["storymode"] print_step("Downloading screenshots of reddit posts...") - id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"]) + reddit_id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"]) # ! Make sure the reddit screenshots folder exists - Path(f"assets/temp/{id}/png").mkdir(parents=True, exist_ok=True) + Path(f"assets/temp/{reddit_id}/png").mkdir(parents=True, exist_ok=True) with sync_playwright() as p: print_substep("Launching Headless Browser...") @@ -59,6 +59,8 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in "./video_creation/data/cookie-light-mode.json", encoding="utf-8" ) cookies = json.load(cookie_file) + cookie_file.close() + context.add_cookies(cookies) # load preference cookies # Get the thread screenshot @@ -94,12 +96,12 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in else: print_substep("Skipping translation...") - postcontentpath = f"assets/temp/{id}/png/title.png" + postcontentpath = f"assets/temp/{reddit_id}/png/title.png" page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath) if storymode: page.locator('[data-click-id="text"]').screenshot( - path=f"assets/temp/{id}/png/story_content.png" + path=f"assets/temp/{reddit_id}/png/story_content.png" ) else: for idx, comment in enumerate( @@ -130,7 +132,7 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in ) try: page.locator(f"#t1_{comment['comment_id']}").screenshot( - path=f"assets/temp/{id}/png/comment_{idx}.png" + path=f"assets/temp/{reddit_id}/png/comment_{idx}.png" ) except TimeoutError: del reddit_object["comments"]