|
| 1 | +# |
| 2 | +# PySceneDetect: Python-Based Video Scene Detector |
| 3 | +# ------------------------------------------------------------------- |
| 4 | +# [ Site: https://scenedetect.com ] |
| 5 | +# [ Docs: https://scenedetect.com/docs/ ] |
| 6 | +# [ Github: https://github.com/Breakthrough/PySceneDetect/ ] |
| 7 | +# |
| 8 | +# Copyright (C) 2026 Brandon Castellano <http://www.bcastell.com>. |
| 9 | +# PySceneDetect is licensed under the BSD 3-Clause License; see the |
| 10 | +# included LICENSE file, or visit one of the above pages for details. |
| 11 | +# |
| 12 | +"""Backend Consistency |
| 13 | +
|
| 14 | +Verifies that all available backends produce consistent cut lists for both CFR and VFR videos. |
| 15 | +""" |
| 16 | + |
| 17 | +import importlib.util |
| 18 | +import os |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from scenedetect import ContentDetector, SceneManager, open_video |
| 23 | + |
| 24 | +REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 25 | + |
| 26 | +VIDEOS = [ |
| 27 | + # (relative path under repo root, is_vfr) |
| 28 | + ("tests/resources/testvideo.mp4", False), |
| 29 | + ("tests/resources/goldeneye.mp4", False), |
| 30 | + ("tests/resources/goldeneye-vfr.mp4", True), |
| 31 | +] |
| 32 | + |
| 33 | +BACKENDS = ("opencv", "pyav", "moviepy") |
| 34 | +_BACKEND_PACKAGE = {"opencv": "cv2", "pyav": "av", "moviepy": "moviepy"} |
| 35 | + |
| 36 | + |
| 37 | +def _installed_backends(): |
| 38 | + return [ |
| 39 | + name for name in BACKENDS if importlib.util.find_spec(_BACKEND_PACKAGE[name]) is not None |
| 40 | + ] |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.release |
| 44 | +@pytest.mark.parametrize("rel_path,is_vfr", VIDEOS) |
| 45 | +def test_cross_backend_consistency(rel_path, is_vfr): |
| 46 | + video_path = os.path.join(REPO_ROOT, rel_path) |
| 47 | + if not os.path.exists(video_path): |
| 48 | + pytest.skip(f"Video {rel_path} not present (needs resources branch).") |
| 49 | + |
| 50 | + backends = _installed_backends() |
| 51 | + if is_vfr and "moviepy" in backends: |
| 52 | + # MoviePy does not honor per-frame PTS on VFR video — tracked separately |
| 53 | + # from the OpenCV/PyAV VFR path that this test gates. |
| 54 | + backends = [b for b in backends if b != "moviepy"] |
| 55 | + if len(backends) < 2: |
| 56 | + pytest.skip(f"Need at least two backends, have: {backends}") |
| 57 | + |
| 58 | + results = {} |
| 59 | + for backend in backends: |
| 60 | + try: |
| 61 | + video = open_video(video_path, backend=backend) |
| 62 | + except Exception as exc: |
| 63 | + pytest.skip(f"{backend} failed to open {rel_path}: {exc}") |
| 64 | + sm = SceneManager() |
| 65 | + sm.add_detector(ContentDetector()) |
| 66 | + sm.detect_scenes(video) |
| 67 | + scenes = sm.get_scene_list() |
| 68 | + if is_vfr: |
| 69 | + results[backend] = [s[0].seconds for s in scenes[1:]] |
| 70 | + else: |
| 71 | + results[backend] = [s[0].frame_num for s in scenes[1:]] |
| 72 | + |
| 73 | + reference = backends[0] |
| 74 | + expected = results[reference] |
| 75 | + for backend in backends[1:]: |
| 76 | + actual = results[backend] |
| 77 | + assert len(actual) == len(expected), ( |
| 78 | + f"Cut count mismatch: {backend}={len(actual)} vs {reference}={len(expected)}" |
| 79 | + ) |
| 80 | + if is_vfr: |
| 81 | + for a, e in zip(actual, expected, strict=True): |
| 82 | + # Tolerance: ~one frame at 30 fps. Plan calls for ±1 local-frame-duration; |
| 83 | + # 50 ms is a conservative superset that still catches real drift. |
| 84 | + assert abs(a - e) < 0.05, ( |
| 85 | + f"VFR timestamp drift between {backend} and {reference}: {a} vs {e}" |
| 86 | + ) |
| 87 | + else: |
| 88 | + assert actual == expected, ( |
| 89 | + f"CFR frame-number mismatch between {backend} and {reference}" |
| 90 | + ) |
0 commit comments