From c6113d65946d3636e8105b223a1a831b74db5da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20SDV?= Date: Wed, 12 Oct 2022 00:38:19 -0300 Subject: [PATCH 1/7] Creating_a_thumbnail --- .gitignore | 3 ++- GUI/settings.html | 32 ++++++++++++++++++++++++++++ utils/.config.template.toml | 5 ++++- utils/thumbnail.py | 39 +++++++++++++++++++++++++++++++++++ video_creation/final_video.py | 31 +++++++++++++++++++++++++--- 5 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 utils/thumbnail.py diff --git a/.gitignore b/.gitignore index 3da725f..5f4115f 100644 --- a/.gitignore +++ b/.gitignore @@ -232,6 +232,7 @@ fabric.properties .idea/caches/build_file_checksums.ser assets/ +/.vscode out .DS_Store .setup-done-before @@ -243,4 +244,4 @@ video_creation/data/videos.json video_creation/data/envvars.txt config.toml -video_creation/data/videos.json +video_creation/data/videos.json \ No newline at end of file diff --git a/GUI/settings.html b/GUI/settings.html index 2b6b014..b905cb2 100644 --- a/GUI/settings.html +++ b/GUI/settings.html @@ -213,6 +213,38 @@ backgrounds +
+ +
+
+ +
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+ +
+ +
+ +
+

TTS Settings

diff --git a/utils/.config.template.toml b/utils/.config.template.toml index 38e454f..3189cbe 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -28,7 +28,10 @@ storymode = { optional = true, type = "bool", default = false, example = false, background_choice = { optional = true, default = "minecraft", example = "rocket-league", options = ["", "minecraft", "gta", "rocket-league", "motor-gta", "csgo-surf", "cluster-truck"], explanation = "Sets the background for the video based on game name" } #background_audio = { optional = true, type = "bool", default = false, example = false, options = [true, false,], explanation = "Sets a audio to play in the background (put a background.mp3 file in the assets/backgrounds directory for it to be used.)" } #background_audio_volume = { optional = true, type = "float", default = 0.3, example = 0.1, explanation="Sets the volume of the background audio. only used if the background_audio is also set to true" } - +background_thumbnail = { optional = true, type = "bool", default = false, example = false, options = [true, false,], explanation = "Sets a thumbnail to the video (put a thumbnail.png file in the assets/backgrounds directory for it to be used.)" } +background_thumbnail_font_family = { optional = true, default = "arial", example = "arial", explanation = "Sets the font family for the thumbnail text" } +background_thumbnail_font_size = { optional = true, type = "int", default = 96, example = 96, explanation = "Sets the font size in pixels for the thumbnail text" } +background_thumbnail_font_color = { optional = true, default = "255,255,255", example = "255,255,255", explanation = "Sets the font color for the thumbnail text" } [settings.tts] voice_choice = { optional = false, default = "tiktok", options = ["streamlabspolly", "tiktok", "googletranslate", "awspolly", "pyttsx", ], example = "tiktok", explanation = "The voice platform used for TTS generation. This can be left blank and you will be prompted to choose at runtime." } diff --git a/utils/thumbnail.py b/utils/thumbnail.py new file mode 100644 index 0000000..4525d2e --- /dev/null +++ b/utils/thumbnail.py @@ -0,0 +1,39 @@ +from PIL import ImageDraw, ImageFont + + +def create_thumbnail(thumbnail, font_family, font_size, font_color, width, height, title): + + font = ImageFont.truetype(font_family + ".ttf", font_size) + Xaxis = width - (width * 0.2) # 20% of the width + sizeLetterXaxis = font_size * 0.5 # 50% of the font size + XaxisLetterQty = round(Xaxis / sizeLetterXaxis) # Quantity of letters that can fit in the X axis + MarginYaxis = (height * 0.12) # 12% of the height + MarginXaxis = (width * 0.05) # 5% of the width + # 1.1 rem + LineHeight = font_size * 1.1 + # rgb = "255,255,255" transform to list + rgb = font_color.split(",") + rgb = (int(rgb[0]), int(rgb[1]), int(rgb[2])) + + arrayTitle = [] + for word in title.split(): + if len(arrayTitle) == 0: + # colocar a primeira palavra no arrayTitl# put the first word in the arrayTitle + arrayTitle.append(word) + else: + # if the size of arrayTitle is less than qtLetters + if len(arrayTitle[-1]) + len(word) < XaxisLetterQty: + arrayTitle[-1] = arrayTitle[-1] + " " + word + else: + arrayTitle.append(word) + + print("arrayTitle: " + str(arrayTitle)) + + draw = ImageDraw.Draw(thumbnail) + # loop for put the title in the thumbnail + for i in range(0, len(arrayTitle)): + # 1.1 rem + draw.text((MarginXaxis, MarginYaxis + (LineHeight * i)), + arrayTitle[i], rgb, font=font) + + return thumbnail diff --git a/video_creation/final_video.py b/video_creation/final_video.py index ad675c5..fe98bc1 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -3,6 +3,7 @@ import multiprocessing import os import re from os.path import exists +import shutil from typing import Tuple, Any from moviepy.audio.AudioClip import concatenate_audioclips, CompositeAudioClip @@ -19,6 +20,8 @@ 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.thumbnail import create_thumbnail +from PIL import Image console = Console() W, H = 1080, 1920 @@ -125,13 +128,31 @@ def make_final_video( 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)[:251]}.mp4" + filename = f"{name_normalize(title)[:251]}" 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}") + # create a tumbnail for the video + settingsbackground = settings.config["settings"]["background"] + + if settingsbackground["background_thumbnail"]: + if not exists(f"./results/{subreddit}/thumbnails"): + print_substep( + "The results/thumbnails folder didn't exist so I made it") + os.makedirs(f"./results/{subreddit}/thumbnails") + + if settingsbackground["background_thumbnail"] and exists(f"assets/backgrounds/thumbnail.png"): + font_family = settingsbackground["background_thumbnail_font_family"] + font_size = settingsbackground["background_thumbnail_font_size"] + font_color = settingsbackground["background_thumbnail_font_color"] + thumbnail = Image.open(f"assets/backgrounds/thumbnail.png") + width, height = thumbnail.size + thumbnailSave = create_thumbnail(thumbnail, font_family, font_size, font_color, width, height, title) + thumbnailSave.save(f"./assets/temp/{id}/thumbnail.png") + # 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) @@ -152,9 +173,13 @@ def make_final_video( f"assets/temp/{id}/temp.mp4", 0, length, - targetname=f"results/{subreddit}/{filename}", + targetname=f"results/{subreddit}/{filename}.mp4", ) - save_data(subreddit, filename, title, idx, background_config[2]) + #get the thumbnail image from assets/temp/id/thumbnail.png and save it in results/subreddit/thumbnails + if settingsbackground["background_thumbnail"] and exists(f"assets/temp/{id}/thumbnail.png"): + shutil.move(f"assets/temp/{id}/thumbnail.png", f"./results/{subreddit}/thumbnails/{filename}.png") + + save_data(subreddit, filename+".mp4", title, idx, background_config[2]) print_step("Removing temporary files 🗑") cleanups = cleanup(id) print_substep(f"Removed {cleanups} temporary files 🗑") From f191365b244e47315b5636097e79886c3b683442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20SDV?= Date: Wed, 12 Oct 2022 13:29:20 -0300 Subject: [PATCH 2/7] update prints --- GUI/settings.html | 2 +- utils/thumbnail.py | 2 -- video_creation/final_video.py | 1 + 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/GUI/settings.html b/GUI/settings.html index b905cb2..9013600 100644 --- a/GUI/settings.html +++ b/GUI/settings.html @@ -237,7 +237,7 @@ placeholder="96" value="{{ data.background_thumbnail_font_size }}"> - +
diff --git a/utils/thumbnail.py b/utils/thumbnail.py index 4525d2e..6f01e6c 100644 --- a/utils/thumbnail.py +++ b/utils/thumbnail.py @@ -27,8 +27,6 @@ def create_thumbnail(thumbnail, font_family, font_size, font_color, width, heigh else: arrayTitle.append(word) - print("arrayTitle: " + str(arrayTitle)) - draw = ImageDraw.Draw(thumbnail) # loop for put the title in the thumbnail for i in range(0, len(arrayTitle)): diff --git a/video_creation/final_video.py b/video_creation/final_video.py index fe98bc1..e9504ae 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -152,6 +152,7 @@ def make_final_video( width, height = thumbnail.size thumbnailSave = create_thumbnail(thumbnail, font_family, font_size, font_color, width, height, title) thumbnailSave.save(f"./assets/temp/{id}/thumbnail.png") + print_substep("Thumbnail - Building Thumbnail in assets/temp/{id}/thumbnail.png") # 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) From 4b3f3bb8ff6283261937001dfabfbc8a7206bc84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20SDV?= Date: Wed, 12 Oct 2022 13:34:25 -0300 Subject: [PATCH 3/7] correcting filename length ffmpeg no such file#err --- video_creation/final_video.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index e9504ae..b084195 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -128,7 +128,7 @@ def make_final_video( 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)[:251]}" + filename = f"{name_normalize(title)[:150]}" subreddit = settings.config["reddit"]["thread"]["subreddit"] if not exists(f"./results/{subreddit}"): From 3384f838ce7e015c3ad78b11eb9e4925e59ea3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20SDV?= Date: Wed, 12 Oct 2022 13:36:04 -0300 Subject: [PATCH 4/7] creating the correct title to thumbnail --- video_creation/final_video.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index b084195..0c88bc7 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -127,6 +127,7 @@ def make_final_video( 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"]) + title_thumb = reddit_obj["thread_title"] filename = f"{name_normalize(title)[:150]}" subreddit = settings.config["reddit"]["thread"]["subreddit"] @@ -150,7 +151,7 @@ def make_final_video( font_color = settingsbackground["background_thumbnail_font_color"] thumbnail = Image.open(f"assets/backgrounds/thumbnail.png") width, height = thumbnail.size - thumbnailSave = create_thumbnail(thumbnail, font_family, font_size, font_color, width, height, title) + thumbnailSave = create_thumbnail(thumbnail, font_family, font_size, font_color, width, height, title_thumb) thumbnailSave.save(f"./assets/temp/{id}/thumbnail.png") print_substep("Thumbnail - Building Thumbnail in assets/temp/{id}/thumbnail.png") From 40a88f362198b9aef0d9f93f939df370a28a66b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20SDV?= Date: Sat, 15 Oct 2022 13:32:13 -0300 Subject: [PATCH 5/7] get the first image(.png) with any name --- video_creation/final_video.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 0c88bc7..6261ade 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -5,6 +5,7 @@ import re from os.path import exists import shutil from typing import Tuple, Any +from PIL import Image from moviepy.audio.AudioClip import concatenate_audioclips, CompositeAudioClip from moviepy.audio.io.AudioFileClip import AudioFileClip @@ -21,7 +22,6 @@ from utils.console import print_step, print_substep from utils.video import Video from utils.videos import save_data from utils.thumbnail import create_thumbnail -from PIL import Image console = Console() W, H = 1080, 1920 @@ -144,16 +144,27 @@ def make_final_video( print_substep( "The results/thumbnails folder didn't exist so I made it") os.makedirs(f"./results/{subreddit}/thumbnails") + # get the first file with the .png extension from assets/backgrounds and use it as a background for the thumbnail + first_image = next( + ( + file + for file in os.listdir("assets/backgrounds") + if file.endswith(".png") + ), + None, + ) + if first_image is None: + print_substep("No png files found in assets/backgrounds", "red") - if settingsbackground["background_thumbnail"] and exists(f"assets/backgrounds/thumbnail.png"): + if settingsbackground["background_thumbnail"] and first_image: font_family = settingsbackground["background_thumbnail_font_family"] font_size = settingsbackground["background_thumbnail_font_size"] font_color = settingsbackground["background_thumbnail_font_color"] - thumbnail = Image.open(f"assets/backgrounds/thumbnail.png") + thumbnail = Image.open(f"assets/backgrounds/{first_image}") width, height = thumbnail.size thumbnailSave = create_thumbnail(thumbnail, font_family, font_size, font_color, width, height, title_thumb) thumbnailSave.save(f"./assets/temp/{id}/thumbnail.png") - print_substep("Thumbnail - Building Thumbnail in assets/temp/{id}/thumbnail.png") + print_substep(f"Thumbnail - Building Thumbnail in assets/temp/{id}/thumbnail.png") # 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) From eeca4e94a045e88c259d16a832209dd773f8190c Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 19 Dec 2022 16:35:34 +0100 Subject: [PATCH 6/7] Better description for creating a thumbnail --- utils/.config.template.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/.config.template.toml b/utils/.config.template.toml index 3189cbe..18248a3 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -28,10 +28,10 @@ storymode = { optional = true, type = "bool", default = false, example = false, background_choice = { optional = true, default = "minecraft", example = "rocket-league", options = ["", "minecraft", "gta", "rocket-league", "motor-gta", "csgo-surf", "cluster-truck"], explanation = "Sets the background for the video based on game name" } #background_audio = { optional = true, type = "bool", default = false, example = false, options = [true, false,], explanation = "Sets a audio to play in the background (put a background.mp3 file in the assets/backgrounds directory for it to be used.)" } #background_audio_volume = { optional = true, type = "float", default = 0.3, example = 0.1, explanation="Sets the volume of the background audio. only used if the background_audio is also set to true" } -background_thumbnail = { optional = true, type = "bool", default = false, example = false, options = [true, false,], explanation = "Sets a thumbnail to the video (put a thumbnail.png file in the assets/backgrounds directory for it to be used.)" } -background_thumbnail_font_family = { optional = true, default = "arial", example = "arial", explanation = "Sets the font family for the thumbnail text" } -background_thumbnail_font_size = { optional = true, type = "int", default = 96, example = 96, explanation = "Sets the font size in pixels for the thumbnail text" } -background_thumbnail_font_color = { optional = true, default = "255,255,255", example = "255,255,255", explanation = "Sets the font color for the thumbnail text" } +background_thumbnail = { optional = true, type = "bool", default = false, example = false, options = [true, false,], explanation = "Generate a thumbnail for the video (put a thumbnail.png file in the assets/backgrounds directory.)" } +background_thumbnail_font_family = { optional = true, default = "arial", example = "arial", explanation = "Font family for the thumbnail text" } +background_thumbnail_font_size = { optional = true, type = "int", default = 96, example = 96, explanation = "Font size in pixels for the thumbnail text" } +background_thumbnail_font_color = { optional = true, default = "255,255,255", example = "255,255,255", explanation = "Font color in RGB format for the thumbnail text" } [settings.tts] voice_choice = { optional = false, default = "tiktok", options = ["streamlabspolly", "tiktok", "googletranslate", "awspolly", "pyttsx", ], example = "tiktok", explanation = "The voice platform used for TTS generation. This can be left blank and you will be prompted to choose at runtime." } From 43701bef3ea4de6ecfa8c2b1916ebf5e97693ef8 Mon Sep 17 00:00:00 2001 From: Simon <65854503+OpenSourceSimon@users.noreply.github.com> Date: Mon, 19 Dec 2022 16:37:31 +0100 Subject: [PATCH 7/7] Change file limit back --- video_creation/final_video.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 6261ade..c195e2c 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -129,7 +129,7 @@ def make_final_video( idx = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) title_thumb = reddit_obj["thread_title"] - filename = f"{name_normalize(title)[:150]}" + filename = f"{name_normalize(title)[:251]}" subreddit = settings.config["reddit"]["thread"]["subreddit"] if not exists(f"./results/{subreddit}"):