feat: transcribe long recordings in overlapping chunks
Sending entire recordings in one request can exceed provider limits. Decode audio once to a shared PCM sample clock and split uploads at a configurable maximum of 1 to 30 minutes, defaulting to 30 minutes. Prefer nearby pauses and include six seconds of overlap to protect words at audio cuts while keeping each encoded MP3 within the duration cap. Request word timestamps for chunked text and SRT output. Match overlapping words and times to retain each passage once, warn when a join falls back to timestamps, and generate SRT cues on the original recording timeline. Reuse subtitle timestamp validation without reordering regressing words. Only emit output after every request and conversion succeeds. Document the Python dependency, temporary storage, boundary limitations, and lack of automatic retry or resume. Keep single-request text formatting. Test Plan: - python3 -B -m unittest -v: all 29 tests passed, including mocked provider failures, native MP3 payload checks, boundary stitching, and SRT timing. - bash -n transcribe and shellcheck transcribe: passed. - git diff --cached --check: passed. - Locally decoded and encoded the supplied 74-minute WebM into three MP3 chunks; ffprobe confirmed each duration was below 30 minutes. - Live transcription was not verified: the configured 1Password credential was unavailable.
This commit is contained in:
1 parent
300203d33f
commit
a5c535cfb9
7 files changed
+635
-57
No files matched your search
+34
-17
@@ -25,27 +25,21 @@ def timestamp(seconds):
|
||||
return f"{hours:02}:{minutes:02}:{seconds:02},{milliseconds:03}"
|
||||
|
||||
|
||||
def convert(response):
|
||||
def normalized_words(response, purpose="SRT output"):
|
||||
"""Validate word timing and preserve transcript order despite regressions."""
|
||||
if not isinstance(response, dict):
|
||||
raise ValueError("Transcription response must be a JSON object")
|
||||
if response.get("error"):
|
||||
raise ValueError(f"Transcription failed: {response['error']}")
|
||||
text = response.get("text", "")
|
||||
if not isinstance(text, str):
|
||||
raise ValueError("Transcription response must contain text as a string")
|
||||
words = response.get("words")
|
||||
if not isinstance(words, list) or (not words and response.get("text", "").strip()):
|
||||
raise ValueError("SRT output requires word timestamps; the API returned none")
|
||||
|
||||
cues = []
|
||||
current = []
|
||||
missing = f"{purpose} requires word timestamps; the API returned none"
|
||||
if not isinstance(words, list):
|
||||
raise ValueError(missing)
|
||||
result = []
|
||||
previous_start = -1
|
||||
|
||||
def flush():
|
||||
if current:
|
||||
text = " ".join(word["word"] for word in current)
|
||||
start = current[0]["start"]
|
||||
end = min(max(word["end"] for word in current), start + MAX_DURATION)
|
||||
# Avoid zero-length cues after rounding very short word durations.
|
||||
end = max(end, math.floor(start * 1000 + 0.5) / 1000 + 0.001)
|
||||
cues.append((start, end, "\n".join(lines(text))))
|
||||
current.clear()
|
||||
|
||||
for item in words:
|
||||
if not isinstance(item, dict) or not isinstance(item.get("word"), str):
|
||||
raise ValueError("Invalid subtitle word: expected word text")
|
||||
@@ -70,6 +64,29 @@ def convert(response):
|
||||
end = max(end, start)
|
||||
word.update(start=start, end=end)
|
||||
previous_start = start
|
||||
result.append(word)
|
||||
if not result and text.strip():
|
||||
raise ValueError(missing)
|
||||
return result
|
||||
|
||||
|
||||
def convert(response):
|
||||
words = normalized_words(response)
|
||||
cues = []
|
||||
current = []
|
||||
|
||||
def flush():
|
||||
if current:
|
||||
text = " ".join(word["word"] for word in current)
|
||||
start = current[0]["start"]
|
||||
end = min(max(word["end"] for word in current), start + MAX_DURATION)
|
||||
# Avoid zero-length cues after rounding very short word durations.
|
||||
end = max(end, math.floor(start * 1000 + 0.5) / 1000 + 0.001)
|
||||
cues.append((start, end, "\n".join(lines(text))))
|
||||
current.clear()
|
||||
|
||||
for word in words:
|
||||
start, end = word["start"], word["end"]
|
||||
if len(lines(word["word"])) > 2:
|
||||
raise ValueError("Invalid subtitle word: text exceeds two lines")
|
||||
if current:
|
||||
|
||||
Reference in new issue
Block a user