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
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test-only replacements for op/curl; no network or real credentials."""
|
||||
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
if Path(sys.argv[0]).name == "op":
|
||||
print("test-api-key")
|
||||
sys.exit(0)
|
||||
|
||||
args = sys.argv[1:]
|
||||
request = json.loads(Path(args[args.index("--data-binary") + 1][1:]).read_text())
|
||||
output = Path(args[args.index("-o") + 1])
|
||||
index = int(output.stem.split("-")[1])
|
||||
manifest = json.loads((output.parent / "chunks.json").read_text())
|
||||
chunk = manifest[index]
|
||||
assert request["model"] == "microsoft/mai-transcribe-2"
|
||||
assert request["input_audio"]["format"] == "mp3"
|
||||
if len(manifest) > 1:
|
||||
assert request["response_format"] == "verbose_json"
|
||||
assert request["timestamp_granularities"] == ["segment", "word"]
|
||||
|
||||
audio = output.parent / "uploaded.mp3"
|
||||
audio.write_bytes(base64.b64decode(request["input_audio"]["data"], validate=True))
|
||||
info = json.loads(subprocess.check_output([
|
||||
"ffprobe", "-v", "error", "-show_entries",
|
||||
"format=duration:stream=codec_name,sample_rate,channels", "-of", "json", str(audio),
|
||||
], text=True))
|
||||
assert info["streams"][0] == {"codec_name": "mp3", "sample_rate": "16000", "channels": 1}
|
||||
assert float(info["format"]["duration"]) <= 60
|
||||
assert abs(float(info["format"]["duration"]) - (chunk["end"] - chunk["start"])) < 0.1
|
||||
with open(os.environ["TRANSCRIBE_TEST_LOG"], "a") as log:
|
||||
log.write(json.dumps({"index": index, "chunk": chunk, "staging": str(output.parent)}) + "\n")
|
||||
|
||||
mode = os.environ.get("TRANSCRIBE_TEST_MODE", "ok")
|
||||
if index == 1:
|
||||
if mode == "http-error":
|
||||
output.write_text('{"error":{"message":"simulated timeout"}}')
|
||||
sys.exit(22)
|
||||
if mode == "bad-json":
|
||||
output.write_text("not json")
|
||||
sys.exit(0)
|
||||
if mode == "api-error":
|
||||
output.write_text('{"error":{"message":"simulated API error"},"text":""}')
|
||||
sys.exit(0)
|
||||
if mode == "missing-words":
|
||||
output.write_text('{"text":"speech without timestamps"}')
|
||||
sys.exit(0)
|
||||
|
||||
if "response_format" not in request:
|
||||
response = {"text": "Provider's original formatting.\nNext line."}
|
||||
else:
|
||||
words = [
|
||||
{"word": text, "start": start - chunk["start"], "end": end - chunk["start"]}
|
||||
for text, start, end in [
|
||||
("Hello.", 1, 1.4), ("boundary", 56.7, 57.1),
|
||||
("continues.", 57.4, 57.8), ("Last.", 65, 65.4),
|
||||
] if start >= chunk["start"] and end <= chunk["end"]
|
||||
]
|
||||
response = {"text": " ".join(w["word"] for w in words), "words": words}
|
||||
output.write_text(json.dumps(response))
|
||||
Reference in new issue
Block a user