Skip to content

Transcribe long videos a window at a time - #182

Merged
A21-devs merged 2 commits into
mainfrom
perf/whisper-stream-windows
Dec 5, 2026
Merged

A21-devs merged 2 commits into
mainfrom
perf/whisper-stream-windows

Conversation

@A21-devs

@A21-devs A21-devs commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

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 asks read_loudness to keep the samples. When whisper runs, pipeline._transcribe plans windows on the loudness curve (over max(duration, len(curve) * hop), since the track can run past the container's duration) and decodes each one with audio.decode_pcm(start=, duration=), i.e. ffmpeg -ss/-t, right before whisper gets it. The log says transcribing 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_SEARCH 30 → 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.

    window 60 s 120 s 180 s 300 s 600 s
    FeatureExtractor peak 59 MB 117 MB 176 MB 294 MB 587 MB

    So 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 for transcribe_windows and 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.
  • Memory. The two measurements below answer different questions.
    • What the audio handling costs (tracemalloc, Python/numpy only) on a 3 h AAC file (the 240 s TTS speech video looped 45 times), whisper small on CUDA, loudness pass + pipeline._transcribe:

      loudness pass whisper words time
      before (Stream the audio decode instead of holding the whole track #158, keep_samples=True) 670 MB whole track in one call
      10 min windows 11 MB 646 MB 22570 712 s
      2 min windows (this PR) 11 MB 165 MB 22578 748 s

      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 (K32GetProcessMemoryInfo peak working set; from the review):

      peak working set
      60 s file, about the floor for model + CUDA 918 MB
      32 min file, this PR 1061 MB
      32 min file, one whisper call (old path) 1920 MB

      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: with no --language, the language whisper detects in the first window with speech is used for the rest of the windows (logged as language: en (detected at 0s, used for the rest)), instead of being detected again per window.
  • Accuracy on the 240 s TTS video, single call vs. 2 min windows: one cut at 111.25 s (in a pause between sentences), 501 words both ways, difflib finds 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).
  • Real run: clipwright talk.mp4 -n 2 --no-cache logs transcribing 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.

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 A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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),

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@A21-devs

A21-devs commented Dec 5, 2026

Copy link
Copy Markdown
Owner Author

Both addressed.

  1. Memory numbers: the description now keeps the tracemalloc table (what the audio handling costs) and adds the process working-set table from the review. The headline is now "about 140 MB on top of the model for any length", not "165 MB".

  2. Language: fixed in 6720e69. In _run, when no language was given and there's more than one window, the language whisper detects in the first window that produces words is passed to every later window, and logged once. A window with no speech doesn't decide it, because intro music is the worst place to detect from. An explicit --language is never replaced, and the one-window path (transcribe()) is unchanged.

    • New tests (the fake model now returns an info.language and records the language each call was asked for): silent first window guessing "cy", second window "de": calls are asked [None, None, "de"] and the log says language: de (detected at 590s, used for the rest). A given language stays ["en", "en", "en"] with no log line. One window logs nothing.
    • Real run: clipwright talk.mp4 -n 2 --no-cache logs language: en (detected at 0s, used for the rest) and still gets 501 words. Single call vs. windows on the same file: 501/501 matched, mean start difference 0.013 s, same as before.
    • ruff, mypy clean; tests/test_whisper_windows.py, test_whisper_load.py, test_progress.py, test_cache_pipeline.py, test_end_to_end.py pass.

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 language is 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.

@A21-devs
A21-devs merged commit fe7b56b into main Dec 5, 2026
4 checks passed
@A21-devs
A21-devs deleted the perf/whisper-stream-windows branch December 5, 2026 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feed whisper the audio in pieces instead of the whole track Stream audio in chunks for very long videos

1 participant