File size: 2,603 Bytes
14c58d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Stage 1 - register every video in datasets/ with metadata, checksum and a
rights record. IDs are stable across reruns: an existing file (same sha256)
keeps its source_id and session_id; new files get the next number."""
from datetime import datetime
from pathlib import Path

from common import clean_title, probe_video, read_json, rel, sha256_file, write_json

VIDEO_EXT = {".mp4", ".mkv", ".mov", ".avi", ".webm", ".m4v"}


def settings_for(cfg, folder_name, file_name):
    cat = dict(cfg["categories"].get(folder_name) or {
        "category": folder_name.lower().replace(" ", "_"), "view_description": "a cricket video",
        "time_scale": 1.0})
    for key, over in (cfg.get("overrides") or {}).items():
        if key in file_name:
            cat.update(over)
    return cat


def run_ingest(cfg):
    datasets = cfg["_paths"]["datasets"]
    reg_path = cfg["_paths"]["results"] / "registry" / "sources.json"
    old = {s["sha256"]: s for s in (read_json(reg_path) if reg_path.exists() else [])}
    next_n = max([int(s["source_id"].split("_")[-1]) for s in old.values()] or [0]) + 1

    files = sorted(p for p in datasets.rglob("*") if p.suffix.lower() in VIDEO_EXT and p.is_file())
    if not files:
        raise SystemExit(f"No videos found under {rel(datasets)}/ - put your category folders there.")
    sources = []
    for f in files:
        folder = f.relative_to(datasets).parts[0] if len(f.relative_to(datasets).parts) > 1 else "uncategorised"
        print(f"  ingest {rel(f)}")
        sha = sha256_file(f)
        meta = probe_video(f)
        st = settings_for(cfg, folder, f.name)
        if sha in old:
            sid, sess = old[sha]["source_id"], old[sha]["session_id"]
        else:
            n = next_n; next_n += 1
            sid = f"SRC_YT_{n:04d}"
            day = datetime.fromtimestamp(f.stat().st_mtime).strftime("%Y%m%d")
            sess = f"{cfg['project']['site_short']}_{day}_S{n:03d}"
        sources.append({
            "source_id": sid, "session_id": sess, "path": rel(f), "folder": folder,
            "title": clean_title(f.name), "sha256": sha, **meta,
            "category": st["category"], "view_description": st.get("view_description"),
            "time_scale": float(st.get("time_scale", 1.0)),
            "time_scale_verified": bool(st.get("time_scale_verified", st.get("time_scale", 1.0) == 1.0)),
            "pose_imgsz": st.get("pose_imgsz"),
            "rights": {"source_id": sid, **cfg["rights_default"]},
        })
    write_json(reg_path, sources)
    print(f"  {len(sources)} sources -> {rel(reg_path)}")
    return sources