|
|
|
@ -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
|
|
|
|
|