visual_sync module synchronizes videos by correlating motion patterns across different camera views. Even though cameras may have different angles, the timing of motion events (walking, sitting, gestures) should be the same.
extract_motion_energy
Extract motion energy timeseries from a video file.Parameters
Path to the video file to process
Spatial downsampling factor for faster processing. Higher values = faster but less detail.
Gaussian blur kernel size to reduce noise. Must be odd. Set to 0 to disable blurring.
If True, use only center 50% of frame (0.25 to 0.75 in both dimensions). Helps focus on main subject area.
Process every Nth frame for speed. step=3 means process every 3rd frame.
Returns
1D array of motion energy values (normalized 0-1) for each processed frame
Effective frame rate after skipping frames (original_fps / step)
Raises
ValueError- Cannot open the specified video file
smooth_motion_signal
Smooth the motion signal to reduce noise using a uniform filter.Parameters
Raw motion energy signal to smooth
Frame rate of the signal in Hz
Smoothing window duration in seconds. Larger values = more smoothing.
Returns
Smoothed motion energy signal with same length as input
correlate_motion_signals
Find time offset between two motion signals using cross-correlation.Parameters
First motion signal (reference)
Second motion signal to align
Frame rate of the signals in Hz
Maximum expected offset in seconds. Constrains the search range.
Returns
Time offset in seconds to add to sig2 to align with sig1
Confidence score between 0 and 1. Values below 0.3 are considered low confidence.
visualize_motion_signals
Create a visualization plot of motion signals for debugging.Parameters
Dictionary mapping video names to motion signal arrays
Frame rate of the signals for x-axis time scaling
Path where the plot image will be saved (PNG format)
Returns
None - saves plot to the specified output pathsync_videos_by_motion
Main entry point for visual/motion-based video synchronization.Parameters
Directory containing video files to synchronize
List of video filenames to synchronize (must be in video_dir)
Maximum expected offset between videos in seconds
Optional directory to save debug plots. If provided, motion signal visualization will be saved.
Returns
Dictionary mapping filename to offset in seconds. The first file is anchored at 0.0. Add these offsets to align the videos.
Algorithm Overview
The synchronization process consists of three steps:- Motion Extraction: Extract motion energy timeseries from each video using frame differencing
- Pairwise Correlation: Cross-correlate all pairs of motion signals to find relative offsets
- Global Optimization: Find globally consistent offsets using weighted least-squares
Performance Notes
- Videos are processed in parallel using ThreadPoolExecutor for speed
- Motion signals are resampled to a common 10 fps for consistent correlation
- The algorithm focuses on motion timing rather than visual appearance, making it robust to different camera angles and lighting conditions