Skip to content

Video encoding separation to new file#248

Merged
Glat0s merged 10 commits into
VisoMasterFusion:devfrom
Elricfae:dev
May 16, 2026
Merged

Video encoding separation to new file#248
Glat0s merged 10 commits into
VisoMasterFusion:devfrom
Elricfae:dev

Conversation

@Elricfae
Copy link
Copy Markdown
Contributor

@Elricfae Elricfae commented May 15, 2026

Overview

This PR refactors the FFmpeg encoding pipeline by extracting subprocess management into a new dedicated module (video_encoding.py), reducing code duplication by ~500 lines and improving testability and maintainability.

🎯 Key Changes

1. New Module: FFmpegEncoder Class (video_utils/video_encoding.py)

  • Encapsulates FFmpeg subprocess lifecycle: process creation, frame writing, and graceful shutdown
  • Implements robust timeout handling with 3-step escalation (graceful close → SIGTERM → SIGKILL)
  • Handles source video probing with caching to prevent redundant I/O calls
  • Manages quality adaptation logic (CQ/CRF calculation from source metrics)
  • Provides public interface: start_process(), write_frame(), close_process(), is_running()

2. New Module: FFmpegPostProcessor Class (Stateless utilities)

  • extract_audio_segments() – Extracts per-segment audio with validation
  • concatenate_audio_segments() – Merges audio using FFmpeg concat demuxer
  • write_video_only_output() – Fallback video-only muxing when audio processing fails
  • concatenate_segments_video_only() – Segment-mode video concatenation fallback
  • validate_audio_file() – Validates audio integrity before processing

3. Refactored video_processor.py

  • Replaced inline FFmpeg subprocess calls with self.encoder.start_process()
  • Replaced direct stdin writes with self.encoder.write_frame() and self.encoder.is_running()
  • Simplified cleanup: self.encoder.close_process() handles all subprocess termination
  • Removed obsolete create_ffmpeg_subprocess() method (functionality moved to FFmpegEncoder)
  • Removed redundant audio extraction methods (now in FFmpegPostProcessor)

4. Enhanced Face Expression Processing (frame_edits.py, faceutil.py)

  • Gaze Stabilization Pipeline: New "Camera Gaze Lock" feature forces eyes to look at camera with per-frame adjustment
  • Eye Aspect Ratio (EAR) Safeguards: Profile occlusion detection, symmetric blink harmonization, anti-bulging eyelid compensation
  • Mouth Aspect Ratio (MAR) Safeguards: Division-by-zero prevention, biological plausibility clamping
  • Split-Eye Calculation: Enable asymmetric winking via independent left/right eye retargeting
  • Structural Lip Decoupling: Separate relative (X-axis shape) and absolute (Y/Z-axis jaw) motion with blending
  • Removed redundant GPU stream synchronization after processing

5. UI/UX Improvements

  • Added "Camera Gaze Lock" controls to advanced face expression settings
  • Added keyboard shortcut X for Face Compare mode
  • Fixed keyboard navigation to use step-through pipeline (prevents flickering in Compare/Mask modes)
  • Improved slider preview behavior: suppresses raw frame flash when stepping with special modes active
  • Fixed arrow key navigation to ensure single-frame stepping instead of slider defaults

6. TensorRT Cache Cleanup (models_processor.py)

  • New _clean_tensorrt_cache() method safely removes corrupted engine/timing files
  • Prevents "cache poisoning" failures on retry after timeout
  • Handles locked/missing files gracefully

✅ Benefits

  • Testability: FFmpeg logic is now isolated and unit-testable
  • Maintainability: Centralized encoding/audio handling vs. scattered throughout codebase
  • Extensibility: Easy to add new post-processors (subtitles, metadata, etc.)
  • Robustness: Explicit lifecycle management prevents zombie processes and timeout cascades
  • Performance: Source metrics caching eliminates redundant ffprobe calls
  • User Experience: Smoother navigation with reduced UI flashing

@Elricfae Elricfae added the minor New feature, backwards compatible label May 16, 2026
@Glat0s Glat0s merged commit 9c31310 into VisoMasterFusion:dev May 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor New feature, backwards compatible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants