From f51148bcdbb4c23a6c393b0f96517c5e01564fb3 Mon Sep 17 00:00:00 2001 From: Mohamed Moataz Date: Tue, 30 Apr 2024 12:56:38 +0300 Subject: [PATCH] fix: :bug: Bug in ffmpeg when generating videos for some posts --- main.py | 47 ++++++++++++++++++++++----------- utils/.config.template.toml | 1 + utils/background_audios.json | 10 +++---- utils/background_videos.json | 4 +-- video_creation/final_video.py | 18 ++++++++----- video_data_generation/gemini.py | 4 +-- 6 files changed, 53 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index b9b13e1..8a19fc6 100755 --- a/main.py +++ b/main.py @@ -53,14 +53,14 @@ def main(POST_ID=None) -> None: global redditid, reddit_object reddit_object = get_subreddit_threads(POST_ID) redditid = id(reddit_object) + if not (settings.config["settings"]["debug"]["reuse_images"] and + settings.config["settings"]["debug"]["reuse_separate_mp3s"]): + reddit_object['thread_post'] = proofread_post(reddit_object['thread_post']) post_text = ' '.join(reddit_object['thread_post']) - print(reddit_object['thread_post']) - print("================================================================") - print(post_text) if not os.path.exists(f"./assets/temp/{reddit_object['thread_id']}/"): for key in settings.config["settings"]["debug"].keys(): - if key != "debug": + if key != "debug" and key != "no_youtube": settings.config["settings"]["debug"][key] = False length, number_of_comments = save_text_to_mp3(reddit_object) @@ -68,14 +68,21 @@ def main(POST_ID=None) -> None: reel = length <= 60 get_screenshots_of_reddit_posts(reddit_object, number_of_comments, reel) - bg_config = { - "video": get_background_config("video"), - "audio": get_background_config("audio"), - } - download_background_video(bg_config["video"]) - download_background_audio(bg_config["audio"]) - chop_background(bg_config, length, reddit_object) + if not settings.config["settings"]["debug"]["reuse_background"]: + bg_config = { + "video": get_background_config("video"), + "audio": get_background_config("audio"), + } + download_background_video(bg_config["video"]) + download_background_audio(bg_config["audio"]) + chop_background(bg_config, length, reddit_object) + else: + bg_config = { + "video": ["debug mode", "debug mode", "debug mode"], + "audio": ["debug mode", "debug mode", "debug mode"], + } video_path = make_final_video(number_of_comments, length, reddit_object, bg_config, reel) + if not video_path: return False video_path = compress_video(video_path) video_data, thumbnail_text = get_video_data(post_text, bg_config) @@ -90,10 +97,12 @@ def main(POST_ID=None) -> None: save_path=f"./assets/temp/{reddit_object['thread_id']}/thumbnail.png" ) print("Thumbnail generated successfully at:", thumbnail) - upload_video_to_youtube(video_path, video_data, thumbnail) + if not settings.config["settings"]["debug"]["no_youtube"]: + upload_video_to_youtube(video_path, video_data, thumbnail) if not settings.config["settings"]["debug"]["debug"]: shutil.rmtree(f"./assets/temp/{reddit_object['thread_id']}/") + return True def run_many(times) -> None: @@ -122,10 +131,13 @@ def run(): ) main(post_id) Popen("cls" if name == "nt" else "clear", shell=True).wait() - elif config["settings"]["times_to_run"]: - run_many(config["settings"]["times_to_run"]) + # elif config["settings"]["times_to_run"]: + # run_many(config["settings"]["times_to_run"]) else: - main() + while True: + status = main() + print("Status:", status) + if status: break print_substep("The video was created successfully! 🎉", style="bold green") print_substep( @@ -149,7 +161,10 @@ if __name__ == "__main__": from video_data_generation.gemini import get_video_data from video_data_generation.image_generation import generate_image, add_text - from utils.youtube_uploader import upload_video_to_youtube + from utils.proofreading import proofread_post + + if not settings.config["settings"]["debug"]["no_youtube"]: + from utils.youtube_uploader import upload_video_to_youtube if ( not settings.config["settings"]["tts"]["tiktok_sessionid"] diff --git a/utils/.config.template.toml b/utils/.config.template.toml index af30ed1..941271f 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -42,6 +42,7 @@ reuse_mp3 = { optional = false, type = "bool", default = false, example = false, reuse_images = { optional = false, type = "bool", default = false, example = false, options = [true, false, ], explanation = "Use images from temp data" } reuse_background = { optional = false, type = "bool", default = false, example = false, options = [true, false, ], explanation = "Use previously generated background audio & video." } reuse_video = { optional = false, type = "bool", default = false, example = false, options = [true, false, ], explanation = "Use already generated video" } +no_youtube = { optional = false, type = "bool", default = false, example = false, options = [true, false, ], explanation = "Upload to youtube" } [settings.background] background_video = { optional = true, default = "mudrunner", example = "granny-remake", explanation = "Sets the background for the video based on game name" } diff --git a/utils/background_audios.json b/utils/background_audios.json index 5841b2c..e2832d6 100644 --- a/utils/background_audios.json +++ b/utils/background_audios.json @@ -3,26 +3,26 @@ "haunted_forest":[ "https://www.youtube.com/watch?v=Yb5XQ9i-gtI", "haunted_forest.mp3", - "@RPGAmbientSounds" + "RPGAmbientSounds" ], "scary_forest":[ "https://www.youtube.com/watch?v=BzYPUjtEVqs", "scary_forest.mp3", - "@GravitySound" + "GravitySound" ], "night_of_the_dead":[ "https://www.youtube.com/watch?v=tmlZeYnfw7g", "night_of_the_dead.mp3", - "@MysticalSoundscapes" + "MysticalSoundscapes" ], "hades":[ "https://www.youtube.com/watch?v=fBR1Qp3njCM", "hades.mp3", - "@restforthewicked6842" + "restforthewicked6842" ], "forest_at_night":[ "https://www.youtube.com/watch?v=3TNK916Pjto", "forest_at_night.mp3", - "@HawaiiASMRNatureRelaxation" + "HawaiiASMRNatureRelaxation" ] } diff --git a/utils/background_videos.json b/utils/background_videos.json index 525db65..e2da97a 100644 --- a/utils/background_videos.json +++ b/utils/background_videos.json @@ -3,13 +3,13 @@ "mudrunner": [ "https://www.youtube.com/watch?v=YsfkZoXv4s8", "mudrunner.mp4", - "@joel.gameplays", + "joel.gameplays", "center" ], "granny-remake": [ "https://www.youtube.com/watch?v=Ynjc1EcRExk", "granny.mp4", - "@DopeGameplays", + "DopeGameplays", "center" ] } \ No newline at end of file diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 0435948..5b08d95 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -86,6 +86,8 @@ def name_normalize(name: str) -> str: def prepare_background(reddit_id: str, W: int, H: int) -> str: output_path = f"assets/temp/{reddit_id}/background_noaudio.mp4" + if settings.config["settings"]["debug"]["reuse_background"]: + return output_path output = ( ffmpeg.input(f"assets/temp/{reddit_id}/background.mp4") .filter("crop", f"ih*({W}/{H})", "ih") @@ -224,11 +226,12 @@ def make_final_video( # louder_audio = audio + 10 # louder_audio.export(f"assets/temp/{reddit_id}/audio.mp3", format='mp3') + print("Merging background audio...") audio = ffmpeg.input(f"assets/temp/{reddit_id}/audio.mp3") final_audio = merge_background_audio(audio, reddit_id) image_clips = list() - + print("Adding image clips...") image_clips.insert( 0, ffmpeg.input(f"assets/temp/{reddit_id}/png/title.png")["v"].filter( @@ -322,6 +325,7 @@ def make_final_video( ) current_time += audio_clips_durations[i] + print("Added clips successfully.") 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"] @@ -373,6 +377,7 @@ def make_final_video( print_substep(f"Thumbnail - Building Thumbnail in assets/temp/{reddit_id}/thumbnail.png") text = f"Background by {background_config['video'][2]}" + print("Drawing text:", text) background_clip = ffmpeg.drawtext( background_clip, text=text, @@ -395,11 +400,12 @@ def make_final_video( defaultPath = f"results/{subreddit}" with ProgressFfmpeg(length, on_update_example) as progress: - path = defaultPath + f"/{filename}" + path = defaultPath + f"/{filename[:15]}" path = ( - path[:251] + ".mp4" + path[:120] + ".mp4" ) # Prevent a error by limiting the path length, do not change this. try: + print(" DEBUG PATH:", path) ffmpeg.output( background_clip, final_audio, @@ -417,9 +423,9 @@ def make_final_video( capture_stdout=False, capture_stderr=False, ) - except ffmpeg.Error as e: - print(e.stderr.decode("utf8")) - exit(1) + except: + save_data(subreddit, filename + ".mp4", title, idx, background_config["video"][2]) + return False old_percentage = pbar.n pbar.update(100 - old_percentage) if allowOnlyTTSFolder: diff --git a/video_data_generation/gemini.py b/video_data_generation/gemini.py index 19ccbe6..eec6012 100644 --- a/video_data_generation/gemini.py +++ b/video_data_generation/gemini.py @@ -54,8 +54,8 @@ def get_credits(bg_config): video_credits = bg_config['video'][-2] credits_template = f""" -Background audio by - {audio_credits} -Background video by - {video_credits} +Background audio by - @{audio_credits} +Background video by - @{video_credits} """ return credits_template