video_sync module applies computed time offsets to entire video files (both audio and video streams) using ffmpeg. It supports both stream copying (fast, lossless) and re-encoding when necessary.
apply_video_offsets
Apply time offsets to video files based on computed synchronization offsets.Parameters
Directory containing original video files
Dictionary mapping filename to offset in seconds:
- Positive offset (
>0): Delay the entire file (video+audio start later with black frames/silence) - Negative offset (
<0): Trim the first |offset| seconds from the file
Directory where synchronized videos will be saved. Created if it doesn’t exist.
Show tqdm progress bar during processing
Returns
Returns True when all videos have been processed successfully
Output Files
Synchronized videos are saved with_synced suffix:
- Input:
camera1.mp4→ Output:camera1_synced.mp4 - Input:
camera2.mp4→ Output:camera2_synced.mp4
Offset Semantics
The function uses “Option C” semantics for offset application:- Positive offset (+2.0s): Adds 2 seconds of black frames at the start
- Negative offset (-1.5s): Removes the first 1.5 seconds of content
- Zero offset (0.0s): Simple copy with duration padding if needed
Processing Strategy
The function intelligently chooses between stream copying and re-encoding:-
Stream Copy (fast, lossless):
- Used for simple trim operations when no end padding is needed
- Preserves original video/audio quality
- Completes in seconds
-
Re-encode (slower, with quality settings):
- Required for delays (adding black frames/silence at start)
- Required for end padding to match all videos to same duration
- Uses ultrafast preset with CRF 23 for good quality/speed balance
- Audio encoded as AAC 128k
Duration Normalization
All output videos are automatically padded to the same duration:- Side-by-side playback comparison
- Creating synchronized multi-view exports
- Frame-accurate alignment
Silent Video Handling
The function automatically detects videos without audio streams and processes them correctly:- No audio delay/padding filters applied
- Only video stream is processed
- Prevents ffmpeg errors from missing audio
Example Workflow
Error Handling
The function includes robust error handling:- Missing files: Logged as warnings, processing continues with remaining files
- Stream copy failures: Automatically falls back to re-encode
- ffmpeg errors: Logged with full error messages for debugging
- Duration detection failures: Skips problematic files with error logs
Performance Considerations
- Stream copy operations: Near-instant (seconds for large files)
- Re-encoding: Depends on video length and system performance
- 1080p 60fps: ~0.5-2x realtime with ultrafast preset
- 4K 30fps: ~0.3-1x realtime with ultrafast preset
- Memory usage: Minimal (ffmpeg streams data)
- Disk usage: Roughly same as original files (CRF 23 is visually lossless)
Logging
The function provides detailed logging:logging.DEBUG level for detailed ffmpeg command inspection.