From 7dcac10d1ef1b6e1c6379545a8a1a3d4d7e54463 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 12 Jul 2022 13:11:33 -0400 Subject: [PATCH] backup but I will be reverting this. --- utils/video.py | 54 ++++++++++++-------------------------------------- 1 file changed, 13 insertions(+), 41 deletions(-) diff --git a/utils/video.py b/utils/video.py index f49e58f..2cceae1 100644 --- a/utils/video.py +++ b/utils/video.py @@ -1,8 +1,11 @@ from __future__ import annotations -from moviepy.video.VideoClip import VideoClip, ImageClip +from typing import Tuple + +from moviepy.video.VideoClip import VideoClip, TextClip from moviepy.video.io.VideoFileClip import VideoFileClip -from PIL import Image, ImageDraw, ImageFont, ImageEnhance +from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip + class Video: def __init__(self, video: VideoClip | VideoFileClip, *args, **kwargs): @@ -11,45 +14,14 @@ class Video: 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. + def _create_watermark(text, fontsize, opacity=0.5): + txt_clip = TextClip(text, fontsize=fontsize, color='black').set_opacity(opacity) + return txt_clip - 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, 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) - 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]) + # Overlay the text clip on the first video clip + self.video = CompositeVideoClip([self.video, txt_clip]) return self.video - - - - -if __name__ == '__main__': # todo delete - Video._create_watermark('Background Video by Jason(example)', '../assets/temp/png/watermark.png')