From b508f2af7353e15070d64bdaa8b9e2aa303bc2ea Mon Sep 17 00:00:00 2001 From: Kristian Date: Fri, 22 Mar 2024 17:41:40 +0100 Subject: [PATCH] meme support --- TTS/engine_wrapper.py | 2 +- main.py | 52 ++++++++++++++++++++++--- utils/.config.template.toml | 2 + utils/subreddit.py | 6 +-- video_creation/final_video.py | 45 ++++++++++++--------- video_creation/screenshot_downloader.py | 8 +++- 6 files changed, 84 insertions(+), 31 deletions(-) diff --git a/TTS/engine_wrapper.py b/TTS/engine_wrapper.py index 6d498d2..cf5faa9 100644 --- a/TTS/engine_wrapper.py +++ b/TTS/engine_wrapper.py @@ -75,7 +75,7 @@ class TTSEngine: # processed_text = ##self.reddit_object["thread_post"] != "" idx = 0 - if settings.config["settings"]["storymode"]: + if settings.config["settings"]["storymode"] and not settings.config["settings"]["mememode"]: if settings.config["settings"]["storymodemethod"] == 0: if len(self.reddit_object["thread_post"]) > self.tts_module.max_chars: self.split_post(self.reddit_object["thread_post"], "postaudio") diff --git a/main.py b/main.py index 0a401c3..f01fa94 100755 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import math import sys +import os from os import name from pathlib import Path from subprocess import Popen @@ -26,6 +27,8 @@ from video_creation.final_video import make_final_video from video_creation.screenshot_downloader import get_screenshots_of_reddit_posts from video_creation.voices import save_text_to_mp3 +from moviepy.editor import VideoFileClip, concatenate_videoclips + __VERSION__ = "3.2.1" print( @@ -63,13 +66,50 @@ def main(POST_ID=None) -> None: def run_many(times) -> None: - for x in range(1, times + 1): - print_step( - f'on the {x}{("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th")[x % 10]} iteration of {times}' - ) # correct 1st 2nd 3rd 4th 5th.... - main() - Popen("cls" if name == "nt" else "clear", shell=True).wait() + if not settings.config["settings"]["mememode"]: + for x in range(1, times + 1): + print_step( + f'on the {x}{("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th")[x % 10]} iteration of {times}' + ) # correct 1st 2nd 3rd 4th 5th.... + main() + Popen("cls" if name == "nt" else "clear", shell=True).wait() + else: + for x in range(1, times + 1): + print_step( + f'on the {x}{("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th")[x % 10]} iteration of {times}' + ) # correct 1st 2nd 3rd 4th 5th.... + main() + Popen("cls" if name == "nt" else "clear", shell=True).wait() + + make_meme_video() + +def make_meme_video(): + if not os.path.exists("./clipped"): + os.mkdir("./clipped") + directory = f'./results/{settings.config["reddit"]["thread"]["subreddit"]}' + + print(directory) + + # Get a list of all MP4 files in the directory + mp4_files = [f for f in os.listdir(directory) if f.endswith('.mp4')] + + # Create a list of VideoFileClip objects + clips = [VideoFileClip(os.path.join(directory, f)) for f in mp4_files] + + # Concatenate the clips into a single video + final_clip = concatenate_videoclips(clips) + + # Write the final video to a file + output_file = './clipped/output.mp4' + final_clip.write_videofile(output_file) + + # Close the video clips + for clip in clips: + clip.close() + # Delete the individual MP4 files + for f in mp4_files: + os.remove(os.path.join(directory, f)) def shutdown() -> NoReturn: if "redditid" in globals(): diff --git a/utils/.config.template.toml b/utils/.config.template.toml index 211cc97..0f6165e 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -25,6 +25,8 @@ theme = { optional = false, default = "dark", example = "light", options = ["dar times_to_run = { optional = false, default = 1, example = 2, explanation = "Used if you want to run multiple times. Set to an int e.g. 4 or 29 or 1", type = "int", nmin = 1, oob_error = "It's very hard to run something less than once." } opacity = { optional = false, default = 0.9, example = 0.8, explanation = "Sets the opacity of the comments when overlayed over the background", type = "float", nmin = 0, nmax = 1, oob_error = "The opacity HAS to be between 0 and 1", input_error = "The opacity HAS to be a decimal number between 0 and 1" } #transition = { optional = true, default = 0.2, example = 0.2, explanation = "Sets the transition time (in seconds) between the comments. Set to 0 if you want to disable it.", type = "float", nmin = 0, nmax = 2, oob_error = "The transition HAS to be between 0 and 2", input_error = "The opacity HAS to be a decimal number between 0 and 2" } +mememode = { optional = true, type = "bool", default = false, example = false, options = [true, false,], explanation = "Only show post content, and multipile of them (no comments)" } +memes_per_vid = { optional = true, default = 10, example = 10, explanation = "Number of memes per video", type = "int", nmin = 1, oob_error = "It's very hard to run something less than once." } storymode = { optional = true, type = "bool", default = false, example = false, options = [true, false,], explanation = "Only read out title and post content, great for subreddits with stories" } storymodemethod= { optional = true, default = 1, example = 1, explanation = "Style that's used for the storymode. Set to 0 for single picture display in whole video, set to 1 for fancy looking video ", type = "int", nmin = 0, oob_error = "It's very hard to run something less than once.", options = [0, 1] } storymode_max_length = { optional = true, default = 1000, example = 1000, explanation = "Max length of the storymode video in characters. 200 characters are approximately 50 seconds.", type = "int", nmin = 1, oob_error = "It's very hard to make a video under a second." } diff --git a/utils/subreddit.py b/utils/subreddit.py index 403b6d3..c5818d2 100644 --- a/utils/subreddit.py +++ b/utils/subreddit.py @@ -50,7 +50,7 @@ def get_subreddit_undone(submissions: list, subreddit, times_checked=0, similari f'This post has under the specified minimum of comments ({settings.config["reddit"]["thread"]["min_comments"]}). Skipping...' ) continue - if settings.config["settings"]["storymode"]: + if settings.config["settings"]["storymode"] and not settings.config["settings"]["mememode"]: if not submission.selftext: print_substep("You are trying to use story mode on post with no post text") continue @@ -63,9 +63,9 @@ def get_subreddit_undone(submissions: list, subreddit, times_checked=0, similari f"Post is too long ({len(submission.selftext)}), try with a different post. ({settings.config['settings']['storymode_max_length']} character limit)" ) continue - elif len(submission.selftext) < 30: + elif len(submission.selftext) < 30: continue - if settings.config["settings"]["storymode"] and not submission.is_self: + if settings.config["settings"]["storymode"] and not submission.is_self and not settings.config["settings"]["mememode"]: continue if similarity_scores is not None: return submission, similarity_scores[i].item() diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 5069474..48404eb 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -99,7 +99,7 @@ def prepare_background(reddit_id: str, W: int, H: int) -> str: .overwrite_output() ) try: - output.run(quiet=True) + output.run(quiet=False) except ffmpeg.Error as e: print(e.stderr.decode("utf8")) exit(1) @@ -166,12 +166,13 @@ def make_final_video( if settings.config["settings"]["storymode"]: if settings.config["settings"]["storymodemethod"] == 0: audio_clips = [ffmpeg.input(f"assets/temp/{reddit_id}/mp3/title.mp3")] - audio_clips.insert(1, ffmpeg.input(f"assets/temp/{reddit_id}/mp3/postaudio.mp3")) + #audio_clips.insert(1, ffmpeg.input(f"assets/temp/{reddit_id}/mp3/postaudio.mp3")) elif settings.config["settings"]["storymodemethod"] == 1: - audio_clips = [ - ffmpeg.input(f"assets/temp/{reddit_id}/mp3/postaudio-{i}.mp3") - for i in track(range(number_of_clips + 1), "Collecting the audio files...") - ] + if not settings.config["settings"]["mememode"]: + audio_clips = [ + ffmpeg.input(f"assets/temp/{reddit_id}/mp3/postaudio-{i}.mp3") + for i in track(range(number_of_clips + 1), "Collecting the audio files...") + ] audio_clips.insert(0, ffmpeg.input(f"assets/temp/{reddit_id}/mp3/title.mp3")) else: @@ -191,7 +192,7 @@ def make_final_video( audio_concat = ffmpeg.concat(*audio_clips, a=1, v=0) ffmpeg.output( audio_concat, f"assets/temp/{reddit_id}/audio.mp3", **{"b:a": "192k"} - ).overwrite_output().run(quiet=True) + ).overwrite_output().run(quiet=False) console.log(f"[bold green] Video Will Be: {length} Seconds Long") @@ -204,18 +205,20 @@ def make_final_video( image_clips.insert( 0, ffmpeg.input(f"assets/temp/{reddit_id}/png/title.png")["v"].filter( - "scale", screenshot_width, -1 + "scale", screenshot_width, -1, ), ) current_time = 0 if settings.config["settings"]["storymode"]: - audio_clips_durations = [ - float( - ffmpeg.probe(f"assets/temp/{reddit_id}/mp3/postaudio-{i}.mp3")["format"]["duration"] - ) - for i in range(number_of_clips) - ] + audio_clips_durations = [] + if not settings.config["settings"]["mememode"]: + audio_clips_durations = [ + float( + ffmpeg.probe(f"assets/temp/{reddit_id}/mp3/postaudio-{i}.mp3")["format"]["duration"] + ) + for i in range(number_of_clips) + ] audio_clips_durations.insert( 0, float(ffmpeg.probe(f"assets/temp/{reddit_id}/mp3/title.mp3")["format"]["duration"]), @@ -227,6 +230,8 @@ def make_final_video( "scale", screenshot_width, -1 ), ) + if settings.config["settings"]["mememode"]: audio_clips_durations[0] += 2 + background_clip = background_clip.overlay( image_clips[0], enable=f"between(t,{current_time},{current_time + audio_clips_durations[0]})", @@ -234,7 +239,7 @@ def make_final_video( y="(main_h-overlay_h)/2", ) current_time += audio_clips_durations[0] - elif settings.config["settings"]["storymodemethod"] == 1: + elif settings.config["settings"]["storymodemethod"] == 1 and not settings.config["settings"]["mememode"]: for i in track(range(0, number_of_clips + 1), "Collecting the image files..."): image_clips.append( ffmpeg.input(f"assets/temp/{reddit_id}/png/img{i}.png")["v"].filter( @@ -248,6 +253,8 @@ def make_final_video( y="(main_h-overlay_h)/2", ) current_time += audio_clips_durations[i] + elif settings.config["settings"]["mememode"]: + pass else: for i in range(0, number_of_clips + 1): image_clips.append( @@ -354,10 +361,10 @@ def make_final_video( "threads": multiprocessing.cpu_count(), }, ).overwrite_output().global_args("-progress", progress.output_file.name).run( - quiet=True, + quiet=False, overwrite_output=True, capture_stdout=False, - capture_stderr=False, + capture_stderr=True, ) except ffmpeg.Error as e: print(e.stderr.decode("utf8")) @@ -384,10 +391,10 @@ def make_final_video( "threads": multiprocessing.cpu_count(), }, ).overwrite_output().global_args("-progress", progress.output_file.name).run( - quiet=True, + quiet=False, overwrite_output=True, capture_stdout=False, - capture_stderr=False, + capture_stderr=True, ) except ffmpeg.Error as e: print(e.stderr.decode("utf8")) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index d688caf..e5a83ab 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -28,6 +28,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): H: Final[int] = int(settings.config["settings"]["resolution_h"]) lang: Final[str] = settings.config["reddit"]["thread"]["post_lang"] storymode: Final[bool] = settings.config["settings"]["storymode"] + mememode: Final[bool] = settings.config["settings"]["mememode"] print_step("Downloading screenshots of reddit posts...") reddit_id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"]) @@ -168,6 +169,8 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): print_substep("Skipping translation...") postcontentpath = f"assets/temp/{reddit_id}/png/title.png" + + try: if settings.config["settings"]["zoom"] != 1: # store zoom settings @@ -181,6 +184,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): page.screenshot(clip=location, path=postcontentpath) else: page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath) + except Exception as e: print_substep("Something went wrong!", style="red") resp = input( @@ -200,11 +204,11 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): raise e - if storymode: + if storymode and not mememode: page.locator('[data-click-id="text"]').first.screenshot( path=f"assets/temp/{reddit_id}/png/story_content.png" ) - else: + elif not mememode: for idx, comment in enumerate( track( reddit_object["comments"][:screenshot_num],