From f6eb9bad4c1a22d86536780fe0890206a014b70c Mon Sep 17 00:00:00 2001 From: Simon <65854503+StopmotionSimonYT@users.noreply.github.com> Date: Sat, 30 Jul 2022 23:33:23 +0200 Subject: [PATCH] Removed watermark todo and fixed it --- final_video.py | 168 +++++++++++++++++++++++++++++++++++++++++++++++++ video.py | 58 +++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 final_video.py create mode 100644 video.py diff --git a/final_video.py b/final_video.py new file mode 100644 index 0000000..c2d7a18 --- /dev/null +++ b/final_video.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +import multiprocessing +import os +import re +from os.path import exists +from typing import Tuple, Any +from moviepy.audio.AudioClip import concatenate_audioclips, CompositeAudioClip +from moviepy.audio.io.AudioFileClip import AudioFileClip +from moviepy.video.VideoClip import ImageClip +from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip +from moviepy.video.compositing.concatenate import concatenate_videoclips +from moviepy.video.io.VideoFileClip import VideoFileClip +from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip +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 + +console = Console() +W, H = 1080, 1920 + + +def name_normalize(name: str) -> str: + name = re.sub(r'[?\\"%*:|<>]', "", name) + name = re.sub(r"( [w,W]\s?\/\s?[o,O,0])", r" without", name) + name = re.sub(r"( [w,W]\s?\/)", r" with", name) + name = re.sub(r"(\d+)\s?\/\s?(\d+)", r"\1 of \2", name) + name = re.sub(r"(\w+)\s?\/\s?(\w+)", r"\1 or \2", name) + name = re.sub(r"\/", r"", name) + name[:30] + + lang = settings.config["reddit"]["thread"]["post_lang"] + if lang: + import translators as ts + + print_substep("Translating filename...") + translated_name = ts.google(name, to_language=lang) + return translated_name + + else: + return name + + +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' + length (int): Length of the video + reddit_obj (dict): The reddit object that contains the posts to read. + background_config (Tuple[str, str, str, Any]): The background config to use. + """ + # try: # if it isn't found (i.e you just updated and copied over config.toml) it will throw an error + # VOLUME_MULTIPLIER = settings.config["settings"]['background']["background_audio_volume"] + # except (TypeError, KeyError): + # print('No background audio volume found in config.toml. Using default value of 1.') + # VOLUME_MULTIPLIER = 1 + id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) + print_step("Creating the final video 🎥") + VideoFileClip.reW = lambda clip: clip.resize(width=W) + VideoFileClip.reH = lambda clip: clip.resize(width=H) + opacity = settings.config["settings"]["opacity"] + transition = settings.config["settings"]["transition"] + background_clip = ( + VideoFileClip(f"assets/temp/{id}/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/{id}/mp3/{i}.mp3") for i in range(number_of_clips)] + audio_clips.insert(0, AudioFileClip(f"assets/temp/{id}/mp3/title.mp3")) + audio_concat = concatenate_audioclips(audio_clips) + audio_composite = CompositeAudioClip([audio_concat]) + + console.log(f"[bold green] Video Will Be: {length} Seconds Long") + # add title to video + image_clips = [] + # Gather all images + new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity) + new_transition = 0 if transition is None or float(transition) > 2 else float(transition) + image_clips.insert( + 0, + ImageClip(f"assets/temp/{id}/png/title.png") + .set_duration(audio_clips[0].duration) + .resize(width=W - 100) + .set_opacity(new_opacity) + .crossfadein(new_transition) + .crossfadeout(new_transition), + ) + + for i in range(0, number_of_clips): + image_clips.append( + ImageClip(f"assets/temp/{id}/png/comment_{i}.png") + .set_duration(audio_clips[i + 1].duration) + .resize(width=W - 100) + .set_opacity(new_opacity) + .crossfadein(new_transition) + .crossfadeout(new_transition) + ) + + # if os.path.exists("assets/mp3/posttext.mp3"): + # image_clips.insert( + # 0, + # ImageClip("assets/png/title.png") + # .set_duration(audio_clips[0].duration + audio_clips[1].duration) + # .set_position("center") + # .resize(width=W - 100) + # .set_opacity(float(opacity)), + # ) + # else: story mode stuff + img_clip_pos = background_config[3] + 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"]) + idx = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) + + filename = f"{name_normalize(title)}.mp4" + subreddit = settings.config["reddit"]["thread"]["subreddit"] + + if not exists(f"./results/{subreddit}"): + print_substep("The results folder didn't exist so I made it") + os.makedirs(f"./results/{subreddit}") + + # if settings.config["settings"]['background']["background_audio"] and exists(f"assets/backgrounds/background.mp3"): + # audioclip = mpe.AudioFileClip(f"assets/backgrounds/background.mp3").set_duration(final.duration) + # audioclip = audioclip.fx( volumex, 0.2) + # final_audio = mpe.CompositeAudioClip([final.audio, audioclip]) + # # 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 = Video(final).add_watermark( + text=f"Background credit: {background_config[2]}", opacity=0.4, redditid=reddit_obj + ) + final.write_videofile( + f"assets/temp/{id}/temp.mp4", + fps=30, + audio_codec="aac", + audio_bitrate="192k", + verbose=False, + threads=multiprocessing.cpu_count(), + ) + ffmpeg_extract_subclip( + f"assets/temp/{id}/temp.mp4", + 0, + length, + targetname=f"results/{subreddit}/{filename}", + ) + save_data(subreddit, filename, title, idx, background_config[2]) + print_step("Removing temporary files 🗑") + cleanups = cleanup(id) + 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]}' + ) diff --git a/video.py b/video.py new file mode 100644 index 0000000..5566cd1 --- /dev/null +++ b/video.py @@ -0,0 +1,58 @@ +from __future__ import annotations +from ast import Str +import re + +from typing import Tuple + +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, *args, **kwargs): + self.video: VideoClip = video + self.fps = self.video.fps + self.duration = self.video.duration + + @staticmethod + def _create_watermark(text, redditid, fontsize, opacity=0.5): + id = re.sub(r"[^\w\s-]", "", redditid["thread_id"]) + path = f"./assets/temp/{id}/png/watermark.png" + width = int(fontsize * len(text)) + height = int(fontsize * len(text) / 2) + 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 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, redditid, opacity=0.5, duration: int | float = 5, position: Tuple = (0.7, 0.9), fontsize=15 + ): + compensation = round( + (position[0] / ((len(text) * (fontsize / 5) / 1.5) / 100 + position[0] * position[0])), + ndigits=2, + ) + position = (compensation, position[1]) + # print(f'{compensation=}') + # print(f'{position=}') + img_clip = self._create_watermark(text, redditid, fontsize=fontsize, opacity=opacity) + img_clip = img_clip.set_opacity(opacity).set_duration(duration) + img_clip = img_clip.set_position( + position, relative=True + ) # todo get dara from utils/CONSTANTS.py and adapt position accordingly + + # Overlay the img clip on the first video clip + self.video = CompositeVideoClip([self.video, img_clip]) + return self.video