1
$\begingroup$

Say we have two audio tracks of the same movie but from different sources. The length of the animated logos at the beginning is different, so the actual movie audio starts slightly earlier on one track comparing to the other. The quality and volume of the audio can be different too.

How to find out the exact time offset? Is there a tool/command (e.g. sox) which simply takes two files as arguments and outputs the time offset (if the audio is found to be substantially the same)?

This and this answers suggest that cross correlation is the way to go, but I am looking for a more practical, ready-to-use solution, preferably a one-liner. Is this still too much to ask in 2023?

$\endgroup$
4
  • 1
    $\begingroup$ Cross-correlation is a ready to use solution, and in most cases, a one-liner. What more do you want? $\endgroup$
    – Jdip
    Commented Jan 15, 2023 at 14:51
  • $\begingroup$ @Jdip An example of that one-liner, preferably for Linux. Thanks. $\endgroup$
    – Greendrake
    Commented Jan 15, 2023 at 15:01
  • $\begingroup$ Most Digital Audio Workstations can do this. The tricky bit here is that it typically requires a bit of sample rate conversion to sync up the two different sample clocks. In other words: the time offset will NOT be constant but slowly drift over the length of the track. $\endgroup$
    – Hilmar
    Commented Jan 15, 2023 at 15:38
  • $\begingroup$ @Hilmar Alright, let's simplify the input then: the two tracks have the same sample rate, and the time offset is constant. Let's even say the 2nd track was made by simply inserting some silence at the beginning of the 1st and slightly adjusting the volume. $\endgroup$
    – Greendrake
    Commented Jan 15, 2023 at 15:49

1 Answer 1

3
$\begingroup$

Cross-correlation is definitely the answer. And it is basically a one-liner in any common DSP package (MATLAB, Python, Julia...).

To make it more practical for your particular scenario, I would suggest using shorter pieces of the audio tracks rather than the full tracks, otherwise you'll be waiting a loooong time!

Steps:

  • Take a short chunk of one of the tracks as the piece you want to look for in the other track
  • Take a chunk of the other track centered around the center time of the first one, but longer than the first (up to the longest delay you reasonably expect between the tracks in either direction)
  • Cross-correlate the two (e.g., with xcorr in MATLAB), and find the point the max occurs at. This will give you the time offset in samples.

If you want a more precise measurement, I'd suggest repeating the above several times at different points in the long track, and averaging the results.

Edit

Here's an example in Python (largely borrowed from this answer to save time):

import numpy as np
from scipy.signal import correlate
from scipy.signal import correlation_lags

# Make 2 vectors to test, 1 being a shifted version of the other
x = np.asarray([1,2,3,4])
y = np.asarray([.5,1,2,3])

# Cross-correlate 
correlation = correlate(x, y, 'full')

# Get the lag vector that corresponds to the correlation vector
lags = correlation_lags(x.size,  y.size, mode="full")

# Find the lag at the peak of the correlation
lag = lags[np.argmax(correlation)]
print(lag)
$\endgroup$
2
  • $\begingroup$ Thank you, this helps. I am not handy with DSP packages though. Could you give a Python example please? $\endgroup$
    – Greendrake
    Commented Jan 17, 2023 at 3:30
  • $\begingroup$ Sure, I added an example. Hope it helps! $\endgroup$
    – Gillespie
    Commented Jan 17, 2023 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.