test_video_frames.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """M11 抽帧测试:用 ffmpeg 生成测试视频,验证 extract_frames 产出帧卡片。
  2. 无 ffmpeg 时自动跳过(CI/本地可能没装)。
  3. """
  4. from __future__ import annotations
  5. import shutil
  6. import subprocess
  7. from pathlib import Path
  8. import pytest
  9. def _ffmpeg() -> str | None:
  10. try:
  11. import imageio_ffmpeg
  12. return imageio_ffmpeg.get_ffmpeg_exe()
  13. except Exception:
  14. return shutil.which("ffmpeg")
  15. FF = _ffmpeg()
  16. @pytest.mark.skipif(not FF, reason="ffmpeg 不可用")
  17. def test_extract_frames_from_generated_clip(tmp_path):
  18. from creation_knowledge.integrations.video_frames import extract_frames
  19. from core.models import Card
  20. clip = tmp_path / "clip.mp4"
  21. subprocess.run(
  22. [FF, "-y", "-f", "lavfi", "-i",
  23. "testsrc=duration=6:size=320x240:rate=10", str(clip)],
  24. capture_output=True,
  25. )
  26. assert clip.exists()
  27. cards = extract_frames(str(clip), out_dir=str(tmp_path / "f"),
  28. url_prefix="/frames/x", max_frames=8)
  29. assert len(cards) >= 1
  30. assert all(isinstance(c, Card) and c.kind == "frame" for c in cards)
  31. assert all(c.url.startswith("/frames/x/") for c in cards)
  32. # 帧文件确实生成
  33. assert all((tmp_path / "f" / Path(c.url).name).exists() for c in cards)
  34. # 1-based 连续索引
  35. assert [c.index for c in cards] == list(range(1, len(cards) + 1))