From d9ff36b034e83865080fc7c1a6d4afdfd7908b9a Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 23 Apr 2023 18:10:10 -0300 Subject: [PATCH] Merge with develop branch and no pytube required --- main.py | 9 +++++++-- requirements.txt | 1 - utils/.config.template.toml | 4 ++-- video_creation/background.py | 16 ++++++++++++---- video_creation/final_video.py | 4 ---- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 81fb503..0a17968 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 @@ -57,7 +58,11 @@ def main(POST_ID=None) -> None: download_background_video(bg_config["video"]) download_background_audio(bg_config["audio"]) chop_background(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: @@ -131,4 +136,4 @@ if __name__ == "__main__": f"Error: {err} \n" f'Config: {config["settings"]}' ) - raise err + raise err \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 634a24b..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 diff --git a/utils/.config.template.toml b/utils/.config.template.toml index a9d6d27..c8d7d61 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -43,11 +43,11 @@ 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" } -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" } diff --git a/video_creation/background.py b/video_creation/background.py index 3b896d9..8c3dcee 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -7,7 +7,6 @@ from typing import Any, Tuple,Dict from moviepy.editor import VideoFileClip,AudioFileClip 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 @@ -104,9 +103,18 @@ def download_background_audio(background_config: Tuple[str, str, str]): ) print_substep("Downloading the backgrounds audio... please be patient 🙏 ") print_substep(f"Downloading {filename} from {uri}") - YouTube(uri, on_progress_callback=on_progress).streams.filter( - only_audio=True - ).first().download("assets/backgrounds/audio", filename=f"{credit}-{filename}") + ydl_opts = { + 'outtmpl': f'./assets/backgrounds/audio/{credit}-{filename}', + 'format': 'bestaudio/best', + 'postprocessors': [{ + 'preferredcodec': 'mp3', + 'preferredquality': '192', + }], + } + + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + ydl.download([uri]) + print_substep("Background audio downloaded successfully! 🎉", style="bold green") diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 63c5842..452b128 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -398,12 +398,8 @@ def make_final_video( ) old_percentage = pbar.n pbar.update(100 - old_percentage) - pbar.close() - - - save_data(subreddit, filename + ".mp4", title, idx, background_config['video'][2]) print_step("Removing temporary files 🗑") cleanups = cleanup(reddit_id)