Skip to content

Commit 7efb51d

Browse files
Merge pull request #984 from elebumm/itwontletmepushsoimmakingthistemp
Itwontletmepushsoimmakingthistemp
2 parents 33d588e + 3c9318d commit 7efb51d

File tree

5 files changed

+50
-38
lines changed

5 files changed

+50
-38
lines changed

TTS/engine_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from utils.voice import sanitize_text
1414
from utils import settings
1515

16-
DEFAULT_MAX_LENGTH: int = 50 # video length variable
16+
DEFAULT_MAX_LENGTH: int = 10 # video length variable
1717

1818

1919
class TTSEngine:

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ requests==2.28.1
99
rich==12.5.1
1010
toml==0.10.2
1111
translators==5.3.1
12+
13+
Pillow~=9.1.1

utils/video.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,44 @@
22

33
from typing import Tuple
44

5-
from moviepy.video.VideoClip import VideoClip, TextClip
6-
from moviepy.video.io.VideoFileClip import VideoFileClip
5+
from PIL import ImageFont, Image, ImageDraw, ImageEnhance
6+
from moviepy.video.VideoClip import VideoClip, ImageClip
77
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
88

99

1010
class Video:
11-
def __init__(self, video: VideoClip | VideoFileClip, *args, **kwargs):
11+
def __init__(self, video: VideoClip, *args, **kwargs):
1212
self.video: VideoClip = video
1313
self.fps = self.video.fps
1414
self.duration = self.video.duration
1515

1616
@staticmethod
1717
def _create_watermark(text, fontsize, opacity=0.5):
18-
txt_clip = TextClip(text, fontsize=fontsize, color='black').set_opacity(opacity)
19-
return txt_clip
18+
path = "./assets/temp/png/watermark.png"
19+
width = int(fontsize * len(text))
20+
height = int(fontsize * len(text) / 2)
2021

21-
def add_watermark(self, text, opacity=0.5, position: Tuple = (0.95, 0.95), fontsize=15):
22-
txt_clip = self._create_watermark(text, opacity=opacity, fontsize=fontsize)
23-
txt_clip = txt_clip.set_pos(position).set_duration(10)
22+
white = (255, 255, 255)
23+
transparent = (0, 0, 0, 0)
2424

25-
# Overlay the text clip on the first video clip
26-
self.video = CompositeVideoClip([self.video, txt_clip])
25+
font = ImageFont.load_default()
26+
wm = Image.new("RGBA", (width, height), transparent)
27+
im = Image.new("RGBA", (width, height), transparent) # Change this line too.
28+
29+
draw = ImageDraw.Draw(wm)
30+
w, h = draw.textsize(text, font)
31+
draw.text(((width - w) / 2, (height - h) / 2), text, white, font)
32+
en = ImageEnhance.Brightness(wm) # todo allow it to use the fontsize
33+
mask = en.enhance(1 - opacity)
34+
im.paste(wm, (25, 25), mask)
35+
im.save(path)
36+
return ImageClip(path)
37+
38+
def add_watermark(self, text, opacity=0.5, duration: int | float = 5, position: Tuple = (1, 100), fontsize=15):
39+
img_clip = self._create_watermark(text, opacity=opacity, fontsize=fontsize)
40+
img_clip = img_clip.set_opacity(opacity).set_duration(duration)
41+
img_clip = img_clip.set_position(("center","bottom")) # set position doesn't work for some reason # todo fix
42+
43+
# Overlay the img clip on the first video clip
44+
self.video = CompositeVideoClip([self.video, img_clip])
2745
return self.video

video_creation/final_video.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ def name_normalize(name: str) -> str:
3030
name = re.sub(r"(\d+)\s?\/\s?(\d+)", r"\1 of \2", name)
3131
name = re.sub(r"(\w+)\s?\/\s?(\w+)", r"\1 or \2", name)
3232
name = re.sub(r"\/", r"", name)
33+
name[:30]
3334

3435
lang = settings.config["reddit"]["thread"]["post_lang"]
3536
if lang:
3637
import translators as ts
37-
3838
print_substep("Translating filename...")
3939
translated_name = ts.google(name, to_language=lang)
4040
return translated_name

video_creation/screenshot_downloader.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,29 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in
4949

5050
print_substep("Post is NSFW. You are spicy...")
5151
page.locator('[data-testid="content-gate"] button').click()
52-
page.locator(
53-
'[data-click-id="text"] button'
54-
).click() # Remove "Click to see nsfw" Button in Screenshot
52+
53+
if page.locator('[data-click-id="text"] button').is_visible():
54+
page.locator('[data-click-id="text"] button').click() # Remove "Click to see nsfw" Button in Screenshot
5555

5656
# translate code
5757

5858
if settings.config["reddit"]["thread"]["post_lang"]:
5959
print_substep("Translating post...")
60-
texts_in_tl = ts.google(
61-
reddit_object["thread_title"],
62-
to_language=settings.config["reddit"]["thread"]["post_lang"],
63-
)
60+
texts_in_tl = ts.google(reddit_object["thread_title"],
61+
to_language=settings.config["reddit"]["thread"]["post_lang"], )
6462

6563
page.evaluate(
6664
"tl_content => document.querySelector('[data-test-id=\"post-content\"] > div:nth-child(3) > div > div').textContent = tl_content",
67-
texts_in_tl,
68-
)
65+
texts_in_tl, )
6966
else:
7067
print_substep("Skipping translation...")
7168

7269
page.locator('[data-test-id="post-content"]').screenshot(path="assets/temp/png/title.png")
7370

7471
if storymode:
75-
page.locator('[data-click-id="text"]').screenshot(
76-
path="assets/temp/png/story_content.png"
77-
)
72+
page.locator('[data-click-id="text"]').screenshot(path="assets/temp/png/story_content.png")
7873
else:
79-
for idx, comment in enumerate(
80-
track(reddit_object["comments"], "Downloading screenshots...")
81-
):
74+
for idx, comment in enumerate(track(reddit_object["comments"], "Downloading screenshots...")):
8275
# Stop if we have reached the screenshot_num
8376
if idx >= screenshot_num:
8477
break
@@ -91,17 +84,16 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in
9184
# translate code
9285

9386
if settings.config["reddit"]["thread"]["post_lang"]:
94-
comment_tl = ts.google(
95-
comment["comment_body"],
96-
to_language=settings.config["reddit"]["thread"]["post_lang"],
97-
)
87+
comment_tl = ts.google(comment["comment_body"],
88+
to_language=settings.config["reddit"]["thread"]["post_lang"], )
9889
page.evaluate(
9990
'([tl_content, tl_id]) => document.querySelector(`#t1_${tl_id} > div:nth-child(2) > div > div[data-testid="comment"] > div`).textContent = tl_content',
100-
[comment_tl, comment["comment_id"]],
101-
)
102-
103-
page.locator(f"#t1_{comment['comment_id']}").screenshot(
104-
path=f"assets/temp/png/comment_{idx}.png"
105-
)
106-
91+
[comment_tl, comment["comment_id"]], )
92+
try:
93+
page.locator(f"#t1_{comment['comment_id']}").screenshot(path=f"assets/temp/png/comment_{idx}.png")
94+
except TimeoutError:
95+
del reddit_object["comments"]
96+
screenshot_num += 1
97+
print('TimeoutError: Skipping screenshot...')
98+
continue
10799
print_substep("Screenshots downloaded Successfully.", style="bold green")

0 commit comments

Comments
 (0)