From 106b79a4a6be59ceba948e73f236bf1583e78791 Mon Sep 17 00:00:00 2001 From: LevaniVashadze Date: Wed, 26 Feb 2025 12:28:09 +0400 Subject: [PATCH 1/4] Removed nvenc encoder --- video_creation/final_video.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index c8be6f5..eedaad5 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -93,7 +93,7 @@ def prepare_background(reddit_id: str, W: int, H: int) -> str: output_path, an=None, **{ - "c:v": "h264_nvenc", + "c:v": "libx264", "b:v": "20M", "b:a": "192k", "threads": multiprocessing.cpu_count(), @@ -438,7 +438,7 @@ def make_final_video( path, f="mp4", **{ - "c:v": "h264_nvenc", + "c:v": "libx264", "b:v": "20M", "b:a": "192k", "threads": multiprocessing.cpu_count(), @@ -468,7 +468,7 @@ def make_final_video( path, f="mp4", **{ - "c:v": "h264_nvenc", + "c:v": "libx264", "b:v": "20M", "b:a": "192k", "threads": multiprocessing.cpu_count(), From f1f67a0272b0c3742c53bf289bf444b99f315a0f Mon Sep 17 00:00:00 2001 From: LevaniVashadze Date: Wed, 26 Feb 2025 23:02:12 +0400 Subject: [PATCH 2/4] add check for nvenc(needs testing on nvidia hardware) --- video_creation/final_video.py | 41 ++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index eedaad5..e729535 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -84,7 +84,30 @@ def name_normalize(name: str) -> str: return name -def prepare_background(reddit_id: str, W: int, H: int) -> str: +def check_nvenc_support(): + """Check if the system supports NVENC encoding by attempting to encode a test frame.""" + import subprocess + + try: + # Try to encode a single black frame using NVENC + result = subprocess.run( + ['ffmpeg', '-loglevel', 'error', '-f', 'lavfi', '-i', + 'color=black:s=1080x1080', '-vframes', '1', '-an', + '-c:v', 'h264_nvenc', '-f', 'null', '-'], + capture_output=True, + text=True, + stderr=subprocess.PIPE + ) + + # If the command succeeds (return code 0) and there's no error output, NVENC is supported + return result.returncode == 0 and not result.stderr + except Exception: + return False + + + + +def prepare_background(reddit_id: str, W: int, H: int, encoder = "libx264") -> str: output_path = f"assets/temp/{reddit_id}/background_noaudio.mp4" output = ( ffmpeg.input(f"assets/temp/{reddit_id}/background.mp4") @@ -93,7 +116,7 @@ def prepare_background(reddit_id: str, W: int, H: int) -> str: output_path, an=None, **{ - "c:v": "libx264", + "c:v": encoder, "b:v": "20M", "b:a": "192k", "threads": multiprocessing.cpu_count(), @@ -217,6 +240,12 @@ def make_final_video( reddit_id = extract_id(reddit_obj) + if check_nvenc_support(): + print("Using NVENC Encoder.") + encoder = "h264_nvenc" + else: + encoder = "libx264" + allowOnlyTTSFolder: bool = ( settings.config["settings"]["background"]["enable_extra_audio"] and settings.config["settings"]["background"]["background_audio_volume"] != 0 @@ -224,7 +253,7 @@ def make_final_video( print_step("Creating the final video 🎥") - background_clip = ffmpeg.input(prepare_background(reddit_id, W=W, H=H)) + background_clip = ffmpeg.input(prepare_background(reddit_id, W=W, H=H, encoder=encoder)) # Gather all audio clips audio_clips = list() @@ -430,7 +459,7 @@ def make_final_video( path = defaultPath + f"/{filename}" path = ( path[:251] + ".mp4" - ) # Prevent a error by limiting the path length, do not change this. + ) # Prevent an error by limiting the path length, do not change this. try: ffmpeg.output( background_clip, @@ -438,7 +467,7 @@ def make_final_video( path, f="mp4", **{ - "c:v": "libx264", + "c:v": encoder, "b:v": "20M", "b:a": "192k", "threads": multiprocessing.cpu_count(), @@ -468,7 +497,7 @@ def make_final_video( path, f="mp4", **{ - "c:v": "libx264", + "c:v": encoder, "b:v": "20M", "b:a": "192k", "threads": multiprocessing.cpu_count(), From fb94c432e9cb524e645ec9b26d770392fcac0721 Mon Sep 17 00:00:00 2001 From: LevaniVashadze Date: Tue, 11 Mar 2025 23:47:50 +0400 Subject: [PATCH 3/4] fixed nvenc check and refactored --- video_creation/final_video.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index e729535..52be33c 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -84,9 +84,10 @@ def name_normalize(name: str) -> str: return name -def check_nvenc_support(): +def get_encoder(): """Check if the system supports NVENC encoding by attempting to encode a test frame.""" import subprocess + base_encoder = "libx264" try: # Try to encode a single black frame using NVENC @@ -100,9 +101,16 @@ def check_nvenc_support(): ) # If the command succeeds (return code 0) and there's no error output, NVENC is supported - return result.returncode == 0 and not result.stderr - except Exception: - return False + if result.returncode == 0: + print("NVENC supported") + return "h264_nvenc" + else: + print("NVENC not supported") + return base_encoder + except Exception as e: + print(f"Error while checking for NVENC support: {e}") + print("Falling back to libx264") + return base_encoder @@ -240,11 +248,7 @@ def make_final_video( reddit_id = extract_id(reddit_obj) - if check_nvenc_support(): - print("Using NVENC Encoder.") - encoder = "h264_nvenc" - else: - encoder = "libx264" + encoder = get_encoder() allowOnlyTTSFolder: bool = ( settings.config["settings"]["background"]["enable_extra_audio"] From d43204b95fbd4f4f9879b2860a2b4bce771ded52 Mon Sep 17 00:00:00 2001 From: LevaniVashadze Date: Tue, 11 Mar 2025 23:48:45 +0400 Subject: [PATCH 4/4] slight improvement in logging --- 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 52be33c..66cc212 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -109,7 +109,7 @@ def get_encoder(): return base_encoder except Exception as e: print(f"Error while checking for NVENC support: {e}") - print("Falling back to libx264") + print(f"Falling back to {base_encoder}") return base_encoder