From 637e4c66f28da2dec6feec29f16e36774b329962 Mon Sep 17 00:00:00 2001 From: liamb Date: Sat, 15 Apr 2023 17:20:52 +1000 Subject: [PATCH 1/3] adds zoom function --- utils/.config.template.toml | 1 + video_creation/screenshot_downloader.py | 34 ++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/utils/.config.template.toml b/utils/.config.template.toml index b2fa1d4..e6fa558 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -31,6 +31,7 @@ storymodemethod= { optional = true, default = 1, example = 1, explanation = "Sty 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." } resolution_w = { optional = false, default = 1080, example = 1440, explantation = "Sets the width in pixels of the final video" } resolution_h = { optional = false, default = 1920, example = 2560, explantation = "Sets the height in pixels of the final video" } +zoom = { optional = true, default = 1, example = 1.1, explanation = "Sets the browser zoom level. Useful if you want the text larger.", type = "float", nmin = 0.1, nmax = 2, oob_error = "The text is really difficult to read at a zoom level higher than 2" } [settings.background] background_choice = { optional = true, default = "minecraft", example = "rocket-league", options = ["minecraft", "gta", "rocket-league", "motor-gta", "csgo-surf", "cluster-truck", "minecraft-2","multiversus","fall-guys","steep", ""], explanation = "Sets the background for the video based on game name" } diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 1c3ab5e..ecff2f4 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -158,9 +158,20 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): postcontentpath = f"assets/temp/{reddit_id}/png/title.png" try: - page.locator('[data-test-id="post-content"]').screenshot( - path=postcontentpath - ) + if(settings.config["settings"]["zoom"] != 1): + # store zoom settings + zoom = settings.config["settings"]["zoom"] + # zoom the body of the page + page.evaluate("document.body.style.zoom="+str(zoom)) + # as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom + location = page.locator('[data-test-id="post-content"]').bounding_box() + for i in location: + location[i] = float("{:.2f}".format(location[i]*zoom)) + 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( @@ -214,9 +225,20 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): [comment_tl, comment["comment_id"]], ) try: - page.locator(f"#t1_{comment['comment_id']}").screenshot( - path=f"assets/temp/{reddit_id}/png/comment_{idx}.png" - ) + if(settings.config["settings"]["zoom"] != 1): + # store zoom settings + zoom = settings.config["settings"]["zoom"] + # zoom the body of the page + page.evaluate("document.body.style.zoom="+str(zoom)) + # as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom + location = page.locator(f"#t1_{comment['comment_id']}").bounding_box() + for i in location: + location[i] = float("{:.2f}".format(location[i]*zoom)) + page.screenshot(clip=location, path=f"assets/temp/{reddit_id}/png/comment_{idx}.png") + else: + page.locator(f"#t1_{comment['comment_id']}").screenshot( + path=f"assets/temp/{reddit_id}/png/comment_{idx}.png" + ) except TimeoutError: del reddit_object["comments"] screenshot_num += 1 From 07beeafa50f19ba699da53cae5407c6803562777 Mon Sep 17 00:00:00 2001 From: liamb13 <69778531+liamb13@users.noreply.github.com> Date: Mon, 22 May 2023 13:16:48 +1000 Subject: [PATCH 2/3] Update screenshot_downloader.py Scrolls comment into view. Without, it can break --- video_creation/screenshot_downloader.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index ecff2f4..636a833 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -230,6 +230,8 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): zoom = settings.config["settings"]["zoom"] # zoom the body of the page page.evaluate("document.body.style.zoom="+str(zoom)) + # scroll comment into view + page.locator(f"#t1_{comment['comment_id']}").scroll_into_view_if_needed() # as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom location = page.locator(f"#t1_{comment['comment_id']}").bounding_box() for i in location: From 655c5070105ed87aac204f211fbb77acf4fedb47 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 28 May 2023 13:17:22 +0200 Subject: [PATCH 3/3] Couple of fixes including translators fix --- TTS/engine_wrapper.py | 7 ++----- video_creation/final_video.py | 9 ++++----- video_creation/screenshot_downloader.py | 10 +++++----- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/TTS/engine_wrapper.py b/TTS/engine_wrapper.py index e6b92d8..8157032 100644 --- a/TTS/engine_wrapper.py +++ b/TTS/engine_wrapper.py @@ -3,11 +3,8 @@ import re from pathlib import Path from typing import Tuple -# import sox -# from mutagen import MutagenError -# from mutagen.mp3 import MP3, HeaderNotFoundError import numpy as np -import translators as ts +import translators from moviepy.audio.AudioClip import AudioClip from moviepy.audio.fx.volumex import volumex from moviepy.editor import AudioFileClip @@ -172,6 +169,6 @@ def process_text(text: str, clean: bool = True): new_text = sanitize_text(text) if clean else text if lang: print_substep("Translating Text...") - translated_text = ts.google(text, to_language=lang) + translated_text = translators.google(text, to_language=lang) new_text = sanitize_text(translated_text) return new_text diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 4838574..68ff6aa 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -1,13 +1,12 @@ import multiprocessing import os import re -import shutil from os.path import exists # Needs to be imported specifically from typing import Final from typing import Tuple, Any import ffmpeg -import translators as ts +import translators from PIL import Image from rich.console import Console from rich.progress import track @@ -18,12 +17,12 @@ from utils.console import print_step, print_substep from utils.thumbnail import create_thumbnail from utils.videos import save_data -console = Console() - import tempfile import threading import time +console = Console() + class ProgressFfmpeg(threading.Thread): def __init__(self, vid_duration_seconds, progress_update_callback): @@ -73,7 +72,7 @@ def name_normalize(name: str) -> str: lang = settings.config["reddit"]["thread"]["post_lang"] if lang: print_substep("Translating filename...") - translated_name = ts.google(name, to_language=lang) + translated_name = translators.google(name, to_language=lang) return translated_name else: return name diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 636a833..fe9a127 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -3,7 +3,7 @@ import re from pathlib import Path from typing import Dict, Final -import translators as ts +import translators from playwright.async_api import async_playwright # pylint: disable=unused-import from playwright.sync_api import ViewportSize, sync_playwright from rich.progress import track @@ -144,7 +144,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): if lang: print_substep("Translating post...") - texts_in_tl = ts.google( + texts_in_tl = translators.google( reddit_object["thread_title"], to_language=lang, ) @@ -158,7 +158,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): postcontentpath = f"assets/temp/{reddit_id}/png/title.png" try: - if(settings.config["settings"]["zoom"] != 1): + if settings.config["settings"]["zoom"] != 1: # store zoom settings zoom = settings.config["settings"]["zoom"] # zoom the body of the page @@ -216,7 +216,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): # translate code if settings.config["reddit"]["thread"]["post_lang"]: - comment_tl = ts.google( + comment_tl = translators.google( comment["comment_body"], to_language=settings.config["reddit"]["thread"]["post_lang"], ) @@ -225,7 +225,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): [comment_tl, comment["comment_id"]], ) try: - if(settings.config["settings"]["zoom"] != 1): + if settings.config["settings"]["zoom"] != 1: # store zoom settings zoom = settings.config["settings"]["zoom"] # zoom the body of the page