Overview
Theui.py module provides a Flask-based web interface for the video synchronization workflow. It includes a three-step wizard for uploading videos, previewing them, and viewing synchronized results.
Flask Application
Main Flask application instance.
Application State
Global state dictionary that tracks the synchronization workflow.Fields:
selected_files(list): List of selected video filenamessync_progress(int): Current progress percentage (0-100)sync_status(str): Current status message (“idle”, “Processing…”, etc.)output_dir(str): Output directory path (defaults toconfig.OUTPUT_DIR)current_step(int): Current wizard step (1, 2, or 3)offsets(dict): Calculated time offsets for each video filelogs(deque): Ring buffer storing last 500 log messagessession_id(str): Unique session identifier
Logging
configure_logging
Sets up centralized logging to file, console, and UI.Configuration:
- Log directory:
logs/ - Log file:
logs/video_sync.log - File rotation: 10MB max size, 5 backup files
- Format:
%(asctime)s - %(name)s - %(levelname)s - %(message)s
- RotatingFileHandler: Writes to log file with rotation
- StreamHandler: Outputs to console
- UILogHandler: Captures logs for real-time UI display
UILogHandler
Custom logging handler that stores logs in Storage: Logs are stored in a
app_state["logs"] for UI display.Inherits: logging.HandlerLog Entry Format:deque with max length 500Routes
Step Routes
Step 1: File SelectionRenders the file upload interface.Actions:
- Resets
app_stateto initial values - Generates unique session ID
- Sets
current_stepto 1
Step 2: Video PreviewDisplays uploaded videos for review before synchronization.Redirects to:
/ if no files are selectedStep 3: Synchronization ResultsShows progress during sync and displays results when complete.Redirects to:
/ if no files are selectedAPI Routes
Handles file uploads from the client.Request: Multipart form data with
files[] fieldValidation:- Minimum 2 files required
- Allowed extensions:
.mp4,.mov,.avi - Filenames are sanitized using
secure_filename()
Validates and selects video files for synchronization.Request Body:Validation:
- Minimum 2 files required
- All files must exist in
VIDEO_DIR
Initiates the synchronization process in a background thread.Process:
- Checks
SYNC_METHODconfiguration (“visual” or “audio”) - Runs synchronization algorithm
- Generates sync indicators
- Applies offsets to videos
- Updates
app_stateprogress throughout
Returns current synchronization progress.Response:
Returns recent log messages for UI display.Response:
Clears the log buffer.Response:
Returns list of synchronized video files.Response:
Creates and downloads a ZIP file of all synchronized videos.Response: Binary ZIP file with MIME type
application/zipFilename: synced_videos.zipVideo Serving Routes
Serves raw uploaded video files from
VIDEO_DIR.Serves synchronized video files from
OUTPUT_DIR.Main Function
run_app
Launches the Flask web UI.Configuration:
- Host:
127.0.0.1(localhost only) - Port:
5050 - Debug:
False - Threaded:
True
- Prints startup banner with URL
- Automatically opens browser after 1 second
- Runs in non-debug mode with threading enabled
UI Features
Sync Progress Tracking
The UI tracks progress through several stages:| Progress | Status Message |
|---|---|
| 0% | “Initializing…“ |
| 5-10% | “Initializing visual synchronization…” / “Extracting audio…“ |
| 40-55% | “Analyzing audio offsets…” / “Motion analysis complete!“ |
| 58% | “Generating visual sync indicators…“ |
| 65% | “Applying offsets to videos…“ |
| 100% | “Complete!” |
Real-time Logging
The UI includes a real-time log viewer with:- Auto-scroll: Automatically scrolls to newest logs
- Filtering: Filter by log level (All, INFO, WARNING, ERROR)
- Ring buffer: Stores last 500 log messages
- Polling: Updates every 500ms during processing
Video Playback Controls
- Play/Pause: Toggle playback on all videos simultaneously
- Mute/Unmute: Control audio for all videos
- Restart: Reset all videos to beginning
- Universal Seek Bar: Synchronized seeking across all videos