feat: append source clip name to annotated output filename
Output was a fixed name (merged_annotated.mp4) so every download looked the same. The source clip name is now appended at the back: merged_annotated_rekap_2026-10-27.mp4 - src/naming.py: annotated_output_name() + source_stem(); client upload name wins, stored file name is the fallback (reuse endpoint), unsafe chars replaced, stem capped at 80 chars - src/job.py: Job.source_stem + add_job(source_stem=...); both output paths go through the helper - app.py: capture the sanitized client filename before the collision suffix is added - cli.py: same helper for the CLI output name Verified live on .93: POST /api/analyze with 'rekap 2026-10-27.mp4' -> output/job-e0409b05/merged_annotated_rekap_2026-10-27.mp4, download 200.
This commit is contained in:
1 parent
dbaa87c4e8
commit
e963ba0cb4
6 files changed
+174
-4
No files matched your search
@@ -135,7 +135,10 @@ def _save_and_queue(video, model_configs, class_filters,
|
||||
output_height, zone_mode, zone_preset):
|
||||
"""Save upload (collision-safe), transcode to 720p, resolve fixed zone,
|
||||
enqueue job. Shared by /upload and /api/analyze."""
|
||||
video_path = os.path.join(UPLOAD_DIR, secure_filename(video.filename))
|
||||
safe_name = secure_filename(video.filename)
|
||||
# Client name without extension — used for the annotated output name.
|
||||
source_stem = os.path.splitext(safe_name)[0]
|
||||
video_path = os.path.join(UPLOAD_DIR, safe_name)
|
||||
base, ext = os.path.splitext(video_path)
|
||||
n = 1
|
||||
while os.path.exists(video_path):
|
||||
@@ -160,6 +163,7 @@ def _save_and_queue(video, model_configs, class_filters,
|
||||
|
||||
return job_queue.add_job(
|
||||
video_path=video_path,
|
||||
source_stem=source_stem,
|
||||
model_configs=model_configs,
|
||||
class_filters=class_filters,
|
||||
zone_mode=zone_mode,
|
||||
|
||||
@@ -7,6 +7,7 @@ import os
|
||||
import sys
|
||||
|
||||
from src.model_registry import ModelConfig, scan_models
|
||||
from src.naming import annotated_output_name
|
||||
from src.pipeline import run_pipeline
|
||||
|
||||
|
||||
@@ -96,7 +97,10 @@ def main(argv: list[str] | None = None) -> None:
|
||||
if args.output and len(selected) == 1:
|
||||
out_path = args.output
|
||||
else:
|
||||
out_path = os.path.join(args.output_dir, f"{model_cfg.stem}_annotated.mp4")
|
||||
out_path = os.path.join(
|
||||
args.output_dir,
|
||||
annotated_output_name(f"{model_cfg.stem}_annotated", args.video),
|
||||
)
|
||||
parent = os.path.dirname(out_path)
|
||||
if parent:
|
||||
os.makedirs(parent, exist_ok=True)
|
||||
|
||||
+12
-2
@@ -12,6 +12,7 @@ from enum import Enum, auto
|
||||
from pathlib import Path
|
||||
|
||||
from src.model_registry import ModelConfig, find_truck_detector
|
||||
from src.naming import annotated_output_name
|
||||
from src.pipeline import run_pipeline, run_merged_pipeline, PipelineResult, MergedPipelineResult
|
||||
from src.truck_roi import TruckROI
|
||||
|
||||
@@ -51,6 +52,7 @@ class Job:
|
||||
model_configs: list[ModelConfig]
|
||||
class_filters: dict[str, list[str] | None] = field(default_factory=dict)
|
||||
output_dir: str = ""
|
||||
source_stem: str = ""
|
||||
status: JobStatus = JobStatus.PENDING
|
||||
progress: float = 0.0
|
||||
current_model: str = ""
|
||||
@@ -116,6 +118,7 @@ class JobQueue:
|
||||
video_path: str,
|
||||
model_configs: list[ModelConfig],
|
||||
class_filters: dict[str, list[str] | None] | None = None,
|
||||
source_stem: str = "",
|
||||
zone_mode: str = "dynamic",
|
||||
fixed_zone: TruckROI | None = None,
|
||||
zone_preset: str = "",
|
||||
@@ -128,6 +131,7 @@ class JobQueue:
|
||||
video_path=video_path,
|
||||
model_configs=list(model_configs),
|
||||
class_filters=class_filters or {},
|
||||
source_stem=source_stem,
|
||||
output_dir=str(self._output_dir / job_id),
|
||||
zone_mode=zone_mode,
|
||||
fixed_zone=fixed_zone,
|
||||
@@ -229,7 +233,10 @@ class JobQueue:
|
||||
if "truck" not in (model_cfg.known_classes or []):
|
||||
truck_model_config = find_truck_detector("./models")
|
||||
|
||||
output_path = os.path.join(job.output_dir, f"{model_cfg.stem}_annotated.mp4")
|
||||
output_path = os.path.join(
|
||||
job.output_dir,
|
||||
annotated_output_name(f"{model_cfg.stem}_annotated", job.video_path, job.source_stem),
|
||||
)
|
||||
|
||||
def _on_live_stats(stats):
|
||||
with self._lock:
|
||||
@@ -275,7 +282,10 @@ class JobQueue:
|
||||
with self._lock:
|
||||
job.current_model = f"Processing {total_models} models (merged)..."
|
||||
|
||||
output_path = os.path.join(job.output_dir, "merged_annotated.mp4")
|
||||
output_path = os.path.join(
|
||||
job.output_dir,
|
||||
annotated_output_name("merged_annotated", job.video_path, job.source_stem),
|
||||
)
|
||||
|
||||
def _on_live_stats_merged(stats):
|
||||
with self._lock:
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Output filename helpers — source clip name appended to the annotated base name.
|
||||
|
||||
The annotated output used to be a fixed name (`merged_annotated.mp4`), so every
|
||||
job's download looked the same. The source clip's name is now appended at the
|
||||
back: `merged_annotated_clip_2026-10-27.mp4`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
# Keep filenames boring: no spaces, no separators, no unicode surprises.
|
||||
_SAFE = re.compile(r"[^A-Za-z0-9._-]+")
|
||||
|
||||
# base name + stem + ".mp4" must stay under the 255-byte filename limit.
|
||||
MAX_STEM = 80
|
||||
|
||||
|
||||
def source_stem(video_path: str, explicit: str = "") -> str:
|
||||
"""Stem of the source clip, without extension.
|
||||
|
||||
`explicit` (the client upload name) wins; otherwise fall back to the
|
||||
stored file name — e.g. the reuse endpoint re-analysing an older upload.
|
||||
"""
|
||||
raw = explicit or os.path.splitext(os.path.basename(video_path))[0]
|
||||
stem = _SAFE.sub("_", raw).strip("._")
|
||||
return stem[:MAX_STEM]
|
||||
|
||||
|
||||
def annotated_output_name(
|
||||
base: str, video_path: str, explicit_stem: str = "", ext: str = ".mp4"
|
||||
) -> str:
|
||||
"""`base` + source stem -> 'merged_annotated_clip_2026-10-27.mp4'.
|
||||
|
||||
Falls back to `base` alone when no usable source name exists.
|
||||
"""
|
||||
stem = source_stem(video_path, explicit_stem)
|
||||
return f"{base}_{stem}{ext}" if stem else f"{base}{ext}"
|
||||
@@ -408,3 +408,78 @@ def test_orphan_registration_off(tmp_path):
|
||||
|
||||
q = JobQueue(output_dir=str(tmp_path), register_orphans=False)
|
||||
assert q.get_job("job-abcd1234") is None
|
||||
|
||||
|
||||
# ── Output naming (source clip name appended) ─────────────────────────
|
||||
|
||||
def test_single_model_output_name_uses_source_stem(tmp_path, monkeypatch):
|
||||
"""Annotated file name carries the source clip name."""
|
||||
import os
|
||||
import src.job as job_mod
|
||||
from src.job import Job, JobQueue
|
||||
from src.model_registry import ModelConfig
|
||||
from src.pipeline import PipelineResult
|
||||
|
||||
seen = {}
|
||||
|
||||
def fake(**kwargs):
|
||||
seen.update(kwargs)
|
||||
return PipelineResult(
|
||||
output_path=kwargs["output_path"], frame_count=10, loading_count=0,
|
||||
unloading_count=0, batch_count=0, duration_seconds=1.0,
|
||||
model_name="a.pt", class_filter=None,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(job_mod, "run_pipeline", fake)
|
||||
|
||||
q = JobQueue(output_dir=str(tmp_path))
|
||||
job = Job(
|
||||
job_id="job-nm-single",
|
||||
video_path="/tmp/clip_1.mp4",
|
||||
model_configs=[ModelConfig(filename="a.pt", path=str(tmp_path / "a.pt"),
|
||||
stem="a", known_classes=["sack"])],
|
||||
output_dir=str(tmp_path / "job-nm-single"),
|
||||
source_stem="clip",
|
||||
)
|
||||
q._jobs[job.job_id] = job
|
||||
q._run_job(job.job_id)
|
||||
|
||||
assert os.path.basename(seen["output_path"]) == "a_annotated_clip.mp4"
|
||||
|
||||
|
||||
def test_merged_output_name_falls_back_to_stored_file_name(tmp_path, monkeypatch):
|
||||
"""Without a client name the stored file name is used."""
|
||||
import os
|
||||
import src.job as job_mod
|
||||
from src.job import Job, JobQueue
|
||||
from src.model_registry import ModelConfig
|
||||
from src.pipeline import MergedPipelineResult
|
||||
|
||||
seen = {}
|
||||
|
||||
def fake(**kwargs):
|
||||
seen.update(kwargs)
|
||||
return MergedPipelineResult(
|
||||
output_path=kwargs["output_path"], frame_count=10, loading_count=0,
|
||||
unloading_count=0, batch_count=0, duration_seconds=1.0,
|
||||
model_names=["a.pt"], class_filter=None,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(job_mod, "run_merged_pipeline", fake)
|
||||
|
||||
q = JobQueue(output_dir=str(tmp_path))
|
||||
job = Job(
|
||||
job_id="job-nm-merged",
|
||||
video_path="/tmp/clip_1.mp4",
|
||||
model_configs=[
|
||||
ModelConfig(filename="a.pt", path=str(tmp_path / "a.pt"), stem="a",
|
||||
known_classes=["sack"]),
|
||||
ModelConfig(filename="b.pt", path=str(tmp_path / "b.pt"), stem="b",
|
||||
known_classes=["sack"]),
|
||||
],
|
||||
output_dir=str(tmp_path / "job-nm-merged"),
|
||||
)
|
||||
q._jobs[job.job_id] = job
|
||||
q._run_job(job.job_id)
|
||||
|
||||
assert os.path.basename(seen["output_path"]) == "merged_annotated_clip_1.mp4"
|
||||
@@ -0,0 +1,38 @@
|
||||
"""Output filename helpers (src/naming.py)."""
|
||||
|
||||
from src.naming import MAX_STEM, annotated_output_name, source_stem
|
||||
|
||||
|
||||
def test_client_name_wins_over_stored_file_name():
|
||||
"""The upload name the operator sees wins over the on-disk name."""
|
||||
assert annotated_output_name("merged_annotated", "/uploads/clip_1.mp4", "clip") == (
|
||||
"merged_annotated_clip.mp4"
|
||||
)
|
||||
|
||||
|
||||
def test_falls_back_to_stored_file_name():
|
||||
"""No client name (reuse endpoint) -> stored file name is used."""
|
||||
assert annotated_output_name("merged_annotated", "/uploads/clip_1.mp4") == (
|
||||
"merged_annotated_clip_1.mp4"
|
||||
)
|
||||
|
||||
|
||||
def test_extension_is_dropped():
|
||||
assert source_stem("/uploads/clip.mp4") == "clip"
|
||||
|
||||
|
||||
def test_unsafe_characters_are_replaced():
|
||||
assert source_stem("/uploads/my clip!.mp4") == "my_clip"
|
||||
|
||||
|
||||
def test_long_stem_is_truncated():
|
||||
stem = source_stem("/uploads/" + "a" * 200 + ".mp4")
|
||||
assert len(stem) == MAX_STEM
|
||||
|
||||
|
||||
def test_empty_stem_falls_back_to_base_name():
|
||||
assert annotated_output_name("merged_annotated", "") == "merged_annotated.mp4"
|
||||
|
||||
|
||||
def test_extension_is_configurable():
|
||||
assert annotated_output_name("out", "/uploads/clip.mp4", ext=".mkv") == "out_clip.mkv"
|
||||
Reference in new issue
Block a user