File size: 6,386 Bytes
5fb1693
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b333a3
5fb1693
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
"""Download source videos and reconstruct annotation-aligned Sekai2 clips."""

from __future__ import annotations

import argparse
import csv
import json
import shutil
import subprocess
from collections import defaultdict
from pathlib import Path

DEFAULT_FORMAT = (
    "bestvideo[height<=720][fps<=30]+bestaudio/"
    "best[height<=720][fps<=30]"
)


def run(command: list[str]) -> None:
    subprocess.run(command, check=True)


def read_rows(path: Path, clip_ids: set[str], dataset: str | None, limit: int | None):
    rows = []
    with path.open(encoding="utf-8-sig", newline="") as handle:
        for row in csv.DictReader(handle):
            if dataset and row["dataset"] != dataset:
                continue
            if clip_ids and row["clip_id"] not in clip_ids:
                continue
            rows.append(row)
            if limit is not None and len(rows) >= limit:
                break
    if clip_ids:
        found = {row["clip_id"] for row in rows}
        missing = sorted(clip_ids - found)
        if missing:
            raise ValueError(f"clip_id not found: {missing}")
    return rows


def find_cached_source(cache_dir: Path, video_id: str) -> Path | None:
    candidates = [
        path
        for path in cache_dir.glob(video_id + ".*")
        if path.suffix not in {".json", ".part", ".ytdl"}
    ]
    return sorted(candidates)[0] if candidates else None


def download_source(
    row: dict[str, str], cache_dir: Path, yt_dlp: str, format_selector: str
) -> Path:
    existing = find_cached_source(cache_dir, row["video_id"])
    if existing:
        return existing
    template = str(cache_dir / (row["video_id"] + ".%(ext)s"))
    command = [
        yt_dlp,
        "--no-playlist",
        "--no-part",
        "--write-info-json",
        "--merge-output-format",
        "mkv",
        "-f",
        format_selector,
        "-o",
        template,
        row["url"],
    ]
    run(command)
    downloaded = find_cached_source(cache_dir, row["video_id"])
    if downloaded is None:
        raise RuntimeError(f"yt-dlp produced no media file for {row['video_id']}")
    return downloaded


def reconstruct_one(row: dict[str, str], source: Path, output: Path, ffmpeg: str) -> None:
    output.parent.mkdir(parents=True, exist_ok=True)
    command = [
        ffmpeg,
        "-y",
        "-nostdin",
        "-loglevel",
        "error",
        "-i",
        str(source),
        # Output-side -ss is slower but frame accurate.  Do not move it before -i.
        "-ss",
        row["start_time"],
        "-t",
        row["duration"],
        "-map",
        "0:v:0",
        "-map",
        "0:a:0?",
        "-vf",
        (
            f"fps={row['fps']},"
            f"scale={row['frame_width']}:{row['frame_height']}:flags=lanczos"
        ),
        "-frames:v",
        row["num_frames"],
        "-fps_mode",
        "cfr",
        "-c:v",
        "libx264",
        "-preset",
        "medium",
        "-crf",
        "18",
        "-pix_fmt",
        "yuv420p",
        "-c:a",
        "aac",
        "-ar",
        "48000",
        "-ac",
        "2",
        "-movflags",
        "+faststart",
        str(output),
    ]
    run(command)


def probe_video(path: Path, ffprobe: str) -> dict[str, object]:
    command = [
        ffprobe,
        "-v",
        "error",
        "-select_streams",
        "v:0",
        "-count_frames",
        "-show_entries",
        "stream=width,height,avg_frame_rate,nb_read_frames",
        "-of",
        "json",
        str(path),
    ]
    output = subprocess.run(command, check=True, capture_output=True, text=True).stdout
    return json.loads(output)["streams"][0]


def validate_encoded(row: dict[str, str], output: Path, ffprobe: str) -> None:
    stream = probe_video(output, ffprobe)
    numerator, denominator = map(int, stream["avg_frame_rate"].split("/"))
    fps = numerator / denominator
    expected = {
        "width": int(row["frame_width"]),
        "height": int(row["frame_height"]),
        "frames": int(row["num_frames"]),
    }
    actual = {
        "width": int(stream["width"]),
        "height": int(stream["height"]),
        "frames": int(stream["nb_read_frames"]),
    }
    if actual != expected or abs(fps - float(row["fps"])) > 1e-6:
        raise RuntimeError(
            f"encoded verification failed for {output}: expected={expected}, "
            f"actual={actual}, fps={fps}"
        )


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("manifest", type=Path)
    parser.add_argument("--output-dir", type=Path, required=True)
    parser.add_argument("--cache-dir", type=Path, required=True)
    parser.add_argument("--clip-id", action="append", default=[])
    parser.add_argument("--dataset", choices=("sekai", "sekai2"))
    parser.add_argument("--limit", type=int)
    parser.add_argument("--format", default=DEFAULT_FORMAT)
    parser.add_argument("--yt-dlp", default="yt-dlp")
    parser.add_argument("--ffmpeg", default="ffmpeg")
    parser.add_argument("--ffprobe", default="ffprobe")
    parser.add_argument(
        "--remove-source",
        action="store_true",
        help="Delete each cached source after all selected clips are reconstructed.",
    )
    args = parser.parse_args()

    for executable in (args.yt_dlp, args.ffmpeg, args.ffprobe):
        if shutil.which(executable) is None:
            raise RuntimeError(f"required executable not found: {executable}")

    rows = read_rows(args.manifest, set(args.clip_id), args.dataset, args.limit)
    if not rows:
        raise RuntimeError("no perspective rows selected")
    args.cache_dir.mkdir(parents=True, exist_ok=True)

    grouped: dict[str, list[dict[str, str]]] = defaultdict(list)
    for row in rows:
        grouped[row["video_id"]].append(row)

    for video_rows in grouped.values():
        source = download_source(video_rows[0], args.cache_dir, args.yt_dlp, args.format)
        for row in video_rows:
            output = args.output_dir / row["dataset"] / row["clip_id"]
            reconstruct_one(row, source, output, args.ffmpeg)
            validate_encoded(row, output, args.ffprobe)
            print(f"OK {row['clip_id']} -> {output}")
        if args.remove_source:
            source.unlink()
    return 0


if __name__ == "__main__":
    raise SystemExit(main())