diff --git a/TTS/engine_wrapper.py b/TTS/engine_wrapper.py index 213defb..93da092 100644 --- a/TTS/engine_wrapper.py +++ b/TTS/engine_wrapper.py @@ -13,7 +13,7 @@ from utils.console import print_step, print_substep from utils.voice import sanitize_text from utils import settings -DEFAULT_MAX_LENGTH: int = 50 # video length variable +DEFAULT_MAX_LENGTH: int = 10 # video length variable class TTSEngine: diff --git a/requirements.txt b/requirements.txt index e57a37d..7bccd0d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,5 @@ requests==2.28.1 rich==12.5.1 toml==0.10.2 translators==5.3.1 + +Pillow~=9.1.1 diff --git a/utils/video.py b/utils/video.py index 2cceae1..61dc41c 100644 --- a/utils/video.py +++ b/utils/video.py @@ -2,26 +2,44 @@ from __future__ import annotations from typing import Tuple -from moviepy.video.VideoClip import VideoClip, TextClip -from moviepy.video.io.VideoFileClip import VideoFileClip +from PIL import ImageFont, Image, ImageDraw, ImageEnhance +from moviepy.video.VideoClip import VideoClip, ImageClip from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip class Video: - def __init__(self, video: VideoClip | VideoFileClip, *args, **kwargs): + def __init__(self, video: VideoClip, *args, **kwargs): self.video: VideoClip = video self.fps = self.video.fps self.duration = self.video.duration @staticmethod def _create_watermark(text, fontsize, opacity=0.5): - txt_clip = TextClip(text, fontsize=fontsize, color='black').set_opacity(opacity) - return txt_clip + path = "./assets/temp/png/watermark.png" + width = int(fontsize * len(text)) + height = int(fontsize * len(text) / 2) - def add_watermark(self, text, opacity=0.5, position: Tuple = (0.95, 0.95), fontsize=15): - txt_clip = self._create_watermark(text, opacity=opacity, fontsize=fontsize) - txt_clip = txt_clip.set_pos(position).set_duration(10) + white = (255, 255, 255) + transparent = (0, 0, 0, 0) - # Overlay the text clip on the first video clip - self.video = CompositeVideoClip([self.video, txt_clip]) + font = ImageFont.load_default() + wm = Image.new("RGBA", (width, height), transparent) + im = Image.new("RGBA", (width, height), transparent) # Change this line too. + + draw = ImageDraw.Draw(wm) + w, h = draw.textsize(text, font) + draw.text(((width - w) / 2, (height - h) / 2), text, white, font) + en = ImageEnhance.Brightness(wm) # todo allow it to use the fontsize + mask = en.enhance(1 - opacity) + im.paste(wm, (25, 25), mask) + im.save(path) + return ImageClip(path) + + def add_watermark(self, text, opacity=0.5, duration: int | float = 5, position: Tuple = (1, 100), fontsize=15): + img_clip = self._create_watermark(text, opacity=opacity, fontsize=fontsize) + img_clip = img_clip.set_opacity(opacity).set_duration(duration) + img_clip = img_clip.set_position(("center","bottom")) # set position doesn't work for some reason # todo fix + + # Overlay the img clip on the first video clip + self.video = CompositeVideoClip([self.video, img_clip]) return self.video diff --git a/video_creation/final_video.py b/video_creation/final_video.py index a6d5d2f..3157296 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -30,11 +30,11 @@ 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 diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 6fb9ef4..8010862 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -49,36 +49,29 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in print_substep("Post is NSFW. You are spicy...") page.locator('[data-testid="content-gate"] button').click() - page.locator( - '[data-click-id="text"] button' - ).click() # Remove "Click to see nsfw" Button in Screenshot + + if page.locator('[data-click-id="text"] button').is_visible(): + page.locator('[data-click-id="text"] button').click() # Remove "Click to see nsfw" Button in Screenshot # translate code if settings.config["reddit"]["thread"]["post_lang"]: print_substep("Translating post...") - texts_in_tl = ts.google( - reddit_object["thread_title"], - to_language=settings.config["reddit"]["thread"]["post_lang"], - ) + texts_in_tl = ts.google(reddit_object["thread_title"], + to_language=settings.config["reddit"]["thread"]["post_lang"], ) page.evaluate( "tl_content => document.querySelector('[data-test-id=\"post-content\"] > div:nth-child(3) > div > div').textContent = tl_content", - texts_in_tl, - ) + texts_in_tl, ) else: print_substep("Skipping translation...") page.locator('[data-test-id="post-content"]').screenshot(path="assets/temp/png/title.png") if storymode: - page.locator('[data-click-id="text"]').screenshot( - path="assets/temp/png/story_content.png" - ) + page.locator('[data-click-id="text"]').screenshot(path="assets/temp/png/story_content.png") else: - for idx, comment in enumerate( - track(reddit_object["comments"], "Downloading screenshots...") - ): + for idx, comment in enumerate(track(reddit_object["comments"], "Downloading screenshots...")): # Stop if we have reached the screenshot_num if idx >= screenshot_num: break @@ -91,17 +84,16 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in # translate code if settings.config["reddit"]["thread"]["post_lang"]: - comment_tl = ts.google( - comment["comment_body"], - to_language=settings.config["reddit"]["thread"]["post_lang"], - ) + comment_tl = ts.google(comment["comment_body"], + to_language=settings.config["reddit"]["thread"]["post_lang"], ) page.evaluate( '([tl_content, tl_id]) => document.querySelector(`#t1_${tl_id} > div:nth-child(2) > div > div[data-testid="comment"] > div`).textContent = tl_content', - [comment_tl, comment["comment_id"]], - ) - - page.locator(f"#t1_{comment['comment_id']}").screenshot( - path=f"assets/temp/png/comment_{idx}.png" - ) - + [comment_tl, comment["comment_id"]], ) + try: + page.locator(f"#t1_{comment['comment_id']}").screenshot(path=f"assets/temp/png/comment_{idx}.png") + except TimeoutError: + del reddit_object["comments"] + screenshot_num += 1 + print('TimeoutError: Skipping screenshot...') + continue print_substep("Screenshots downloaded Successfully.", style="bold green")