Skip to main content

Sync Offset Tool

A command‑line utility to measure audio sync offsets between two MKV files. It extracts audio tracks, cross‑correlates them, and reports the best alignment offset in milliseconds. As of v1.2.0, the tool also detects and reports MKV container audio delays and calculates the effective offset including those delays.

Features

  • 🎵 Audio extraction: Uses ffmpeg to pull raw PCM samples directly from MKV audio tracks.
  • ⚡ Fast correlation: Defaults to FFT‑based cross‑correlation for speed.
  • 🧮 Direct correlation option: More precise but slower.
  • ⏱ Runtime reporting: Displays the exact runtime of both MKV files (hh:mm:ss.mmm).
  • 🎬 FPS reporting: Displays the frames per second of the primary video stream in both files.
  • ⏳ Container delay detection: Reports per‑track MKV audio delay metadata (start_time).
  • 📐 Effective offset reporting: Combines raw correlation offset with container delays for a net sync difference.
  • 📌 Version reporting: --version option shows the current tool version.

Requirements

  • Python 3.8+
  • NumPy
  • SciPy
  • FFmpeg installed and available in your PATH

Usage

./sync_offset.py original.mkv async.mkv [lang1] [lang2] [duration_seconds] [start_seconds] [method]

Arguments:

  • original.mkv — reference file
  • async.mkv — file suspected of being out of sync
  • lang1 — language code for original track (default: eng)
  • lang2 — language code for async track (default: eng)
  • duration_seconds — slice length to analyze (default: 120)
  • start_seconds — offset into the file to start slice (default: 0)
  • method — correlation method: fft (default) or direct

Options:

  • --version — display the tool’s version string

Examples

Run FFT correlation on a 120‑second slice from the start, English vs English (default):

./sync_offset.py movie1.mkv movie2.mkv

Analyze a German audio track against an English one for 90 s:

./sync_offset.py movie1.mkv movie2.mkv deu eng 90

Run FFT correlation on a long 300‑second slice starting at 600 s:

./sync_offset.py movie1.mkv movie2.mkv eng eng 300 600 fft

Compare 10‑second slices starting at 320 s using direct correlation:

./sync_offset.py movie1.mkv movie2.mkv eng eng 10 320 direct

Show version:

./sync_offset.py --version

Output

  • Runtime (original): hh:mm:ss.mmm | FPS: xx.xxx fps
  • Runtime (async): hh:mm:ss.mmm | FPS: xx.xxx fps
  • Container delay (original track): Reported in milliseconds.
  • Container delay (async track): Reported in milliseconds.
  • Best alignment offset (raw): Correlation‑based offset in milliseconds.
  • Peak correlation strength: Value between 0 and 1 indicating match quality.
  • Effective offset (including container delays): Net sync difference after accounting for MKV metadata delays.

Delay Detection

The tool detects and reports four flavours of MKV container audio delay:

  1. Stream start_time — audio begins at non‑zero time.
  2. codec_delay — encoder padding (AAC, Opus).
  3. Tags — delay_relative_to_video or delay.
  4. Packet timestamp offsets — first audio packet shifted relative to video (first_packet_pts).

Example Output

When comparing English vs. Turkish tracks in a remuxed MKV:

Container delay (original track): 0.00 ms
Container delay (async track) [first_packet_pts]: 9260.00 ms
Best alignment offset (raw): 9261.08 ms
Peak correlation strength: 0.0358
Effective offset (including container delays): 1.08 ms

This shows:

  • The Turkish track starts 9260 ms later (detected via first packet PTS).
  • Raw waveform correlation also found ~9261 ms.
  • Effective offset collapses to ~1 ms after subtracting the container delay.

Interpreting correlation strength

  • > 0.80 → Excellent match, high confidence in the offset measurement
  • 0.50 – 0.80 → Moderate match, offset is usable but less certain
  • < 0.50 → Weak match, results may be unreliable (consider shorter slices or different segments)

Tips for Better Results

  • Choose slices with dialogue, sharp sounds, or clear audio events — they produce stronger correlations than background noise or music alone.
  • Use shorter slices (10–60 s) if correlation strength is weak; smaller segments often align more clearly.
  • Try analyzing different parts of the file (e.g., start, middle, end) to confirm consistency of offsets.

Notes

  • Use FFT mode for long slices — it’s much faster.
  • Use Direct mode only for short slices or when you need maximum precision.
  • The script will refuse to run direct mode on slices >60 s without confirmation.
  • On interruption, the worker process is terminated immediately — no orphan processes left behind.
  • Container delays are reported directly from MKV metadata and may explain sync differences even before correlation.

Troubleshooting

  • No audio track found: Check the language codes (eng, deu, fra, etc.) with ffprobe.
  • FFmpeg errors: Ensure ffmpeg and ffprobe are installed and accessible in your PATH.
  • Negative offsets: A negative offset means the async file is ahead of the original.
  • Performance: Direct mode is O(n²). For slices longer than ~30 s, prefer FFT mode.
  • Effective offset mismatch: If raw offset and container delays differ, the MKV metadata may already compensate for sync.

License

MIT License

Metadata

Release files for sync-offset-tool 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sync-offset-tool 1.3.0
File Size Uploaded
sync_offset_tool-1.3.0.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sync-offset-tool 1.3.0
File Interpreter ABI Platform
sync_offset_tool-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.4 kB

Release files / sync_offset_tool-1.3.0.tar.gz

Download URL sync_offset_tool-1.3.0.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6945cf3fc0ea4b05a741b8081e7a5d9c582a87df0ace54cbe4bcc433ed9f2dcf
BLAKE2b-256 checksum
How to use checksums
79867b30fec4dc8c4f59219ee15c3d969b6de0868e5db8cb8f88fd73af25cfd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / sync_offset_tool-1.3.0-py3-none-any.whl

Download URL sync_offset_tool-1.3.0-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ba01e1f230921124f5419dcf451ddcb09124fc0c2fe971f04960c7410fe9ad9
BLAKE2b-256 checksum
How to use checksums
a27076891e193e2b716619c5e9ae16233bcd5ded64993ad3441dc4aea16670eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.1

2 release files

1.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page