feat: improve audio stream selection and caching

This commit is contained in:
Honney OC committed 2026-09-14 02:02:26 +00:00
1 parent 4cde4a7332
commit 2aa6d03810
4 files changed
+86 -29

No files matched your search

+39 -10
View File
@@ -297,7 +297,8 @@ class VerifyWindow(QtWidgets.QMainWindow):
def __init__(self, video_path: str, wav_a_path: str, wav_b_path: str,
sr: int = 48000, lag_sec: float = 0.0,
mode: str = "short", out_dir: str = "output",
temp_dir: str | None = None, volume: int = 70):
temp_dir: str | None = None, volume: int = 70,
cache_key_a: str | None = None, cache_key_b: str | None = None):
super().__init__()
self.video_path = video_path
self.wav_a_path = wav_a_path
@@ -325,9 +326,9 @@ class VerifyWindow(QtWidgets.QMainWindow):
# Load audio
print(f"Loading audio A: {wav_a_path}")
self.a_orig, _ = cached_load_wav(wav_a_path, target_sr=sr, _loader=load_wav)
self.a_orig, _ = cached_load_wav(wav_a_path, target_sr=sr, _loader=load_wav, _cache_key=cache_key_a)
print(f"Loading audio B: {wav_b_path}")
self.b_orig, _ = cached_load_wav(wav_b_path, target_sr=sr, _loader=load_wav)
self.b_orig, _ = cached_load_wav(wav_b_path, target_sr=sr, _loader=load_wav, _cache_key=cache_key_b)
# Apply lag alignment
self.a_aligned, self.b_aligned = apply_lag(self.a_orig, self.b_orig, self.lag_samples)
@@ -688,18 +689,44 @@ def main():
print("ERROR: synced MKV needs at least 2 audio streams (A + B)", file=sys.stderr)
sys.exit(1)
# Determine which streams to extract from synced MKV
# A's streams are first, B's delayed streams follow
video_a = ext.get("file_a", "")
a_streams_in = probe_audio_streams(video_a) if os.path.exists(video_a) else []
n_a = len(a_streams_in)
sel_a = ext.get("audio_stream_a", 0) or 0
sel_b = ext.get("audio_stream_b", 0) or 0
sel_a = int(sel_a)
sel_b = int(sel_b)
# Output indices: A's selected stream stays at sel_a, B's becomes n_a + sel_b
out_idx_a = sel_a
out_idx_b = n_a + sel_b
print(f"A has {n_a} streams, selected [{sel_a}] -> output [{out_idx_a}]")
print(f"B selected [{sel_b}] -> output [{out_idx_b}]")
if out_idx_a >= len(streams) or out_idx_b >= len(streams):
print(f"ERROR: computed stream indices [{out_idx_a}, {out_idx_b}] out of range "
f"(file has {len(streams)} streams)", file=sys.stderr)
sys.exit(1)
temp_dir = tempfile.mkdtemp(prefix="verify_sync_")
wav_a = os.path.join(temp_dir, "audio_a.wav")
wav_b = os.path.join(temp_dir, "audio_b.wav")
print(f"Extracting audio stream 0 (A) to {wav_a}...")
if not extract_audio_from_mkv(synced, 0, wav_a, sr):
print("ERROR: failed to extract audio stream 0", file=sys.stderr)
# Stable cache keys based on source MKV + stream index (not temp file path)
import hashlib
_mkv_mtime = os.path.getmtime(synced)
cache_key_a = hashlib.sha256(f"{synced}|{out_idx_a}|{sr}|{_mkv_mtime:.6f}".encode()).hexdigest()[:16]
cache_key_b = hashlib.sha256(f"{synced}|{out_idx_b}|{sr}|{_mkv_mtime:.6f}".encode()).hexdigest()[:16]
print(f"Extracting audio stream {out_idx_a} (A) to {wav_a}...")
if not extract_audio_from_mkv(synced, out_idx_a, wav_a, sr):
print(f"ERROR: failed to extract audio stream {out_idx_a}", file=sys.stderr)
sys.exit(1)
print(f"Extracting audio stream 1 (B) to {wav_b}...")
if not extract_audio_from_mkv(synced, 1, wav_b, sr):
print("ERROR: failed to extract audio stream 1", file=sys.stderr)
print(f"Extracting audio stream {out_idx_b} (B) to {wav_b}...")
if not extract_audio_from_mkv(synced, out_idx_b, wav_b, sr):
print(f"ERROR: failed to extract audio stream {out_idx_b}", file=sys.stderr)
sys.exit(1)
video_path = synced
@@ -737,6 +764,8 @@ def main():
else:
print(f"WARNING: extracted video not found ({extracted_video}), using original", file=sys.stderr)
temp_dir = None
cache_key_a = None
cache_key_b = None
print(f"Mode: {args.mode}")
print(f"Video: {video_path}")
@@ -750,7 +779,7 @@ def main():
w = VerifyWindow(video_path, wav_a, wav_b, sr=sr, lag_sec=lag_sec,
mode=args.mode, out_dir=out_dir, temp_dir=temp_dir,
volume=volume)
volume=volume, cache_key_a=cache_key_a, cache_key_b=cache_key_b)
w.show()
sys.exit(app.exec())