Files
hpr-intro-stripper/hpr_stripper/trim.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

73 lines
2.9 KiB
Python

"""
Produces a trimmed copy of one episode's audio. Never touches the
original -- always writes to a separate output directory, atomically
(temp path, renamed into place only on success).
Refuses by default for No-Derivatives-licensed episodes and for anything
the detector flagged (see docs/DESIGN.md, "License handling" and "Fit
with HPR's own ethos & principles" -- the intro is also how HPR satisfies
its CC-BY-SA attribution requirement, so trimmed output always carries the
episode's license/host/date/number forward rather than silently dropping
it).
"""
import json
import os
import subprocess
FADE_SECONDS = 0.15
def trim_episode(src_path, dst_path, intro_end, outro_start, duration, fade=FADE_SECONDS):
start = intro_end or 0.0
end = outro_start if outro_start is not None else duration
length = max(0.1, end - start)
tmp_path = dst_path + ".tmp" + os.path.splitext(dst_path)[1]
cmd = [
"ffmpeg", "-v", "error", "-y",
"-ss", str(start), "-i", src_path, "-t", str(length),
"-af", f"afade=t=in:st=0:d={fade},afade=t=out:st={max(0, length - fade)}:d={fade}",
tmp_path,
]
subprocess.run(cmd, check=True)
os.replace(tmp_path, dst_path)
return length
def write_sidecar(dst_path, episode_meta, detection_result):
"""A small JSON file carrying forward exactly what the license
requires downstream: host, episode number, date, license, plus which
detection method/confidence produced the cut, so trimmed output stays
self-documenting if it's ever shared separately from this tool."""
sidecar_path = os.path.splitext(dst_path)[0] + ".json"
payload = {
"episode": episode_meta.get("id"),
"title": episode_meta.get("title"),
"host": episode_meta.get("host"),
"date": episode_meta.get("date"),
"license": episode_meta.get("license"),
"license_long_name": episode_meta.get("license_long_name"),
"original_url": f"https://hackerpublicradio.org/eps/hpr{episode_meta.get('id'):04d}/index.html",
"intro_end": detection_result.get("intro_end"),
"intro_method": detection_result.get("intro_method"),
"outro_start": detection_result.get("outro_start"),
"outro_method": detection_result.get("outro_method"),
}
tmp_path = sidecar_path + ".tmp"
with open(tmp_path, "w") as f:
json.dump(payload, f, indent=2)
os.replace(tmp_path, sidecar_path)
return sidecar_path
def should_trim(detection_result, force=False):
"""Returns (ok: bool, reason: str)."""
if force:
return True, "forced"
if detection_result.get("license_flag"):
return False, detection_result["license_flag"]
if detection_result.get("intro_flag") or detection_result.get("outro_flag"):
return False, "flagged for manual review"
if detection_result.get("intro_method") == "none" or detection_result.get("outro_method") == "none":
return False, "no reliable cut point found"
return True, "ok"