forked from longtao.wu/droboticflow
🐛 Pair Capture sidecars by segment and reject unmapped transcoded timestamps
This commit is contained in:
parent
4f9342c53d
commit
0854eaf2cf
@ -9,7 +9,7 @@ from typing import Any, Callable
|
||||
|
||||
HelperMap = dict[str, Callable[..., Any]]
|
||||
VIDEO_SUFFIXES = {".mp4", ".mov", ".m4v", ".avi", ".mkv", ".webm"}
|
||||
CAPTURE_PATTERN = re.compile(r"^(?P<capture>Orbbec_Ego_[^/]+?_(?:19|20)\d{6}_\d{6})(?:/|_)")
|
||||
CAPTURE_PATTERN = re.compile(r"^(?P<capture>[^/]+?_(?:19|20)\d{6}_\d{6})(?:/|_)")
|
||||
CAMERA_PATTERN = re.compile(r"_camera_(?P<side>left|right)_", re.IGNORECASE)
|
||||
|
||||
|
||||
@ -30,6 +30,51 @@ def _camera_side(path: str) -> str:
|
||||
return matched.group("side").lower() if matched else ""
|
||||
|
||||
|
||||
def _paired_files(video_path: str, entries: list[dict[str, Any]]) -> dict[str, str]:
|
||||
"""Match sidecars within one Capture and segment, never the first segment."""
|
||||
video = pathlib.PurePosixPath(video_path)
|
||||
part = re.match(r"^(?P<capture>.+)_camera_(?:left|right)_(?P<part>part\d+)(?P<suffix>.*)$", video.stem)
|
||||
|
||||
def find(name: str) -> str:
|
||||
candidates = [_entry_path(item) for item in entries
|
||||
if pathlib.PurePosixPath(_entry_path(item)).name == name]
|
||||
local = [path for path in candidates if pathlib.PurePosixPath(path).parent == video.parent]
|
||||
matches = local or candidates
|
||||
if len(matches) > 1:
|
||||
raise RuntimeError(f"配套文件不唯一: {video_path} -> {name}: {matches}")
|
||||
return matches[0] if matches else ""
|
||||
|
||||
pts = find(video.stem + "_pts.csv")
|
||||
if part:
|
||||
capture, segment = part.group("capture"), part.group("part")
|
||||
original_stem = video.stem.removesuffix(part.group("suffix")) if part.group("suffix") else video.stem
|
||||
original_pts = find(original_stem + "_pts.csv")
|
||||
if not pts and part.group("suffix") and original_pts:
|
||||
raise RuntimeError(
|
||||
f"转码视频缺少逐帧对应的 PTS: {video_path}; 找到的是原始 PTS {original_pts}。"
|
||||
"请提供原始视频或转码时保存的帧映射及对应 PTS;不能按帧率猜测或从零生成硬件时间戳。"
|
||||
)
|
||||
names = {"audio": f"{capture}_audio_{segment}.wav",
|
||||
"audioPts": f"{capture}_audio_{segment}_pts.csv",
|
||||
"imu": f"{capture}_imu_{segment}.csv"}
|
||||
result = {key: find(name) for key, name in names.items()}
|
||||
for key, suffix in (("audio", "audio.wav"), ("audioPts", "audio_pts.csv"), ("imu", "imu.csv")):
|
||||
if not result[key]:
|
||||
result[key] = find(f"{capture}_{suffix}")
|
||||
result.update({"cameraCalibration": find(f"{capture}_calibration_camera.yaml"),
|
||||
"imuCalibration": find(f"{capture}_calibration_imu.yaml")})
|
||||
else:
|
||||
result = {}
|
||||
for key, suffix in (("audio", "_audio.wav"), ("audioPts", "_audio_pts.csv"),
|
||||
("imu", "_imu.csv"), ("cameraCalibration", "_calibration_camera.yaml"),
|
||||
("imuCalibration", "_calibration_imu.yaml")):
|
||||
matches = [_entry_path(item) for item in entries if _entry_path(item).endswith(suffix)]
|
||||
if len(matches) > 1:
|
||||
raise RuntimeError(f"配套文件不唯一: {video_path} -> {suffix}")
|
||||
result[key] = matches[0] if matches else ""
|
||||
return {"pts": pts, **result}
|
||||
|
||||
|
||||
def _run_legacy(
|
||||
run_id: str,
|
||||
node_id: str,
|
||||
@ -58,7 +103,6 @@ def _run_legacy(
|
||||
videos: list[dict[str, Any]] = []
|
||||
for capture_id in sorted(grouped):
|
||||
capture_entries = grouped[capture_id]
|
||||
by_name = {pathlib.PurePosixPath(_entry_path(item)).name: item for item in capture_entries}
|
||||
capture_videos = []
|
||||
present_sides: set[str] = set()
|
||||
for entry in capture_entries:
|
||||
@ -71,7 +115,6 @@ def _run_legacy(
|
||||
if side_filter != "both" and side != side_filter:
|
||||
continue
|
||||
filename = pathlib.PurePosixPath(path).name
|
||||
pts_name = pathlib.PurePosixPath(path).stem + "_pts.csv"
|
||||
video = {
|
||||
"collectionId": capture_id,
|
||||
"collectionName": capture_id,
|
||||
@ -82,14 +125,7 @@ def _run_legacy(
|
||||
"mimeType": str(entry.get("mimeType", "video/mp4") or "video/mp4"),
|
||||
"cameraSide": side,
|
||||
"source": entry.get("source") if isinstance(entry.get("source"), dict) else {"path": entry.get("path", path)},
|
||||
"pairedFiles": {
|
||||
"pts": _entry_path(by_name.get(pts_name, {})),
|
||||
"audio": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_audio.wav")), ""),
|
||||
"audioPts": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_audio_pts.csv")), ""),
|
||||
"imu": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_imu.csv")), ""),
|
||||
"cameraCalibration": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_calibration_camera.yaml")), ""),
|
||||
"imuCalibration": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_calibration_imu.yaml")), ""),
|
||||
},
|
||||
"pairedFiles": _paired_files(path, capture_entries),
|
||||
"meta": {"device": "Orbbec Ego", "captureId": capture_id, "cameraSide": side},
|
||||
}
|
||||
capture_videos.append(video)
|
||||
|
||||
@ -9,7 +9,7 @@ from typing import Any, Callable
|
||||
|
||||
HelperMap = dict[str, Callable[..., Any]]
|
||||
VIDEO_SUFFIXES = {".mp4", ".mov", ".m4v", ".avi", ".mkv", ".webm"}
|
||||
CAPTURE_PATTERN = re.compile(r"^(?P<capture>Orbbec_Ego_[^/]+?_(?:19|20)\d{6}_\d{6})(?:/|_)")
|
||||
CAPTURE_PATTERN = re.compile(r"^(?P<capture>[^/]+?_(?:19|20)\d{6}_\d{6})(?:/|_)")
|
||||
CAMERA_PATTERN = re.compile(r"_camera_(?P<side>left|right)_", re.IGNORECASE)
|
||||
|
||||
|
||||
@ -30,6 +30,51 @@ def _camera_side(path: str) -> str:
|
||||
return matched.group("side").lower() if matched else ""
|
||||
|
||||
|
||||
def _paired_files(video_path: str, entries: list[dict[str, Any]]) -> dict[str, str]:
|
||||
"""Match sidecars within one Capture and segment, never the first segment."""
|
||||
video = pathlib.PurePosixPath(video_path)
|
||||
part = re.match(r"^(?P<capture>.+)_camera_(?:left|right)_(?P<part>part\d+)(?P<suffix>.*)$", video.stem)
|
||||
|
||||
def find(name: str) -> str:
|
||||
candidates = [_entry_path(item) for item in entries
|
||||
if pathlib.PurePosixPath(_entry_path(item)).name == name]
|
||||
local = [path for path in candidates if pathlib.PurePosixPath(path).parent == video.parent]
|
||||
matches = local or candidates
|
||||
if len(matches) > 1:
|
||||
raise RuntimeError(f"配套文件不唯一: {video_path} -> {name}: {matches}")
|
||||
return matches[0] if matches else ""
|
||||
|
||||
pts = find(video.stem + "_pts.csv")
|
||||
if part:
|
||||
capture, segment = part.group("capture"), part.group("part")
|
||||
original_stem = video.stem.removesuffix(part.group("suffix")) if part.group("suffix") else video.stem
|
||||
original_pts = find(original_stem + "_pts.csv")
|
||||
if not pts and part.group("suffix") and original_pts:
|
||||
raise RuntimeError(
|
||||
f"转码视频缺少逐帧对应的 PTS: {video_path}; 找到的是原始 PTS {original_pts}。"
|
||||
"请提供原始视频或转码时保存的帧映射及对应 PTS;不能按帧率猜测或从零生成硬件时间戳。"
|
||||
)
|
||||
names = {"audio": f"{capture}_audio_{segment}.wav",
|
||||
"audioPts": f"{capture}_audio_{segment}_pts.csv",
|
||||
"imu": f"{capture}_imu_{segment}.csv"}
|
||||
result = {key: find(name) for key, name in names.items()}
|
||||
for key, suffix in (("audio", "audio.wav"), ("audioPts", "audio_pts.csv"), ("imu", "imu.csv")):
|
||||
if not result[key]:
|
||||
result[key] = find(f"{capture}_{suffix}")
|
||||
result.update({"cameraCalibration": find(f"{capture}_calibration_camera.yaml"),
|
||||
"imuCalibration": find(f"{capture}_calibration_imu.yaml")})
|
||||
else:
|
||||
result = {}
|
||||
for key, suffix in (("audio", "_audio.wav"), ("audioPts", "_audio_pts.csv"),
|
||||
("imu", "_imu.csv"), ("cameraCalibration", "_calibration_camera.yaml"),
|
||||
("imuCalibration", "_calibration_imu.yaml")):
|
||||
matches = [_entry_path(item) for item in entries if _entry_path(item).endswith(suffix)]
|
||||
if len(matches) > 1:
|
||||
raise RuntimeError(f"配套文件不唯一: {video_path} -> {suffix}")
|
||||
result[key] = matches[0] if matches else ""
|
||||
return {"pts": pts, **result}
|
||||
|
||||
|
||||
def _run_legacy(
|
||||
run_id: str,
|
||||
node_id: str,
|
||||
@ -58,7 +103,6 @@ def _run_legacy(
|
||||
videos: list[dict[str, Any]] = []
|
||||
for capture_id in sorted(grouped):
|
||||
capture_entries = grouped[capture_id]
|
||||
by_name = {pathlib.PurePosixPath(_entry_path(item)).name: item for item in capture_entries}
|
||||
capture_videos = []
|
||||
present_sides: set[str] = set()
|
||||
for entry in capture_entries:
|
||||
@ -71,7 +115,6 @@ def _run_legacy(
|
||||
if side_filter != "both" and side != side_filter:
|
||||
continue
|
||||
filename = pathlib.PurePosixPath(path).name
|
||||
pts_name = pathlib.PurePosixPath(path).stem + "_pts.csv"
|
||||
video = {
|
||||
"collectionId": capture_id,
|
||||
"collectionName": capture_id,
|
||||
@ -82,14 +125,7 @@ def _run_legacy(
|
||||
"mimeType": str(entry.get("mimeType", "video/mp4") or "video/mp4"),
|
||||
"cameraSide": side,
|
||||
"source": entry.get("source") if isinstance(entry.get("source"), dict) else {"path": entry.get("path", path)},
|
||||
"pairedFiles": {
|
||||
"pts": _entry_path(by_name.get(pts_name, {})),
|
||||
"audio": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_audio.wav")), ""),
|
||||
"audioPts": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_audio_pts.csv")), ""),
|
||||
"imu": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_imu.csv")), ""),
|
||||
"cameraCalibration": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_calibration_camera.yaml")), ""),
|
||||
"imuCalibration": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_calibration_imu.yaml")), ""),
|
||||
},
|
||||
"pairedFiles": _paired_files(path, capture_entries),
|
||||
"meta": {"device": "Orbbec Ego", "captureId": capture_id, "cameraSide": side},
|
||||
}
|
||||
capture_videos.append(video)
|
||||
|
||||
@ -114,6 +114,33 @@ def test_orbbec_normalizer_pairs_real_capture_files(tmp_path):
|
||||
assert all(item["pairedFiles"]["imu"].endswith("_imu.csv") for item in payload["items"])
|
||||
|
||||
|
||||
def test_normalizer_pairs_each_segment_and_rejects_unmapped_transcodes():
|
||||
import pytest
|
||||
capture = "FYLX_TEST02_AZER764009A_20260903_155824"
|
||||
entries = [{"path": f"{capture}/part{part:04d}/{capture}_{suffix}"}
|
||||
for part in (1, 2)
|
||||
for suffix in (f"camera_left_part{part:04d}.mp4", f"camera_left_part{part:04d}_pts.csv",
|
||||
f"audio_part{part:04d}.wav", f"audio_part{part:04d}_pts.csv", f"imu_part{part:04d}.csv")]
|
||||
for part in (1, 2):
|
||||
video = f"{capture}/part{part:04d}/{capture}_camera_left_part{part:04d}.mp4"
|
||||
paired = orbbec_operator._paired_files(video, entries)
|
||||
assert all(f"part{part:04d}" in paired[key] for key in ("pts", "audio", "audioPts", "imu"))
|
||||
transformed = video.removesuffix(".mp4") + "_silent_480p18fps_wm.mp4"
|
||||
with pytest.raises(RuntimeError, match="转码视频缺少逐帧对应的 PTS"):
|
||||
orbbec_operator._paired_files(transformed, entries)
|
||||
exact = transformed.removesuffix(".mp4") + "_pts.csv"
|
||||
assert orbbec_operator._paired_files(transformed, entries + [{"path": exact}])["pts"] == exact
|
||||
|
||||
|
||||
def test_normalizer_rejects_ambiguous_sidecars():
|
||||
import pytest
|
||||
with pytest.raises(RuntimeError, match="配套文件不唯一"):
|
||||
orbbec_operator._paired_files("capture_camera_left_part0001.mp4", [
|
||||
{"path": "a/capture_camera_left_part0001_pts.csv"},
|
||||
{"path": "b/capture_camera_left_part0001_pts.csv"},
|
||||
])
|
||||
|
||||
|
||||
def test_directory_reader_multiport_emits_real_collection_artifacts(tmp_path):
|
||||
source = tmp_path / "capture"
|
||||
source.mkdir()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user