From 82731db6f2ae946a1ed4b586308e6039271e95e3 Mon Sep 17 00:00:00 2001 From: liamb Date: Sat, 15 Apr 2023 17:08:48 +1000 Subject: [PATCH 1/8] adds background audio --- utils/.config.template.toml | 4 ++-- video_creation/final_video.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/utils/.config.template.toml b/utils/.config.template.toml index b2fa1d4..30aef5c 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -34,8 +34,8 @@ resolution_h = { optional = false, default = 1920, example = 2560, explantation [settings.background] background_choice = { optional = true, default = "minecraft", example = "rocket-league", options = ["minecraft", "gta", "rocket-league", "motor-gta", "csgo-surf", "cluster-truck", "minecraft-2","multiversus","fall-guys","steep", ""], 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_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 = "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" } diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 4838574..7ef9c2b 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -177,6 +177,14 @@ def make_final_video( screenshot_width = int((W * 45) // 100) audio = ffmpeg.input(f"assets/temp/{reddit_id}/audio.mp3") + # adds background audio + if settings.config["settings"]['background']["background_audio"] and exists(f"assets/backgrounds/background.mp3"): + # sets volume to config + bg_audio = ffmpeg.input(f"assets/backgrounds/background.mp3").filter('volume', settings.config["settings"]['background']["background_audio_volume"]) + # merges audio and bg_audio + merged_audio = ffmpeg.filter([audio, bg_audio], 'amix', duration='first') + # sets final audio to merged audio + audio = merged_audio image_clips = list() From 9f79880fb3e55399b06cbe6e5db78901ba59bd5f Mon Sep 17 00:00:00 2001 From: liamb Date: Sat, 15 Apr 2023 17:11:05 +1000 Subject: [PATCH 2/8] sets default voice --- utils/.config.template.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/.config.template.toml b/utils/.config.template.toml index b2fa1d4..ec837a1 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -42,7 +42,7 @@ background_thumbnail_font_size = { optional = true, type = "int", default = 96, 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." } +voice_choice = { optional = false, default = "streamlabspolly", 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." } aws_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for AWS Polly" } streamlabs_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for Streamlabs Polly" } tiktok_voice = { optional = true, default = "en_us_001", example = "en_us_006", explanation = "The voice used for TikTok TTS" } From 74b0061e67e06ad86af04c0142621840a08b9a09 Mon Sep 17 00:00:00 2001 From: Simon <65854503+OpenSourceSimon@users.noreply.github.com> Date: Sat, 15 Apr 2023 09:14:56 +0200 Subject: [PATCH 3/8] Update .config.template.toml --- utils/.config.template.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/.config.template.toml b/utils/.config.template.toml index b2fa1d4..126b2e1 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -46,7 +46,7 @@ voice_choice = { optional = false, default = "tiktok", options = ["streamlabspol aws_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for AWS Polly" } streamlabs_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for Streamlabs Polly" } tiktok_voice = { optional = true, default = "en_us_001", example = "en_us_006", explanation = "The voice used for TikTok TTS" } -tiktok_sessionid = { optional = true, example = "c76bcc3a7625abcc27b508c7db457ff1", explanation = "TikTok sessionid needed for the TTS API request. Check documentation if you don't know how to obtain it." } +tiktok_sessionid = { optional = true, example = "c76bcc3a7625abcc27b508c7db457ff1", explanation = "TikTok sessionid needed if you're using the TikTok TTS. Check documentation if you don't know how to obtain it." } python_voice = { optional = false, default = "1", example = "1", explanation = "The index of the system tts voices (can be downloaded externally, run ptt.py to find value, start from zero)" } py_voice_num = { optional = false, default = "2", example = "2", explanation = "The number of system voices (2 are pre-installed in Windows)" } silence_duration = { optional = true, example = "0.1", explanation = "Time in seconds between TTS comments", default = 0.3, type = "float" } From 5c517a50271efcf32069c44ba6003f7f9641e015 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 16 Apr 2023 14:32:51 +0200 Subject: [PATCH 4/8] Reformat --- video_creation/final_video.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 7ef9c2b..fc19f4e 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -178,13 +178,34 @@ def make_final_video( screenshot_width = int((W * 45) // 100) audio = ffmpeg.input(f"assets/temp/{reddit_id}/audio.mp3") # adds background audio - if settings.config["settings"]['background']["background_audio"] and exists(f"assets/backgrounds/background.mp3"): - # sets volume to config - bg_audio = ffmpeg.input(f"assets/backgrounds/background.mp3").filter('volume', settings.config["settings"]['background']["background_audio_volume"]) - # merges audio and bg_audio - merged_audio = ffmpeg.filter([audio, bg_audio], 'amix', duration='first') - # sets final audio to merged audio - audio = merged_audio + if settings.config["settings"]["background"]["background_audio"]: + if not exists("assets/backgrounds/background.mp3"): + print_substep( + "No audio file found called background.mp3 in assets/backgrounds", "red" + ) + else: + if ( + not settings.config["settings"]["background"]["background_audio_volume"] + or settings.config["settings"]["background"]["background_audio_volume"] == 0 + or settings.config["settings"]["background"]["background_audio_volume"] == "" + ): + print_substep( + "Background audio volume is set to 0, not adding background audio", + "red", + ) + else: + # sets volume to config + bg_audio = ( + ffmpeg.input("assets/backgrounds/background.mp3") + .filter( + "volume", + settings.config["settings"]["background"]["background_audio_volume"], + ) + ) + # merges audio and bg_audio + merged_audio = ffmpeg.filter([audio, bg_audio], "amix", duration="longest") + # sets final audio to merged audio + audio = merged_audio image_clips = list() From 3515afe7f18aa595dce3476b67e262e3ee789e3f Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 16 Apr 2023 14:42:22 +0200 Subject: [PATCH 5/8] Display ffmpeg error if any --- main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index b7a1b7f..d2273c6 100755 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ from os import name from pathlib import Path from subprocess import Popen +import ffmpeg from prawcore import ResponseException from utils.console import print_substep from reddit.subreddit import get_subreddit_threads @@ -53,7 +54,11 @@ def main(POST_ID=None) -> None: bg_config = get_background_config() download_background(bg_config) chop_background_video(bg_config, length, reddit_object) - make_final_video(number_of_comments, length, reddit_object, bg_config) + try: + make_final_video(number_of_comments, length, reddit_object, bg_config) + except ffmpeg.Error as e: + print(e.stderr.decode("utf8")) + exit(1) def run_many(times) -> None: From 9bacf100dfcc6e3b3ede4e7013d6e329e29fc713 Mon Sep 17 00:00:00 2001 From: Xpl0itU <24777100+Xpl0itU@users.noreply.github.com> Date: Sun, 16 Apr 2023 15:28:28 +0200 Subject: [PATCH 6/8] Use yt-dlp for downloading the background videos --- requirements.txt | 4 ++-- video_creation/background.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/requirements.txt b/requirements.txt index 22d3d7f..823493e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,6 @@ moviepy==1.0.3 playwright==1.23.0 praw==7.6.1 prawcore~=2.3.0 -pytube==12.1.0 requests==2.28.1 rich==13.3.1 toml==0.10.2 @@ -19,4 +18,5 @@ unidecode==1.3.2 spacy==3.4.1 torch==1.12.1 transformers==4.25.1 -ffmpeg-python==0.2.0 \ No newline at end of file +ffmpeg-python==0.2.0 +yt-dlp==2023.3.4 \ No newline at end of file diff --git a/video_creation/background.py b/video_creation/background.py index 0458ce6..14c9623 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -7,10 +7,9 @@ from typing import Any, Tuple from moviepy.editor import VideoFileClip from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip -from pytube import YouTube -from pytube.cli import on_progress from utils import settings from utils.console import print_step, print_substep +import yt_dlp # Load background videos with open("./utils/backgrounds.json") as json_file: @@ -72,9 +71,14 @@ def download_background(background_config: Tuple[str, str, str, Any]): ) print_substep("Downloading the backgrounds videos... please be patient 🙏 ") print_substep(f"Downloading {filename} from {uri}") - YouTube(uri, on_progress_callback=on_progress).streams.filter( - res="1080p" - ).first().download("assets/backgrounds", filename=f"{credit}-{filename}") + ydl_opts = { + 'format': "bestvideo[height<=1080][ext=mp4]", + "outtmpl": f"assets/backgrounds/{credit}-{filename}", + "retries": 10, + } + + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + ydl.download(uri) print_substep("Background video downloaded successfully! 🎉", style="bold green") From 40e42b1307d02623a8d241d782e59993a91109a2 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 16 Apr 2023 15:45:07 +0200 Subject: [PATCH 7/8] Fix font size --- utils/imagenarator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/imagenarator.py b/utils/imagenarator.py index a3883b6..847073c 100644 --- a/utils/imagenarator.py +++ b/utils/imagenarator.py @@ -68,9 +68,9 @@ def imagemaker(theme, reddit_obj: dict, txtclr, padding=5, transparent=False) -> tfont = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 50) else: tfont = ImageFont.truetype( - os.path.join("fonts", "Roboto-Bold.ttf"), 35 + os.path.join("fonts", "Roboto-Bold.ttf"), 100 ) # for title - font = ImageFont.truetype(os.path.join("fonts", "Roboto-Regular.ttf"), 30) + font = ImageFont.truetype(os.path.join("fonts", "Roboto-Regular.ttf"), 90) size = (1920, 1080) image = Image.new("RGBA", size, theme) From fb039735437455c3aade22a29b475fd2cada4fe9 Mon Sep 17 00:00:00 2001 From: Xpl0itU <24777100+Xpl0itU@users.noreply.github.com> Date: Sun, 16 Apr 2023 16:03:07 +0200 Subject: [PATCH 8/8] Add random background choice --- utils/.config.template.toml | 2 +- video_creation/background.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/.config.template.toml b/utils/.config.template.toml index 0eaeb26..387eeb6 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -33,7 +33,7 @@ resolution_w = { optional = false, default = 1080, example = 1440, explantation resolution_h = { optional = false, default = 1920, example = 2560, explantation = "Sets the height in pixels of the final video" } [settings.background] -background_choice = { optional = true, default = "minecraft", example = "rocket-league", options = ["minecraft", "gta", "rocket-league", "motor-gta", "csgo-surf", "cluster-truck", "minecraft-2","multiversus","fall-guys","steep", ""], explanation = "Sets the background for the video based on game name" } +background_choice = { optional = true, default = "minecraft", example = "rocket-league", options = ["minecraft", "gta", "rocket-league", "motor-gta", "csgo-surf", "cluster-truck", "minecraft-2","multiversus","fall-guys","steep", "random", ""], 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 = "Generate a thumbnail for the video (put a thumbnail.png file in the assets/backgrounds directory.)" } diff --git a/video_creation/background.py b/video_creation/background.py index 14c9623..87f725a 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -53,7 +53,7 @@ def get_background_config(): # Handle default / not supported background using default option. # Default : pick random from supported background. - if not choice or choice not in background_options: + if not choice or choice not in background_options or choice == "random": choice = random.choice(list(background_options.keys())) return background_options[choice]