|
|
@ -1,8 +1,11 @@
|
|
|
|
from __future__ import annotations
|
|
|
|
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 moviepy.video.io.VideoFileClip import VideoFileClip
|
|
|
|
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
|
|
|
|
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Video:
|
|
|
|
class Video:
|
|
|
|
def __init__(self, video: VideoClip | VideoFileClip, *args, **kwargs):
|
|
|
|
def __init__(self, video: VideoClip | VideoFileClip, *args, **kwargs):
|
|
|
@ -11,45 +14,14 @@ class Video:
|
|
|
|
self.duration = self.video.duration
|
|
|
|
self.duration = self.video.duration
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def _create_watermark(text, path, fontsize=15, opacity=0.5):
|
|
|
|
def _create_watermark(text, fontsize, opacity=0.5):
|
|
|
|
|
|
|
|
txt_clip = TextClip(text, fontsize=fontsize, color='black').set_opacity(opacity)
|
|
|
|
width = 500
|
|
|
|
return txt_clip
|
|
|
|
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)
|
|
|
|
def add_watermark(self, text, opacity=0.5, position: Tuple = (0.95, 0.95), fontsize=15):
|
|
|
|
w, h = draw.textsize(text, font)
|
|
|
|
txt_clip = self._create_watermark(text, opacity=opacity, fontsize=fontsize)
|
|
|
|
draw.text(((width - w) / 2, (height - h) / 2), text, white, font)
|
|
|
|
txt_clip = txt_clip.set_pos(position).set_duration(10)
|
|
|
|
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):
|
|
|
|
# Overlay the text clip on the first video clip
|
|
|
|
# add a watermark to the video clip with the given text and opacity without importing a new library
|
|
|
|
self.video = CompositeVideoClip([self.video, txt_clip])
|
|
|
|
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
|
|
|
|
return self.video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': # todo delete
|
|
|
|
|
|
|
|
Video._create_watermark('Background Video by Jason(example)', '../assets/temp/png/watermark.png')
|
|
|
|
|
|
|
|