Repository navigation
Transcribe long videos a window at a time - #182
Conversation
Videos longer than one window (2 min + 20 s) no longer keep the whole track as float32 for whisper. The loudness pass keeps only the curve, and each window is decoded with -ss/-t right before whisper needs it, cut at the quietest point near the plan. Short videos keep their samples from the loudness pass, as before. Windows are 2 minutes because faster-whisper's feature extraction peaks at about 1 MB per second of audio it's given.
A21-devs
left a comment
There was a problem hiding this comment.
Checked the plumbing: the last window reads up to max(duration, curve length), so a track that runs past the container isn't cut short. Each window's decode_pcm gets duration, so there's no ffprobe call per window. The cached-loudness path decodes windows the same way as a fresh run. A cancel during a window decode waits at most one 2 min decode (well under a second).
Two things.
1. The memory numbers in the description are Python-heap only. tracemalloc doesn't see CTranslate2's or CUDA's native allocations. I measured the real process peak working set (K32GetProcessMemoryInfo) with whisper small on CUDA:
| peak working set | |
|---|---|
| 60 s file (model + CUDA runtime, basically the floor) | 918 MB |
| 32 min file, this PR (16 windows) | 1061 MB |
| 32 min file, one call (old path) | 1920 MB |
So the part that grows with the video really is gone: 32 min of audio adds ~140 MB over the floor instead of ~1 GB, and that's the 2 min window's features. But nobody running this sees "165 MB". The description should say the 165 MB is what the audio handling costs on top of the loaded model, and give the process numbers.
2. Language detection now runs per window, inline.
| job.whisper_model, language=job.language, log=log, | ||
| on_progress=on_progress) | ||
| # the curve covers the whole track, which can run past the container's duration | ||
| windows = transcribe.plan_windows(db, job.hop, max(info.duration, len(db) * job.hop), |
There was a problem hiding this comment.
With language=None (the default, no --language), faster-whisper detects the language in every model.transcribe call, so a 3 h VOD now gets 90 separate detections instead of one. Each is made from the start of its own 2 min window. A window that's mostly music, game sound or a few shouted words is exactly where detection is shaky, and a wrong guess there gets that window transcribed (or translated) as another language. The single call detected once, from the first speech in the file. Cheap fix: in _run, when language is None, take info.language from the first window's transcribe() result and pass it to the rest.
Each window was its own transcribe() call, so with no --language the language was detected 90 times on a 3 h video, each time from the start of a 2 minute window that may be music or a few shouted words.
|
Both addressed.
|
A21-devs
left a comment
There was a problem hiding this comment.
Second pass over 6720e69 and the updated description.
- Language: fixed. It locks only after a window produced words, so a silent intro window's guess isn't kept. It only applies with more than one window, and
languageis a local in_run, so the CUDA→CPU retry starts again from the caller's value (None) and detects anew instead of carrying a guess from the failed attempt. The tests cover the silent first window, a given language, and the one-window path. - Memory: the description now separates the heap measurement from the process working set and no longer suggests the process runs in 165 MB.
Nothing left from the first review. Clean.
Closes #166, closes #22.
What
Wires #178's windowed transcription into
analyze(), so a long video's audio is never all in memory, with or without whisper.A video longer than one window (
WINDOW_SECONDS + CUT_SEARCH, 140 s) no longer asksread_loudnessto keep the samples. When whisper runs,pipeline._transcribeplans windows on the loudness curve (overmax(duration, len(curve) * hop), since the track can run past the container's duration) and decodes each one withaudio.decode_pcm(start=, duration=), i.e. ffmpeg-ss/-t, right before whisper gets it. The log saystranscribing in 90 parts of about 120s.A short video still keeps its samples from the loudness pass and goes to whisper as one window, so there's no second decode for the common case.
On a run where the loudness came from the cache, the windows are decoded the same way. The old "reading audio for whisper" full-track decode is gone.
Window size changed from 10 to 2 minutes (
CUT_SEARCH30 → 20 s). Measuring the 3 h file with 10 minute windows gave a 646 MB peak, and the samples were not the cost: faster-whisper's feature extraction peaks at about 1 MB per second of audio it's given.FeatureExtractorpeakSo the window size is what decides the peak. Whisper reads 30 s at a time anyway, and with the cuts on quiet points the transcript didn't change (below).
Testing
uv run ruff check src tests,uv run mypy,uv run pytest -q: 759 passed.tests/test_cache_pipeline.py: the whisper fake now stands in fortranscribe_windowsand reads every window it's given. New: a short video gives whisper the kept samples with one decode. A "long" video (60 s with the window patched to 20 s) doesn't keep samples, and its windows are read as(0, 19.25) (19.25, 22.0) (41.25, 18.75), cut at the two silent dips of the fake track. The existing cache tests pass unchanged (cached-loudness run decodes once more, transcript cache hit decodes nothing).tests/test_whisper_windows.py: the planner tests pin their 10 min / 30 s numbers, plus a check that the defaults keep every window ≤ 140 s on a 3 h curve.What the audio handling costs (
tracemalloc, Python/numpy only) on a 3 h AAC file (the 240 s TTS speech video looped 45 times), whispersmallon CUDA, loudness pass +pipeline._transcribe:keep_samples=True)Every 240 s loop gave 501-504 words (the single-call transcript of one loop has 501), and the word list is in time order.
The whole process, which also holds the model and the CUDA runtime (
K32GetProcessMemoryInfopeak working set; from the review):The model's ~0.9 GB doesn't depend on the video. What does depend on it is now about 140 MB for any length.
--language, the language whisper detects in the first window with speech is used for the rest of the windows (logged aslanguage: en (detected at 0s, used for the rest)), instead of being detected again per window.difflibfinds nothing inserted, dropped or changed, mean start difference 0.013 s, max 0.39 s (the first word after the cut, as noted on Transcribe long audio in windows cut at quiet points #178).clipwright talk.mp4 -n 2 --no-cachelogstranscribing in 2 parts of about 120s,analysis done in 21.2s (0 cuts, 501 words). Clip 1 (107-127 s) spans the cut, and its .srt/.ass have the right lines on both sides ("How did he get that so fast?" / "Alright, new plan."). The frame at 3 s shows the burned caption "GET THAT SO" over 1080x1920 output.Slower by ~5% on 3 h (748 vs 712 s) for the extra calls and seeks. For a 3 h VOD that's 36 s against 480 MB less memory.