Rendering optimization

The PNG encode/decode alone was consuming significant CPU time per frame. This is eliminated entirely.
This commit is contained in:
Kenneth Estanislao
2026-04-09 16:25:22 +08:00
parent 2b26d5539e
commit 69e3fc5611
3 changed files with 355 additions and 31 deletions
+56 -30
View File
@@ -22,7 +22,7 @@ import tensorflow
import modules.globals
import modules.metadata
import modules.ui as ui
from modules.processors.frame.core import get_frame_processors_modules
from modules.processors.frame.core import get_frame_processors_modules, process_video_in_memory
from modules.utilities import has_image_extension, is_image, is_video, detect_fps, create_video, extract_frames, get_temp_frame_paths, restore_audio, create_temp, move_temp, clean_temp, normalize_output_path
if HAS_TORCH and 'ROCMExecutionProvider' in modules.globals.execution_providers:
@@ -222,44 +222,70 @@ def start() -> None:
if modules.globals.nsfw_filter and ui.check_and_ignore_nsfw(modules.globals.target_path, destroy):
return
extraction_start = time.time()
if not modules.globals.map_faces:
update_status('Creating temp resources...')
create_temp(modules.globals.target_path)
update_status('Extracting frames...')
extract_frames(modules.globals.target_path)
extraction_time = time.time() - extraction_start
update_status(f'Frame extraction completed in {extraction_time:.2f}s')
temp_frame_paths = get_temp_frame_paths(modules.globals.target_path)
total_frames = len(temp_frame_paths)
update_status(f'Processing {total_frames} frames with {modules.globals.execution_threads} threads...')
processing_start = time.time()
for frame_processor in get_frame_processors_modules(modules.globals.frame_processors):
update_status('Progressing...', frame_processor.NAME)
frame_processor.process_video(modules.globals.source_path, temp_frame_paths)
release_resources()
processing_time = time.time() - processing_start
fps_processing = total_frames / processing_time if processing_time > 0 else 0
update_status(f'Frame processing completed in {processing_time:.2f}s ({fps_processing:.2f} fps)')
# handles fps
encoding_start = time.time()
# Detect FPS early (needed by both pipelines)
if modules.globals.keep_fps:
update_status('Detecting fps...')
fps = detect_fps(modules.globals.target_path)
else:
fps = 30.0
video_created = False
# --- In-memory pipeline (non-map_faces only) ---
# Reads frames from FFmpeg pipe, processes in memory, encodes directly.
# Eliminates all per-frame PNG disk I/O for a major speed-up.
if not modules.globals.map_faces:
update_status(f'Processing video in-memory at {fps} fps...')
create_temp(modules.globals.target_path)
processing_start = time.time()
video_created = process_video_in_memory(
modules.globals.source_path,
modules.globals.target_path,
fps,
)
processing_time = time.time() - processing_start
release_resources()
if video_created:
update_status(f'In-memory processing + encoding completed in {processing_time:.2f}s')
# --- Disk-based fallback (required for map_faces, or if pipe failed) ---
if not video_created:
if not modules.globals.map_faces:
update_status('Falling back to disk-based processing...')
extraction_start = time.time()
if not modules.globals.map_faces:
create_temp(modules.globals.target_path)
update_status('Extracting frames...')
extract_frames(modules.globals.target_path)
extraction_time = time.time() - extraction_start
temp_frame_paths = get_temp_frame_paths(modules.globals.target_path)
total_frames = len(temp_frame_paths)
update_status(f'Processing {total_frames} frames with {modules.globals.execution_threads} threads...')
processing_start = time.time()
for frame_processor in get_frame_processors_modules(modules.globals.frame_processors):
update_status('Progressing...', frame_processor.NAME)
frame_processor.process_video(modules.globals.source_path, temp_frame_paths)
release_resources()
processing_time = time.time() - processing_start
fps_processing = total_frames / processing_time if processing_time > 0 else 0
update_status(f'Frame processing completed in {processing_time:.2f}s ({fps_processing:.2f} fps)')
encoding_start = time.time()
update_status(f'Creating video with {fps} fps...')
video_created = create_video(modules.globals.target_path, fps)
else:
update_status('Creating video with 30.0 fps...')
video_created = create_video(modules.globals.target_path)
encoding_time = time.time() - encoding_start
encoding_time = time.time() - encoding_start
if video_created:
update_status(f'Video encoding completed in {encoding_time:.2f}s')
if not video_created:
update_status('Video encoding failed. No temporary output video was created.')
clean_temp(modules.globals.target_path)
return
update_status(f'Video encoding completed in {encoding_time:.2f}s')
# handle audio
if modules.globals.keep_audio: