77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
from pathlib import Path
|
|
import subprocess
|
|
import numpy as np
|
|
from scipy.signal import correlate
|
|
import tempfile
|
|
import soundfile as sf
|
|
import matplotlib.pyplot as plt
|
|
|
|
def extract_audio(file: Path, duration: float):
|
|
tmp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
|
|
|
subprocess.run([
|
|
"ffmpeg",
|
|
"-y",
|
|
"-i", str(file),
|
|
"-map", "0:a:0", # first audio stream
|
|
"-ac", "1", # mono
|
|
"-ar", "44100", # fixed sample rate
|
|
"-t", str(duration), # limit duration
|
|
tmp.name
|
|
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
data, sr = sf.read(tmp.name)
|
|
return data, sr
|
|
|
|
|
|
def get_audio_offset(file1: Path, file2: Path, important_time: float):
|
|
sig1, sr1 = extract_audio(file1, important_time)
|
|
sig2, sr2 = extract_audio(file2, important_time)
|
|
|
|
if sr1 != sr2:
|
|
raise ValueError("Sample rates do not match")
|
|
|
|
# normalize signals
|
|
sig1 = sig1 - np.mean(sig1)
|
|
sig2 = sig2 - np.mean(sig2)
|
|
|
|
corr = correlate(sig1, sig2, mode="full")
|
|
lag = np.argmax(corr) - (len(sig2) - 1)
|
|
|
|
offset_seconds = lag / sr1
|
|
return offset_seconds
|
|
|
|
def view_audio_correlation(file1: Path, file2: Path, important_time: float):
|
|
sig1, sr1 = extract_audio(file1, important_time)
|
|
sig2, sr2 = extract_audio(file2, important_time)
|
|
|
|
if sr1 != sr2:
|
|
raise ValueError("Sample rates differ")
|
|
|
|
sig1 = sig1 - np.mean(sig1)
|
|
sig2 = sig2 - np.mean(sig2)
|
|
|
|
corr = correlate(sig1, sig2, mode="full")
|
|
|
|
lags = np.arange(-len(sig2)+1, len(sig1))
|
|
lag_seconds = lags / sr1
|
|
|
|
offset_index = np.argmax(corr)
|
|
offset_seconds = lag_seconds[offset_index]
|
|
|
|
plt.figure(figsize=(10,5))
|
|
plt.plot(lag_seconds, corr)
|
|
plt.axvline(offset_seconds, linestyle="--")
|
|
plt.title(f"Audio Cross-Correlation (offset ≈ {offset_seconds:.3f}s)")
|
|
plt.xlabel("Lag (seconds)")
|
|
plt.ylabel("Correlation")
|
|
plt.show()
|
|
|
|
return offset_seconds
|
|
|
|
ger = "/home/honney/mount/Media/tmp/serien/Maid Sama All/Maid Sama/S01/07.mkv"
|
|
jap = "/home/honney/mount/Media/tmp/serien/Maid Sama All/Kaichou wa Maid-sama!/S01/07.mkv"
|
|
|
|
offset = view_audio_correlation(Path(ger), Path(jap), 90)
|
|
print(offset)
|