clipwright/tests/test_ffmpeg_utils.py
juancarlos.cavero 719d444139 feat: GPU pipeline — Parakeet ASR backend with whisper fallback + NVENC encoder helper
- transcribe_backends.py: parakeet-tdt-0.6b-v3 (onnx-asr, VAD+timestamps) as
  selectable backend (TRANSCRIBE_BACKEND), token->word reconstruction keeping
  the leading-space convention, absolute timestamps, native floats, language
  detection via py3langid, and automatic whisper fallback (errors, empty
  output, or language outside parakeet's 25).
- whisper singleton + ASR GPU semaphore (model stays resident; GPU decodes
  serialized; CPU path behavior unchanged).
- ffmpeg_utils.py: central encoder args (FFMPEG_ENCODER=x264|nvenc|auto with
  cached probe + fallback); migrated the 9 hardcoded libx264 call sites.
- saasshorts now honors WHISPER_* config instead of hardcoded base/cpu/int8.
- Dockerfile: ARG GPU=1 installs CUDA user-space libs (cuBLAS/cuDNN9) +
  onnx-asr/onnxruntime-gpu; LD_LIBRARY_PATH + NVIDIA_DRIVER_CAPABILITIES.
- Validated on prod GPU (RTX 4000 Ada, driver 580): 94s clip transcribed in
  6.5s, word timestamps within ~0.1s of whisper, fallback path exercised.
2026-07-18 01:38:11 +02:00

77 lines
2.2 KiB
Python

import subprocess
import pytest
import ffmpeg_utils
from ffmpeg_utils import (
DELIVERY,
QUALITY,
QUALITY_FAST,
reset_encoder_cache,
video_encode_args,
)
@pytest.fixture(autouse=True)
def _clean_encoder_state(monkeypatch):
monkeypatch.delenv("FFMPEG_ENCODER", raising=False)
reset_encoder_cache()
yield
reset_encoder_cache()
def test_default_args_pin_historical_x264_settings():
assert video_encode_args(QUALITY) == [
"-c:v", "libx264", "-preset", "medium", "-crf", "18"]
assert video_encode_args(QUALITY_FAST) == [
"-c:v", "libx264", "-preset", "fast", "-crf", "18"]
assert video_encode_args(DELIVERY) == [
"-c:v", "libx264", "-preset", "fast", "-crf", "22"]
def test_unknown_tier_raises():
with pytest.raises(ValueError):
video_encode_args("ultra")
def test_nvenc_mode_uses_nvenc_when_probe_passes(monkeypatch):
monkeypatch.setenv("FFMPEG_ENCODER", "nvenc")
monkeypatch.setattr(ffmpeg_utils, "_probe_nvenc", lambda: True)
args = video_encode_args(QUALITY)
assert args[:2] == ["-c:v", "h264_nvenc"]
assert "-cq" in args
def test_nvenc_mode_falls_back_to_x264_when_probe_fails(monkeypatch):
monkeypatch.setenv("FFMPEG_ENCODER", "nvenc")
monkeypatch.setattr(ffmpeg_utils, "_probe_nvenc", lambda: False)
assert video_encode_args(QUALITY)[:2] == ["-c:v", "libx264"]
def test_auto_probes_only_once(monkeypatch):
calls = []
def fake_probe():
calls.append(1)
return True
monkeypatch.setenv("FFMPEG_ENCODER", "auto")
monkeypatch.setattr(ffmpeg_utils, "_probe_nvenc", fake_probe)
for _ in range(3):
video_encode_args(QUALITY_FAST)
assert len(calls) == 1
def test_missing_ffmpeg_binary_means_x264(monkeypatch):
def raise_missing(*args, **kwargs):
raise FileNotFoundError("ffmpeg")
monkeypatch.setenv("FFMPEG_ENCODER", "auto")
monkeypatch.setattr(subprocess, "run", raise_missing)
assert video_encode_args(DELIVERY)[:2] == ["-c:v", "libx264"]
def test_returns_a_fresh_list_each_call():
first = video_encode_args(QUALITY)
first.append("-mutated")
assert "-mutated" not in video_encode_args(QUALITY)