Files
hpr-intro-stripper/hpr_stripper/audio.py
T
Lee Hanken 9917320640 hpr-intro-stripper: layered intro/outro cut-point detection and trimming
Fetches HPR's own metadata, audio, and official transcripts live; falls
back to local speech recognition (tiny model, matching HPR's own
hpr-get-and-transcribe.bash tooling) only when needed, and to
correlation against vendored reference clips or sibling episodes
otherwise. License-aware, resumable, and never guesses -- flags
anything it isn't confident about for manual review rather than
producing a plausible-looking wrong cut.
2026-11-22 10:36:07 +00:00

108 lines
3.9 KiB
Python

"""ffmpeg/ffprobe wrappers and the cross-correlation primitives."""
import subprocess
import numpy as np
from . import config
def duration_seconds(path):
out = subprocess.run(
["ffprobe", "-v", "error", "-show_entries", "format=duration",
"-of", "csv=p=0", path],
capture_output=True, check=True, text=True,
).stdout.strip()
return float(out)
def extract_clip(src, dst_wav, start, duration, sample_rate=16000):
cmd = ["ffmpeg", "-v", "error", "-y"]
if start > 0:
cmd += ["-ss", str(start)]
cmd += ["-i", src]
if duration is not None:
cmd += ["-t", str(duration)]
cmd += ["-ac", "1", "-ar", str(sample_rate), dst_wav]
subprocess.run(cmd, check=True)
def load_pcm_f32(src, start=0.0, duration=None, sample_rate=config.XCORR_SAMPLE_RATE):
cmd = ["ffmpeg", "-v", "error"]
if start > 0:
cmd += ["-ss", str(start)]
cmd += ["-i", src]
if duration is not None:
cmd += ["-t", str(duration)]
cmd += ["-ac", "1", "-ar", str(sample_rate), "-f", "f32le", "-"]
out = subprocess.run(cmd, capture_output=True, check=True).stdout
return np.frombuffer(out, dtype=np.float32)
def windowed_corr(a, b, win_sec=config.XCORR_WINDOW_SECONDS, sample_rate=config.XCORR_SAMPLE_RATE):
win = int(win_sec * sample_rate)
n = min(len(a), len(b)) // win
corrs = np.empty(n)
for i in range(n):
s = i * win
wa = a[s:s + win]
wb = b[s:s + win]
if wa.std() < 1e-6 or wb.std() < 1e-6:
corrs[i] = 0.0
continue
corrs[i] = np.corrcoef(wa, wb)[0, 1]
return corrs, win_sec
def leading_match_seconds(corrs, win_sec, threshold=config.XCORR_MATCH_THRESHOLD, tolerate_gap=1):
"""Length of the leading run of high correlation, tolerating up to
tolerate_gap consecutive low windows (encoder jitter / clicks)."""
run = 0
gap = 0
for c in corrs:
if c >= threshold:
run += 1
gap = 0
else:
gap += 1
if gap > tolerate_gap:
break
run += 1
return run * win_sec
def reference_prefix_match_seconds(episode_path, reference_path, probe_sec=60):
"""How long does the start of episode_path correlate with
reference_path (e.g. the vendored theme.flac)? See docs/DESIGN.md:
the real intro is theme+speech mixed additively (sox -m), not a
sequential splice, but correlation against the pure reference still
drops sharply once the reference's own contribution stops."""
a = load_pcm_f32(episode_path, 0, probe_sec)
b = load_pcm_f32(reference_path, 0, min(probe_sec, duration_seconds(reference_path)))
corrs, win = windowed_corr(a, b)
return leading_match_seconds(corrs, win)
def sibling_suffix_match_seconds(path_a, duration_a, path_b, duration_b, probe_sec=60):
"""Symmetric to reference_suffix_match_seconds, but between two
downloaded episodes rather than against a vendored reference clip --
the primary outro signal for eras with no vendored reference asset
yet (e.g. pre-2015)."""
a = load_pcm_f32(path_a, max(0, duration_a - probe_sec), probe_sec)
b = load_pcm_f32(path_b, max(0, duration_b - probe_sec), probe_sec)
m = min(len(a), len(b))
corrs, win = windowed_corr(a[-m:], b[-m:])
return leading_match_seconds(corrs[::-1], win)
def reference_suffix_match_seconds(episode_path, episode_duration, reference_path, probe_sec=60):
"""How long does the end of episode_path correlate with
reference_path (e.g. the vendored outro.flac, which is the complete
fixed outro clip, not just a music bed)?"""
ref_dur = duration_seconds(reference_path)
probe_sec = min(probe_sec, ref_dur)
a = load_pcm_f32(episode_path, max(0, episode_duration - probe_sec), probe_sec)
b = load_pcm_f32(reference_path, max(0, ref_dur - probe_sec), probe_sec)
m = min(len(a), len(b))
corrs, win = windowed_corr(a[-m:], b[-m:])
return leading_match_seconds(corrs[::-1], win)