clipwright/tests/test_frame_sampler.py
victormanuel.caverogracia a4c92cac2c perf(pipeline): halve a job's CPU with byte-identical clips
Reverts 1d3d20c (NVDEC decode + quarter-size blur): it changed the
delivered pixels (SSIM 0.998-0.9997, not identical), and clips must not
change. Replaces it with two fixes that leave every clip byte-identical.

- Parakeet's onnxruntime pool threads no longer spin
  (transcribe_backends.parakeet_session_options). While the GPU did the
  work they burned ~160 CPU-s per 9 min of audio, the biggest CPU cost of
  a job. The VAD keeps load_vad's defaults: any options there make
  onnx_asr drop its default providers and a word changes.
- analyze_scenes_strategy and split_layout read their sampled frames in
  one forward pass (frame_sampler.read_at) instead of a seek per sample,
  which re-decoded the GOP every time: 89 -> 6 CPU-s for one clip's
  samples, same frames.

Bench on balrog, 3 Carlos Gracia videos (2.5/9/17 min, 15 clips), Gemini
decisions recorded once and replayed: every clip identical to the old
code (decoded-frame MD5s + audio). Serial stage profile of the 9 min
video: 629 -> 273 CPU-s per job; Python side 332 -> 57 CPU-s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 15:40:22 +02:00

78 lines
2.2 KiB
Python

import cv2
import numpy as np
import pytest
import frame_sampler
N_FRAMES = 60
@pytest.fixture(scope="module")
def video(tmp_path_factory):
"""A short clip whose frames all differ, with keyframes far apart."""
path = str(tmp_path_factory.mktemp("fs") / "clip.avi")
writer = cv2.VideoWriter(path, cv2.VideoWriter_fourcc(*"MJPG"), 30, (64, 48))
if not writer.isOpened():
pytest.skip("no video writer in this OpenCV build")
rng = np.random.default_rng(1)
for i in range(N_FRAMES):
frame = rng.integers(0, 255, (48, 64, 3), dtype=np.uint8)
cv2.putText(frame, str(i), (4, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
writer.write(frame)
writer.release()
return path
def _seek_read(path, indices):
"""What the classifiers did before: one seek + read per sample."""
cap = cv2.VideoCapture(path)
out = []
for i in indices:
cap.set(cv2.CAP_PROP_POS_FRAMES, int(i))
ok, frame = cap.read()
out.append(frame if ok else None)
cap.release()
return out
def _forward(path, indices, **kw):
cap = cv2.VideoCapture(path)
out = list(frame_sampler.read_at(cap, indices, **kw))
cap.release()
return out
def _same(a, b):
assert len(a) == len(b)
for x, y in zip(a, b):
assert (x is None) == (y is None)
if x is not None:
assert np.array_equal(x, y)
def test_forward_pass_returns_the_frames_a_seek_returns(video):
idx = [0, 3, 4, 10, 11, 12, 30, 45, 59]
_same(_forward(video, idx), _seek_read(video, idx))
def test_repeated_index_yields_the_same_frame_again(video):
idx = [5, 5, 5, 6]
got = _forward(video, idx)
_same(got, _seek_read(video, idx))
assert got[0] is got[1] is got[2]
def test_past_the_end_is_none_like_a_failed_read(video):
idx = [58, 59, 60, 75]
got = _forward(video, idx)
assert got[2] is None and got[3] is None
_same(got, _seek_read(video, idx))
def test_long_jumps_and_steps_back_seek_instead(video):
idx = [2, 50, 20, 21, 57]
_same(_forward(video, idx, max_forward=10), _seek_read(video, idx))
def test_empty_request_reads_nothing(video):
assert _forward(video, []) == []