- Directory ports now emit a browsable collection reference (droboticflow.directory-index/v1): the members API lists BOS or workspace entries live with read-only streaming content delivery, and the frontend renders directory artifacts as 目录 collections instead of opaque files. - Video slicing and target clipping default to VIRTUAL_CUT: they record source-relative timestamp ranges without re-encoding physical segments; packaging materializes final MP4s from the original sources. VIRTUAL_CUT=false preserves the legacy path. - Rough-cut member previews bound playback to recorded ranges via media fragments; refresh release manifests for changed operators.
258 lines
12 KiB
Python
258 lines
12 KiB
Python
import pathlib
|
|
import tempfile
|
|
import unittest
|
|
from typing import Any, Callable
|
|
from unittest import mock
|
|
|
|
from backend.operators.builtin.package import operator as package_operator
|
|
from backend.operators.builtin.video_slice import operator as video_slice_operator
|
|
from backend.operators.builtin.video_target_clip import operator as video_target_clip_operator
|
|
|
|
|
|
def make_helpers(overrides: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
helpers: dict[str, Any] = {
|
|
"hyperparams": lambda node_data: {
|
|
str(item.get("key", "")): str(item.get("value", ""))
|
|
for item in node_data.get("hyperparams", [])
|
|
if isinstance(item, dict)
|
|
},
|
|
"first_payload_with_keys": lambda inputs, *keys: next(
|
|
(
|
|
artifact.get("data")
|
|
for artifact in inputs
|
|
if isinstance(artifact.get("data"), dict)
|
|
and any(key in artifact["data"] for key in keys)
|
|
),
|
|
None,
|
|
),
|
|
"parse_positive_int_param": lambda params, key, default: max(
|
|
1, int(str(params.get(key, "")).strip() or default)
|
|
),
|
|
"parse_positive_float_param": lambda params, key, default: float(
|
|
str(params.get(key, "")).strip() or default
|
|
),
|
|
"parse_unit_interval_param": lambda params, key, default: min(
|
|
1.0, max(0.0, float(str(params.get(key, "")).strip() or default))
|
|
),
|
|
"parse_bool_param": lambda params, key, default=False: (
|
|
default
|
|
if not str(params.get(key, "")).strip()
|
|
else str(params.get(key, "")).strip().lower() in {"1", "true", "yes", "on"}
|
|
),
|
|
"log": lambda message: None,
|
|
"output_binding": lambda node_data: {"id": "out", "name": "out"},
|
|
"write_json_artifact": None,
|
|
"collect_output_artifacts": None,
|
|
}
|
|
helpers.update(overrides or {})
|
|
return helpers
|
|
|
|
|
|
def recording_json_writer(store: list[tuple[str, dict[str, Any]]]) -> Callable:
|
|
def write_json_artifact(_run_id, _node_id, _output_dir, name, payload, **_kwargs):
|
|
store.append((str(name), payload))
|
|
return {"name": str(name), "type": "json", "data": payload}
|
|
|
|
return write_json_artifact
|
|
|
|
|
|
class VideoSliceVirtualCutTest(unittest.TestCase):
|
|
def test_virtual_cut_default_records_timestamps_without_physical_clips(self):
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
output_dir = pathlib.Path(temp_dir)
|
|
written: list[tuple[str, dict[str, Any]]] = []
|
|
collect_calls: list[Any] = []
|
|
ffmpeg_calls: list[Any] = []
|
|
helpers = make_helpers(
|
|
{
|
|
"write_json_artifact": recording_json_writer(written),
|
|
"collect_output_artifacts": lambda *args, **kwargs: collect_calls.append(args) or [],
|
|
"source_path_from_item": lambda item, name, output, sources=None: output / name,
|
|
"inspect_video_file": lambda path: {"duration": 700.0, "resolution": "640x480", "frameRate": 30.0},
|
|
"ffmpeg_available": lambda: True,
|
|
}
|
|
)
|
|
node_data = {"hyperparams": []}
|
|
payload = {
|
|
"items": [
|
|
{
|
|
"filename": "video.mp4",
|
|
"duration": 700.0,
|
|
"collectionId": "collection-1",
|
|
"source": {"path": "/workspace/raw/video.mp4"},
|
|
"meta": {},
|
|
"quality": {},
|
|
}
|
|
]
|
|
}
|
|
|
|
artifacts = video_slice_operator.run("run-1", "slice", output_dir, node_data, [{"name": "filtered.json", "type": "json", "data": payload}], helpers)
|
|
|
|
self.assertFalse(ffmpeg_calls)
|
|
manifest = next(payload for name, payload in written if name == "clips-manifest.json")
|
|
self.assertTrue(manifest["virtualCut"])
|
|
self.assertEqual(manifest["clipCount"], 3)
|
|
first_clip = manifest["items"][0]
|
|
self.assertTrue(first_clip["virtualCut"])
|
|
self.assertNotIn("clipPath", first_clip)
|
|
self.assertEqual(first_clip["startSecond"], 0.0)
|
|
self.assertEqual(first_clip["endSecond"], 300.0)
|
|
last_clip = manifest["items"][-1]
|
|
self.assertEqual(last_clip["startSecond"], 600.0)
|
|
self.assertEqual(last_clip["endSecond"], 700.0)
|
|
self.assertEqual(last_clip["duration"], 100.0)
|
|
self.assertFalse((output_dir / "clips").exists())
|
|
self.assertEqual(collect_calls, [])
|
|
self.assertEqual([artifact.get("name") for artifact in artifacts], ["clips-manifest.json"])
|
|
|
|
def test_virtual_cut_false_keeps_physical_segment_pipeline(self):
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
output_dir = pathlib.Path(temp_dir)
|
|
written: list[tuple[str, dict[str, Any]]] = []
|
|
collect_calls: list[Any] = []
|
|
|
|
def fake_ffmpeg_segment(source_path, destination, start_second, end_second):
|
|
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
destination.write_bytes(b"clip")
|
|
return {"duration": end_second - start_second, "resolution": "640x480", "frameRate": 30.0}
|
|
|
|
helpers = make_helpers(
|
|
{
|
|
"write_json_artifact": recording_json_writer(written),
|
|
"collect_output_artifacts": lambda *args, **kwargs: collect_calls.append(args) or [],
|
|
"source_path_from_item": lambda item, name, output, sources=None: output / name,
|
|
"inspect_video_file": lambda path: {"duration": 700.0, "resolution": "640x480", "frameRate": 30.0},
|
|
"ffmpeg_available": lambda: True,
|
|
"write_video_segment_with_ffmpeg": fake_ffmpeg_segment,
|
|
}
|
|
)
|
|
node_data = {"hyperparams": [{"key": "VIRTUAL_CUT", "value": "false"}]}
|
|
payload = {
|
|
"items": [
|
|
{
|
|
"filename": "video.mp4",
|
|
"duration": 700.0,
|
|
"source": {"path": "/workspace/raw/video.mp4"},
|
|
}
|
|
]
|
|
}
|
|
|
|
video_slice_operator.run("run-1", "slice", output_dir, node_data, [{"name": "filtered.json", "type": "json", "data": payload}], helpers)
|
|
|
|
manifest = written[0][1]
|
|
self.assertFalse(manifest["virtualCut"])
|
|
first_clip = manifest["items"][0]
|
|
self.assertNotIn("virtualCut", first_clip)
|
|
self.assertIn("clipPath", first_clip)
|
|
self.assertTrue(pathlib.Path(first_clip["clipPath"]).exists())
|
|
self.assertEqual(len(collect_calls), 1)
|
|
|
|
|
|
class VideoTargetClipVirtualCutTest(unittest.TestCase):
|
|
def test_virtual_cut_default_skips_physical_materialization(self):
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
output_dir = pathlib.Path(temp_dir)
|
|
source_path = output_dir / "video.mp4"
|
|
source_path.write_bytes(b"fake-video-bytes")
|
|
written: list[tuple[str, dict[str, Any]]] = []
|
|
collect_calls: list[Any] = []
|
|
clip_calls: list[Any] = []
|
|
detection = {
|
|
"label": "motion",
|
|
"confidence": 0.9,
|
|
"second": 5.0,
|
|
"frameIndex": 50,
|
|
"boxes": [{"x": 1, "y": 2, "width": 3, "height": 4}],
|
|
}
|
|
helpers = make_helpers(
|
|
{
|
|
"VIDEO_FILE_SUFFIXES": {".mp4", ".mov"},
|
|
"write_json_artifact": recording_json_writer(written),
|
|
"collect_output_artifacts": lambda *args, **kwargs: collect_calls.append(args) or [],
|
|
"inspect_video_file": lambda path: {"duration": 10.0, "resolution": "640x480", "frameRate": 10.0},
|
|
"ffmpeg_available": lambda: True,
|
|
}
|
|
)
|
|
node_data = {"hyperparams": []}
|
|
payload = {"items": [{"filename": "video.mp4", "path": str(source_path), "duration": 10.0}]}
|
|
|
|
with mock.patch.object(
|
|
video_target_clip_operator,
|
|
"_detect_targets",
|
|
return_value=("motion", [detection], {"fps": 10.0}),
|
|
), mock.patch.object(
|
|
video_target_clip_operator,
|
|
"_clip_segment",
|
|
side_effect=lambda *_args, **_kwargs: clip_calls.append(_args) or {},
|
|
):
|
|
video_target_clip_operator.run("run-1", "target", output_dir, node_data, [{"name": "videos.json", "type": "json", "data": payload}], helpers)
|
|
|
|
self.assertEqual(clip_calls, [])
|
|
self.assertEqual(collect_calls, [])
|
|
manifest = next(payload for name, payload in written if name == "target-clips-manifest.json")
|
|
self.assertTrue(manifest["virtualCut"])
|
|
clip = manifest["items"][0]
|
|
self.assertTrue(clip["virtualCut"])
|
|
self.assertNotIn("clipPath", clip)
|
|
self.assertAlmostEqual(clip["startSecond"], 5.0 - 0.25 - 0.1, places=3)
|
|
self.assertAlmostEqual(clip["endSecond"], 5.0 + 0.25 + 0.2, places=3)
|
|
|
|
|
|
class PackageVirtualItemTest(unittest.TestCase):
|
|
def test_fanyu_v5_package_materializes_virtual_items_from_source(self):
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
output_dir = pathlib.Path(temp_dir)
|
|
written: list[tuple[str, dict[str, Any]]] = []
|
|
materialize_calls: list[dict[str, Any]] = []
|
|
|
|
def fake_materialize(item, package_dir, helpers):
|
|
materialize_calls.append(dict(item))
|
|
destination = package_dir / f"{item['clipId']}.mp4"
|
|
destination.write_bytes(b"final-segment")
|
|
return destination
|
|
|
|
accepted_payload = {
|
|
"schemaVersion": "droboticflow.annotation.accepted-partition/v1",
|
|
"items": [
|
|
{
|
|
"clipId": "c1",
|
|
"clipName": "c1.mp4",
|
|
"virtualCut": True,
|
|
"startSecond": 12.0,
|
|
"endSecond": 18.5,
|
|
"sourceVideo": "src.mp4",
|
|
"source": {"path": "bos://bucket/raw/src.mp4"},
|
|
"annotation": {"actions": [], "objects": []},
|
|
}
|
|
],
|
|
}
|
|
helpers = make_helpers(
|
|
{
|
|
"payloads_from_inputs": lambda inputs: [
|
|
artifact["data"] for artifact in inputs if isinstance(artifact.get("data"), dict)
|
|
],
|
|
"write_json_artifact": recording_json_writer(written),
|
|
"collect_output_artifacts": lambda *args, **kwargs: [],
|
|
}
|
|
)
|
|
node_data = {"hyperparams": []}
|
|
|
|
with mock.patch.object(package_operator, "_materialize_final_mp4", side_effect=fake_materialize):
|
|
package_operator._run_fanyu_v5(
|
|
"run-1",
|
|
"package",
|
|
output_dir,
|
|
node_data,
|
|
[{"name": "accepted.json", "type": "json", "data": accepted_payload}],
|
|
helpers,
|
|
)
|
|
|
|
self.assertEqual(len(materialize_calls), 1)
|
|
manifest = next(payload for name, payload in written if name == "packages-manifest.json")
|
|
self.assertEqual(manifest["packageCount"], 1)
|
|
self.assertEqual(manifest["items"][0]["clipId"], "c1")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|