Skip to main content

A command-line utility to measure audio sync offsets between MKV files

Project description

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. Tagsdelay_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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sync_offset_tool-1.3.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sync_offset_tool-1.3.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file sync_offset_tool-1.3.0.tar.gz.

File metadata

  • Download URL: sync_offset_tool-1.3.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for sync_offset_tool-1.3.0.tar.gz
Algorithm Hash digest
SHA256 6945cf3fc0ea4b05a741b8081e7a5d9c582a87df0ace54cbe4bcc433ed9f2dcf
MD5 495c8ad42546d6d5e167cd841205a380
BLAKE2b-256 79867b30fec4dc8c4f59219ee15c3d969b6de0868e5db8cb8f88fd73af25cfd8

See more details on using hashes here.

File details

Details for the file sync_offset_tool-1.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sync_offset_tool-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ba01e1f230921124f5419dcf451ddcb09124fc0c2fe971f04960c7410fe9ad9
MD5 3504e98eab0736701dbfca682d02337f
BLAKE2b-256 a27076891e193e2b716619c5e9ae16233bcd5ded64993ad3441dc4aea16670eb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page