Stereo Sync Stack
Audio-sync, drift-correct, align, and merge two camera videos into a single stereoscopic output.
This project takes a left-eye video and a right-eye video, automatically synchronizes them by their audio, optionally checks and corrects for drift, optionally estimates stereo alignment offsets, and renders a final output such as:
- Side-by-side (SBS) stereo
- Red/Cyan Anaglyph stereo
It is designed for fast real-world stereo workflows where two separate cameras were started at slightly different times, may drift slightly over the duration of the clip, and may need small image alignment adjustments.
Why this exists
If you record stereo video with two separate cameras, you usually run into some combination of the following problems:
- The cameras do not start at the exact same time
- One camera may run slightly longer or shorter than the other
- Small clock drift can accumulate over several minutes
- The views may have a slight vertical misalignment
- The views may have a small horizontal offset
- The right and left views may need a tiny roll correction
- You may want multiple output formats from the same source pair
This project solves those problems in a single Python + FFmpeg workflow.
Core features
1. Audio-based start synchronization
The script analyzes the beginning of both videos and determines which camera started earlier.
It then trims the correct side so the videos begin in sync.
2. End-of-clip drift analysis
After the initial start sync is established, the script analyzes a later section of the clip to check whether the cameras drifted apart over time.
If needed, it computes a small speed correction for the right stream.
3. Stereo alignment controls
After the videos are time-synced, the script can also apply geometric alignment to the right-eye view:
- Manual X shift
- Manual Y shift
- Manual roll rotation
- Automatic vertical alignment estimation
- Optional automatic horizontal alignment estimation
4. GPU-accelerated encoding
The render path uses NVIDIA NVENC by default for fast encoding.
5. Multiple stereo output modes
Current output layouts supported by the main script:
sbs — side-by-side stereo
anaglyph — red/cyan-style single-frame 3D
High-level workflow
The overall pipeline is:
- Probe both input files with
ffprobe
- Determine durations and source FPS
- Extract low-rate mono audio for analysis
- Find the start offset using the original long-window envelope correlation method
- Trim the earlier-starting stream
- Probe a late section to estimate drift by the end
- If drift is significant, compute a tiny speed correction for the right stream
- Optionally analyze a synced section for stereo alignment offsets
- Apply the requested or detected alignment to the right image
- Render the final stereo output with FFmpeg
Project files
Typical layout:
Stereo Sync Stack/
├── stereo_sync_stack_v9.py
├── stereo_alignment.py
└── left.mp4 / right.mp4
stereo_sync_stack_v9.py
Main CLI tool.
Responsibilities:
- input probing
- audio sync
- drift analysis
- option parsing
- final FFmpeg render command construction
stereo_alignment.py
Alignment helper module.
Responsibilities:
- synced frame extraction
- grayscale preprocessing
- center crop handling
- phase-correlation-based translation estimation
- alignment analysis reporting
Requirements
Python
Recommended: Python 3.10+
Python packages
Install the required Python dependencies:
pip install numpy scipy opencv-python
FFmpeg
You must have both:
installed and available on your system PATH.
To verify:
ffmpeg -version
ffprobe -version
NVIDIA GPU (optional but recommended)
The default render path uses:
So you need:
- a compatible NVIDIA GPU
- a recent driver
- an FFmpeg build with NVENC enabled
If AV1 NVENC is unavailable on your system, you can modify the script to use:
instead.
Installation
- Clone or download the repository
- Place both Python files in the same folder
- Install the Python dependencies
- Make sure FFmpeg is on
PATH
Example:
git clone <your-repo-url>
cd <your-repo-folder>
pip install numpy scipy opencv-python
Basic usage
The script has two modes:
Important
The default mode is:
analyze
That means if you do not specify --mode render, the script will print analysis results but will not create an output file.
Analyze only
Use this to inspect sync and drift without writing a video file:
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode analyze
You will see output like:
- left duration
- right duration
- start offset
- start confidence
- end residual after sync
- drift estimate
- trim values
- setpts factor
- whether drift correction is needed
If stereo auto-alignment is enabled, you will also see:
- suggested right-eye X shift
- suggested right-eye Y shift
- response/confidence statistics
Render a side-by-side stereo file
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode render --output output.mkv
This will:
- sync the videos by audio
- optionally correct drift if the detected drift exceeds the built-in threshold
- render a side-by-side output
- use the left audio by default
Render an anaglyph stereo file
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode render --output anaglyph3d.mkv --stereo-output anaglyph
This converts the internally constructed side-by-side stereo pair into an anaglyph output.
Default anaglyph mode:
arcd
which is typically the best general-purpose red/cyan choice.
Quick examples
Standard SBS output
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode render --output sbs_output.mkv
Analyze only with auto vertical alignment
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode analyze --auto-align-vertical
Render with auto vertical alignment
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode render --output aligned_output.mkv --auto-align-vertical
Render with auto vertical and auto horizontal alignment
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode render --output aligned_output.mkv --auto-align-vertical --auto-align-horizontal
Render with manual Y offset only
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode render --output output.mkv --manual-right-shift-y -3
Render with manual Y offset and roll correction
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode render --output output.mkv --manual-right-shift-y -3 --manual-right-rotate-deg 0.15
Render anaglyph with auto vertical alignment
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode render --output anaglyph3d.mkv --stereo-output anaglyph --auto-align-vertical
Render using only start trim and no drift correction
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode render --output output.mkv --disable-drift-correction
Force a known-good trim from the right
python stereo_sync_stack_v9.py left.mp4 right.mp4 --mode render --output output.mkv --force-right-trim 2.055
How synchronization works
Start sync
The script deliberately reuses the original long-window start-sync approach from the earlier working version of the project.
It:
- extracts low-rate mono audio from the beginning of both videos
- converts the signal to an envelope-style feature
- cross-correlates the full envelopes
- finds the best offset
This approach was kept because it produced more reliable start-lock behavior than a shorter-window limited-lag method.
Drift analysis
After the start trim is determined, the script analyzes a late portion of the clip using a shorter-window residual check.
It compares the already-synced streams near the end and asks:
after trimming the beginning, are the videos still aligned near the end?
If not, it computes a small correction factor for the right side.
Drift correction threshold
At present, drift correction is automatically enabled when:
abs(drift_seconds) >= 0.100
That means the script will only apply drift correction if the detected end drift is at least about 100 ms.
This avoids unnecessary micro-corrections when the clip is already close enough.
How stereo alignment works
The stereo alignment stage happens after time synchronization.
This is important because stereo image alignment is only meaningful when both frames represent the same moment in time.
Current automatic alignment method
The alignment module:
- picks a synced time section
- extracts matching left/right frames from that section
- center-crops the images
- converts them to grayscale
- downsizes them for analysis
- runs phase correlation to estimate translation
- aggregates the results across multiple samples
What is estimated automatically
Currently supported automatic estimation:
- vertical translation of the right eye
- optional horizontal translation of the right eye
What is manual only
Currently manual only:
- roll rotation (
--manual-right-rotate-deg)
Why vertical alignment matters most
For stereo comfort, vertical disparity is usually the most annoying mismatch.
A small vertical mismatch can be surprisingly easy to miss while watching the scene itself, because your eyes compensate for it.
However, it often becomes obvious when:
- you pause the video
- you look at player UI text
- you look at menus overlaid on top of the stereo image
This is why the auto vertical alignment option is especially useful.
Why auto horizontal alignment is less reliable
Horizontal disparity in stereo footage is strongly tied to depth.
That means there is no single perfect horizontal shift that aligns everything in the scene equally well.
For this reason:
- vertical auto-align is generally trustworthy
- horizontal auto-align can still be useful
- but horizontal auto-align should be treated more cautiously
Command-line options
Below is a detailed reference for the current script options.
Positional arguments
left_video
Path to the left-eye input video.
right_video
Path to the right-eye input video.
General mode selection
--mode {analyze,render}
Select whether to only print analysis or actually write an output file.
Default:
analyze
--output OUTPUT_PATH
Required when --mode render is used.
Sync analysis options
--start-analyze-seconds FLOAT
How much of the beginning of the video to analyze for the original start lock.
Default:
300.0
This is intentionally long because the original method proved more reliable when it could examine a large audio window.
--drift-probe-window FLOAT
How much audio to analyze in the late-window drift check.
Default:
30.0
--end-margin FLOAT
How far before the nominal end of the synced overlap to place the drift analysis window.
Default:
15.0
--sample-rate INT
Audio sample rate used for analysis-only extraction.
Default:
2000
Low analysis sample rates are intentional and help keep the sync calculations fast.
--max-lag FLOAT
Maximum lag allowed when estimating late-window residual drift.
Default:
2.0
Output sizing and cadence
--height INT
Height for each eye view before stacking.
Default:
1080
For SBS, each side is scaled to this height before horizontal stacking.
--fps INT
Optional output frame rate override.
If not provided, the script preserves the source cadence using the left input FPS.
Encoding options
--preset PRESET
Encoder preset.
Current default in v9:
slow
--crf INT
Present as an argument in the script, but note that the current AV1 NVENC command path is primarily controlled by:
So --crf is not the main quality knob in the current AV1 NVENC path.
--use-right-audio
Mix the right audio into the output instead of using only the left audio.
Default behavior without this flag:
Manual sync overrides
--force-left-trim FLOAT
Force the script to trim this many seconds from the left start.
--force-right-trim FLOAT
Force the script to trim this many seconds from the right start.
Only use one of these two at a time.
--disable-drift-correction
Disables the automatic drift correction and applies only the start trim.
Useful when:
- the beginning is clearly correct