Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions video_creation/final_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -93,7 +116,7 @@ def prepare_background(reddit_id: str, W: int, H: int) -> str:
output_path,
an=None,
**{
"c:v": "h264_nvenc",
"c:v": encoder,
"b:v": "20M",
"b:a": "192k",
"threads": multiprocessing.cpu_count(),
Expand Down Expand Up @@ -217,14 +240,20 @@ def make_final_video(

reddit_id = extract_id(reddit_obj)

if check_nvenc_support():
Comment thread
LevaniVashadze marked this conversation as resolved.
Outdated
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
)

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()
Expand Down Expand Up @@ -430,15 +459,15 @@ def on_update_example(progress) -> None:
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,
final_audio,
path,
f="mp4",
**{
"c:v": "h264_nvenc",
"c:v": encoder,
"b:v": "20M",
"b:a": "192k",
"threads": multiprocessing.cpu_count(),
Expand Down Expand Up @@ -468,7 +497,7 @@ def on_update_example(progress) -> None:
path,
f="mp4",
**{
"c:v": "h264_nvenc",
"c:v": encoder,
"b:v": "20M",
"b:a": "192k",
"threads": multiprocessing.cpu_count(),
Expand Down