diff --git a/examples/final_video.mp4 b/examples/final_video.mp4 deleted file mode 100644 index cd19420..0000000 Binary files a/examples/final_video.mp4 and /dev/null differ diff --git a/utils/.config.template.toml b/utils/.config.template.toml index 11e62ec..b2fa1d4 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -22,7 +22,7 @@ ai_similarity_keywords = {optional = true, type="str", example= 'Elon Musk, Twit [settings] allow_nsfw = { optional = false, type = "bool", default = false, example = false, options = [true, false, ], explanation = "Whether to allow NSFW content, True or False" } -theme = { optional = false, default = "dark", example = "light", options = ["dark", "light", ], explanation = "Sets the Reddit theme, either LIGHT or DARK" } +theme = { optional = false, default = "dark", example = "light", options = ["dark", "light", "transparent", ], explanation = "Sets the Reddit theme, either LIGHT or DARK. For story mode you can also use a transparent background." } 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" } diff --git a/utils/imagenarator.py b/utils/imagenarator.py index 8e3789e..63d8df3 100644 --- a/utils/imagenarator.py +++ b/utils/imagenarator.py @@ -6,7 +6,8 @@ from PIL import Image, ImageDraw, ImageFont from rich.progress import track from TTS.engine_wrapper import process_text -def draw_multiple_line_text(image, text, font, text_color, padding, wrap=50) -> None: + +def draw_multiple_line_text(image, text, font, text_color, padding, wrap=50, transparent=False) -> None: """ Draw multiline text over given image """ @@ -14,63 +15,45 @@ def draw_multiple_line_text(image, text, font, text_color, padding, wrap=50) -> Fontperm = font.getsize(text) image_width, image_height = image.size lines = textwrap.wrap(text, width=wrap) - y = (image_height / 2) - ( - ((Fontperm[1] + (len(lines) * padding) / len(lines)) * len(lines)) / 2 - ) + y = (image_height / 2) - (((Fontperm[1] + (len(lines) * padding) / len(lines)) * len(lines)) / 2) for line in lines: line_width, line_height = font.getsize(line) + if transparent: + shadowcolor = "black" + for i in range(1, 5): + draw.text(((image_width - line_width) / 2 - i, y - i), line, font=font, fill=shadowcolor) + draw.text(((image_width - line_width) / 2 + i, y - i), line, font=font, fill=shadowcolor) + draw.text(((image_width - line_width) / 2 - i, y + i), line, font=font, fill=shadowcolor) + draw.text(((image_width - line_width) / 2 + i, y + i), line, font=font, fill=shadowcolor) draw.text(((image_width - line_width) / 2, y), line, font=font, fill=text_color) y += line_height + padding -# theme=bgcolor,reddit_obj=reddit_object,txtclr=txtcolor -def imagemaker(theme, reddit_obj: dict, txtclr, padding=5) -> None: +def imagemaker(theme, reddit_obj: dict, txtclr, padding=5, transparent=False) -> None: """ Render Images for video """ - title = process_text(reddit_obj["thread_title"], False) #TODO if second argument cause any error + title = process_text(reddit_obj["thread_title"], False) # TODO if second argument cause any error texts = reddit_obj["thread_post"] id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) - tfont = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 27) # for title - font = ImageFont.truetype( - os.path.join("fonts", "Roboto-Regular.ttf"), 20 - ) # for despcription|comments - size = (500, 176) + if transparent: + font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 50) + tfont = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 50) + else: + tfont = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 35) # for title + font = ImageFont.truetype(os.path.join("fonts", "Roboto-Regular.ttf"), 30) + size = (1920, 1080) image = Image.new("RGBA", size, theme) - draw = ImageDraw.Draw(image) # for title - if len(title) > 40: - draw_multiple_line_text(image, title, tfont, txtclr, padding, wrap=30) - else: - - Fontperm = tfont.getsize(title) - draw.text( - ((image.size[0] - Fontperm[0]) / 2, (image.size[1] - Fontperm[1]) / 2), - font=tfont, - text=title, - ) # (image.size[1]/2)-(Fontperm[1]/2) + draw_multiple_line_text(image, title, tfont, txtclr, padding, wrap=30, transparent=transparent) image.save(f"assets/temp/{id}/png/title.png") - # for comment|description - - for idx, text in track(enumerate(texts), "Rendering Image"):#, total=len(texts)): - + for idx, text in track(enumerate(texts), "Rendering Image"): image = Image.new("RGBA", size, theme) - draw = ImageDraw.Draw(image) - text = process_text(text,False) - if len(text) > 50: - draw_multiple_line_text(image, text, font, txtclr, padding) - - else: - - Fontperm = font.getsize(text) - draw.text( - ((image.size[0] - Fontperm[0]) / 2, (image.size[1] - Fontperm[1]) / 2), - font=font, - text=text, - ) # (image.size[1]/2)-(Fontperm[1]/2) + text = process_text(text, False) + draw_multiple_line_text(image, text, font, txtclr, padding, wrap=30, transparent=transparent) image.save(f"assets/temp/{id}/png/img{idx}.png") diff --git a/video_creation/final_video.py b/video_creation/final_video.py index e4a150e..ec4ac5c 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -176,7 +176,7 @@ def make_final_video( ): image_clips.append( ffmpeg.input(f"assets/temp/{reddit_id}/png/img{i}.png")['v'] - .filter('scale', screenshot_width, -1) + .filter('scale', 1080, -1) ) background_clip = background_clip.overlay(image_clips[i], enable=f'between(t,{current_time},{current_time + audio_clips_durations[i]})', @@ -235,7 +235,12 @@ def make_final_video( print_substep(f"Thumbnail - Building Thumbnail in assets/temp/{reddit_id}/thumbnail.png") text = f"Background by {background_config[2]}" - + background_clip = ffmpeg.drawtext(background_clip, + text=text, + x=f'(w-text_w)', y=f'(h-text_h)', + fontsize=12, + fontcolor="White", + fontfile=os.path.join("fonts", "Roboto-Regular.ttf")) print_step("Rendering the video 🎥") from tqdm import tqdm pbar = tqdm(total=100, desc="Progress: ", bar_format="{l_bar}{bar}", unit=" %") diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 71bfb70..b04bd61 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -58,15 +58,31 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): ) bgcolor = (33, 33, 36, 255) txtcolor = (240, 240, 240) + transparent = False + elif settings.config["settings"]["theme"] == "transparent": + if storymode: + # Transparent theme + bgcolor = (0, 0, 0, 0) + txtcolor = (255, 255, 255) + transparent = True + else: + # Switch to dark theme + cookie_file = open( + "./video_creation/data/cookie-dark-mode.json", encoding="utf-8" + ) + bgcolor = (33, 33, 36, 255) + txtcolor = (240, 240, 240) + transparent = False else: cookie_file = open( "./video_creation/data/cookie-light-mode.json", encoding="utf-8" ) bgcolor = (255, 255, 255, 255) txtcolor = (0, 0, 0) + transparent = False if storymode and settings.config["settings"]["storymodemethod"] == 1: # for idx,item in enumerate(reddit_object["thread_post"]): - return imagemaker(theme=bgcolor, reddit_obj=reddit_object, txtclr=txtcolor) + return imagemaker(theme=bgcolor, reddit_obj=reddit_object, txtclr=txtcolor, transparent=transparent) cookies = json.load(cookie_file) cookie_file.close()