add check for nvenc(needs testing on nvidia hardware)

pull/2259/head
LevaniVashadze 6 months ago
parent 106b79a4a6
commit f1f67a0272

@ -84,7 +84,30 @@ def name_normalize(name: str) -> str:
return name 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_path = f"assets/temp/{reddit_id}/background_noaudio.mp4"
output = ( output = (
ffmpeg.input(f"assets/temp/{reddit_id}/background.mp4") 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, output_path,
an=None, an=None,
**{ **{
"c:v": "libx264", "c:v": encoder,
"b:v": "20M", "b:v": "20M",
"b:a": "192k", "b:a": "192k",
"threads": multiprocessing.cpu_count(), "threads": multiprocessing.cpu_count(),
@ -217,6 +240,12 @@ def make_final_video(
reddit_id = extract_id(reddit_obj) reddit_id = extract_id(reddit_obj)
if check_nvenc_support():
print("Using NVENC Encoder.")
encoder = "h264_nvenc"
else:
encoder = "libx264"
allowOnlyTTSFolder: bool = ( allowOnlyTTSFolder: bool = (
settings.config["settings"]["background"]["enable_extra_audio"] settings.config["settings"]["background"]["enable_extra_audio"]
and settings.config["settings"]["background"]["background_audio_volume"] != 0 and settings.config["settings"]["background"]["background_audio_volume"] != 0
@ -224,7 +253,7 @@ def make_final_video(
print_step("Creating the 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 # Gather all audio clips
audio_clips = list() audio_clips = list()
@ -430,7 +459,7 @@ def make_final_video(
path = defaultPath + f"/{filename}" path = defaultPath + f"/{filename}"
path = ( path = (
path[:251] + ".mp4" 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: try:
ffmpeg.output( ffmpeg.output(
background_clip, background_clip,
@ -438,7 +467,7 @@ def make_final_video(
path, path,
f="mp4", f="mp4",
**{ **{
"c:v": "libx264", "c:v": encoder,
"b:v": "20M", "b:v": "20M",
"b:a": "192k", "b:a": "192k",
"threads": multiprocessing.cpu_count(), "threads": multiprocessing.cpu_count(),
@ -468,7 +497,7 @@ def make_final_video(
path, path,
f="mp4", f="mp4",
**{ **{
"c:v": "libx264", "c:v": encoder,
"b:v": "20M", "b:v": "20M",
"b:a": "192k", "b:a": "192k",
"threads": multiprocessing.cpu_count(), "threads": multiprocessing.cpu_count(),

Loading…
Cancel
Save