Watermark implementation
THIS IS IN BETA
This commit is contained in:
1 parent
900816535b
commit
1a4edfafca
4 files changed
+77
-44
No files matched your search
@@ -16,8 +16,8 @@ from video_creation.final_video import make_final_video
|
||||
from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts
|
||||
from video_creation.voices import save_text_to_mp3
|
||||
|
||||
__VERSION__ = "2.3"
|
||||
__BRANCH__ = "master"
|
||||
__VERSION__ = "2.3.1"
|
||||
__BRANCH__ = "develop"
|
||||
|
||||
print(
|
||||
"""
|
||||
|
||||
@@ -9,3 +9,5 @@ requests==2.28.1
|
||||
rich==12.5.1
|
||||
toml==0.10.2
|
||||
translators==5.3.1
|
||||
|
||||
Pillow~=9.1.1
|
||||
@@ -0,0 +1,55 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from moviepy.video.VideoClip import VideoClip, ImageClip
|
||||
from moviepy.video.io.VideoFileClip import VideoFileClip
|
||||
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
|
||||
|
||||
class Video:
|
||||
def __init__(self, video: VideoClip | VideoFileClip, *args, **kwargs):
|
||||
self.video: VideoClip = video
|
||||
self.fps = self.video.fps
|
||||
self.duration = self.video.duration
|
||||
|
||||
@staticmethod
|
||||
def _create_watermark(text, path, fontsize=15, opacity=0.5):
|
||||
|
||||
width = 500
|
||||
height = 200
|
||||
|
||||
white = (255, 255, 255)
|
||||
transparent = (0, 0, 0, 0)
|
||||
|
||||
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 alow it to use the fontsize
|
||||
mask = en.enhance(1 - opacity)
|
||||
im.paste(wm, (25, 25), mask)
|
||||
im.save(path)
|
||||
|
||||
def add_watermark(self, text, opacity=0.5):
|
||||
# add a watermark to the video clip with the given text and opacity without importing a new library
|
||||
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
|
||||
from moviepy.video.compositing.concatenate import concatenate_videoclips
|
||||
path = './assets/temp/png/watermark.png'
|
||||
self._create_watermark(text, path, opacity=opacity)
|
||||
image_clips = []
|
||||
image_clips.insert(
|
||||
0,
|
||||
ImageClip(path)
|
||||
.set_duration(self.video.duration)
|
||||
#.resize(width=W - 100)
|
||||
)
|
||||
image_concat = concatenate_videoclips(image_clips).set_position((0.1, 0.1))
|
||||
self.video = CompositeVideoClip([self.video, image_concat])
|
||||
return self.video
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__': # todo delete
|
||||
Video._create_watermark('Background Video by Jason(example)', '../assets/temp/png/watermark.png')
|
||||
@@ -15,6 +15,7 @@ from rich.console import Console
|
||||
|
||||
from utils.cleanup import cleanup
|
||||
from utils.console import print_step, print_substep
|
||||
from utils.video import Video
|
||||
from utils.videos import save_data
|
||||
from utils import settings
|
||||
|
||||
@@ -42,12 +43,8 @@ def name_normalize(name: str) -> str:
|
||||
return name
|
||||
|
||||
|
||||
def make_final_video(
|
||||
number_of_clips: int,
|
||||
length: int,
|
||||
reddit_obj: dict,
|
||||
background_config: Tuple[str, str, str, Any],
|
||||
):
|
||||
def make_final_video(number_of_clips: int, length: int, reddit_obj: dict,
|
||||
background_config: Tuple[str, str, str, Any], ):
|
||||
"""Gathers audio clips, gathers all screenshots, stitches them together and saves the final video to assets/temp
|
||||
Args:
|
||||
number_of_clips (int): Index to end at when going through the screenshots'
|
||||
@@ -65,11 +62,8 @@ def make_final_video(
|
||||
VideoFileClip.reH = lambda clip: clip.resize(width=H)
|
||||
opacity = settings.config["settings"]["opacity"]
|
||||
background_clip = (
|
||||
VideoFileClip("assets/temp/background.mp4")
|
||||
.without_audio()
|
||||
.resize(height=H)
|
||||
.crop(x1=1166.6, y1=0, x2=2246.6, y2=1920)
|
||||
)
|
||||
VideoFileClip("assets/temp/background.mp4").without_audio().resize(height=H).crop(x1=1166.6, y1=0, x2=2246.6,
|
||||
y2=1920))
|
||||
|
||||
# Gather all audio clips
|
||||
audio_clips = [AudioFileClip(f"assets/temp/mp3/{i}.mp3") for i in range(number_of_clips)]
|
||||
@@ -82,21 +76,15 @@ def make_final_video(
|
||||
image_clips = []
|
||||
# Gather all images
|
||||
new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity)
|
||||
image_clips.insert(
|
||||
0,
|
||||
ImageClip("assets/temp/png/title.png")
|
||||
.set_duration(audio_clips[0].duration)
|
||||
.resize(width=W - 100)
|
||||
.set_opacity(new_opacity),
|
||||
)
|
||||
image_clips.insert(0,
|
||||
ImageClip("assets/temp/png/title.png").set_duration(audio_clips[0].duration).resize(
|
||||
width=W - 100).set_opacity(
|
||||
new_opacity), )
|
||||
|
||||
for i in range(0, number_of_clips):
|
||||
image_clips.append(
|
||||
ImageClip(f"assets/temp/png/comment_{i}.png")
|
||||
.set_duration(audio_clips[i + 1].duration)
|
||||
.resize(width=W - 100)
|
||||
.set_opacity(new_opacity)
|
||||
)
|
||||
ImageClip(f"assets/temp/png/comment_{i}.png").set_duration(audio_clips[i + 1].duration).resize(
|
||||
width=W - 100).set_opacity(new_opacity))
|
||||
|
||||
# if os.path.exists("assets/mp3/posttext.mp3"):
|
||||
# image_clips.insert(
|
||||
@@ -109,7 +97,8 @@ def make_final_video(
|
||||
# )
|
||||
# else: story mode stuff
|
||||
img_clip_pos = background_config[3]
|
||||
image_concat = concatenate_videoclips(image_clips).set_position(img_clip_pos)
|
||||
image_concat = concatenate_videoclips(image_clips).set_position(
|
||||
img_clip_pos) # note transition kwarg for delay in imgs
|
||||
image_concat.audio = audio_composite
|
||||
final = CompositeVideoClip([background_clip, image_concat])
|
||||
title = re.sub(r"[^\w\s-]", "", reddit_obj["thread_title"])
|
||||
@@ -129,27 +118,14 @@ def make_final_video(
|
||||
# # lowered_audio = audio_background.multiply_volume( # todo get this to work
|
||||
# # VOLUME_MULTIPLIER) # lower volume by background_audio_volume, use with fx
|
||||
# final.set_audio(final_audio)
|
||||
|
||||
final.write_videofile(
|
||||
"assets/temp/temp.mp4",
|
||||
fps=30,
|
||||
audio_codec="aac",
|
||||
audio_bitrate="192k",
|
||||
verbose=False,
|
||||
threads=multiprocessing.cpu_count(),
|
||||
)
|
||||
ffmpeg_extract_subclip(
|
||||
"assets/temp/temp.mp4",
|
||||
0,
|
||||
final.duration,
|
||||
targetname=f"results/{subreddit}/{filename}",
|
||||
)
|
||||
final = Video(final).add_watermark(text=f'Background credit: {background_config[2]}', opacity=0.4)
|
||||
final.write_videofile("assets/temp/temp.mp4", fps=30, audio_codec="aac", audio_bitrate="192k", verbose=False,
|
||||
threads=multiprocessing.cpu_count(), )
|
||||
ffmpeg_extract_subclip("assets/temp/temp.mp4", 0, final.duration, targetname=f"results/{subreddit}/{filename}", )
|
||||
save_data(subreddit, filename, title, idx, background_config[2])
|
||||
print_step("Removing temporary files 🗑")
|
||||
cleanups = cleanup()
|
||||
print_substep(f"Removed {cleanups} temporary files 🗑")
|
||||
print_substep("See result in the results folder!")
|
||||
|
||||
print_step(
|
||||
f'Reddit title: {reddit_obj["thread_title"]} \n Background Credit: {background_config[2]}'
|
||||
)
|
||||
print_step(f'Reddit title: {reddit_obj["thread_title"]} \n Background Credit: {background_config[2]}')
|
||||
Reference in new issue
Block a user