-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_corpus.py
More file actions
254 lines (217 loc) · 7.97 KB
/
Copy pathfetch_corpus.py
File metadata and controls
254 lines (217 loc) · 7.97 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from __future__ import annotations
import argparse
import shlex
import shutil
import subprocess
import tomllib
import urllib.request
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Literal
ManifestMethod = Literal["http", "yt-dlp", "generated"]
@dataclass(frozen=True, slots=True)
class ClipSpec:
id: str
tier: str
method: ManifestMethod
local_path: Path
intended_use: str
download_url: str | None = None
download_command: str | None = None
needs_local_encode: bool = False
expected_sha256: str | None = None
@dataclass(frozen=True, slots=True)
class DerivedEncode:
id: str
video_codec: str
preset: str
crf: int
gop: int
pixel_format: str
@dataclass(frozen=True, slots=True)
class CorpusManifest:
clips: tuple[ClipSpec, ...]
derived_encodes: tuple[DerivedEncode, ...]
def _load_manifest(path: Path) -> CorpusManifest:
payload = tomllib.loads(path.read_text())
raw_clips = payload.get("clip", [])
raw_encodes = payload.get("derived_encode", [])
clips = tuple(_parse_clip(raw_clip) for raw_clip in raw_clips)
derived_encodes = tuple(_parse_derived_encode(raw_encode) for raw_encode in raw_encodes)
return CorpusManifest(clips=clips, derived_encodes=derived_encodes)
def _parse_clip(raw_clip: Any) -> ClipSpec:
if not isinstance(raw_clip, dict):
raise ValueError(f"clip entry must be a table, got {type(raw_clip)!r}")
raw_method = raw_clip.get("method")
if raw_method not in {"http", "yt-dlp", "generated"}:
raise ValueError(f"unsupported clip method: {raw_method!r}")
raw_path = raw_clip.get("local_path")
if not isinstance(raw_path, str):
raise ValueError(f"clip local_path must be a string, got {raw_path!r}")
return ClipSpec(
id=_expect_str(raw_clip, "id"),
tier=_expect_str(raw_clip, "tier"),
method=raw_method,
local_path=Path(raw_path),
intended_use=_expect_str(raw_clip, "intended_use"),
download_url=_optional_str(raw_clip, "download_url"),
download_command=_optional_str(raw_clip, "download_command"),
needs_local_encode=bool(raw_clip.get("needs_local_encode", False)),
expected_sha256=_optional_str(raw_clip, "expected_sha256"),
)
def _parse_derived_encode(raw_encode: Any) -> DerivedEncode:
if not isinstance(raw_encode, dict):
raise ValueError(f"derived_encode entry must be a table, got {type(raw_encode)!r}")
return DerivedEncode(
id=_expect_str(raw_encode, "id"),
video_codec=_expect_str(raw_encode, "video_codec"),
preset=_expect_str(raw_encode, "preset"),
crf=_expect_int(raw_encode, "crf"),
gop=_expect_int(raw_encode, "gop"),
pixel_format=_expect_str(raw_encode, "pixel_format"),
)
def _expect_str(payload: dict[str, Any], key: str) -> str:
value = payload.get(key)
if not isinstance(value, str) or not value:
raise ValueError(f"expected non-empty string for {key}, got {value!r}")
return value
def _optional_str(payload: dict[str, Any], key: str) -> str | None:
value = payload.get(key)
if value is None:
return None
if not isinstance(value, str) or not value:
raise ValueError(f"expected optional string for {key}, got {value!r}")
return value
def _expect_int(payload: dict[str, Any], key: str) -> int:
value = payload.get(key)
if not isinstance(value, int):
raise ValueError(f"expected int for {key}, got {value!r}")
return value
def _run(command: list[str], *, dry_run: bool) -> None:
print("$", " ".join(shlex.quote(part) for part in command))
if dry_run:
return
subprocess.run(command, check=True)
def _download_http(url: str, destination: Path, *, force: bool, dry_run: bool) -> None:
destination.parent.mkdir(parents=True, exist_ok=True)
if destination.exists() and not force:
print(f"skip existing {destination}")
return
print(f"download {url} -> {destination}")
if dry_run:
return
with urllib.request.urlopen(url) as response, destination.open("wb") as output:
shutil.copyfileobj(response, output)
def _download_crosscheck(command_text: str, *, dry_run: bool) -> None:
command = shlex.split(command_text)
if not command:
raise ValueError("empty cross-check download command")
_run(command, dry_run=dry_run)
def _encode_clip(source: Path, encode: DerivedEncode, *, force: bool, dry_run: bool) -> None:
if not source.exists() and not dry_run:
raise FileNotFoundError(f"cannot encode missing source clip: {source}")
output_name = f"{source.stem}_{encode.id}.mp4"
destination = Path("data/corpus/derived") / output_name
destination.parent.mkdir(parents=True, exist_ok=True)
if destination.exists() and not force:
print(f"skip existing {destination}")
return
command = [
"ffmpeg",
"-y",
"-i",
str(source),
"-c:v",
encode.video_codec,
"-preset",
encode.preset,
"-crf",
str(encode.crf),
"-g",
str(encode.gop),
"-pix_fmt",
encode.pixel_format,
str(destination),
]
_run(command, dry_run=dry_run)
def _selected_clips(
manifest: CorpusManifest,
*,
tiers: set[str],
clip_ids: set[str],
) -> list[ClipSpec]:
selected: list[ClipSpec] = []
for clip in manifest.clips:
if tiers and clip.tier not in tiers:
continue
if clip_ids and clip.id not in clip_ids:
continue
selected.append(clip)
return selected
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Fetch or generate local corpus assets.")
parser.add_argument(
"--manifest",
type=Path,
default=Path("data/corpus/manifest.toml"),
help="Path to the corpus manifest.",
)
parser.add_argument(
"--tier",
action="append",
default=[],
help="Tier(s) to fetch, for example: primary, crosscheck, synthetic.",
)
parser.add_argument(
"--clip-id",
action="append",
default=[],
help="Specific clip id(s) to fetch.",
)
parser.add_argument(
"--encode",
action="store_true",
help="Encode derived H.264 assets for primary raw clips after download.",
)
parser.add_argument(
"--force",
action="store_true",
help="Overwrite existing local files.",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Print actions without executing them.",
)
return parser.parse_args()
def main() -> None:
args = _parse_args()
manifest = _load_manifest(args.manifest)
tiers = set(args.tier)
clip_ids = set(args.clip_id)
selected = _selected_clips(manifest, tiers=tiers, clip_ids=clip_ids)
if not selected:
raise SystemExit("no matching clips selected")
for clip in selected:
if clip.method == "http":
if clip.download_url is None:
raise ValueError(f"http clip {clip.id} is missing download_url")
_download_http(
clip.download_url, clip.local_path, force=args.force, dry_run=args.dry_run
)
if args.encode and clip.needs_local_encode:
for encode in manifest.derived_encodes:
_encode_clip(clip.local_path, encode, force=args.force, dry_run=args.dry_run)
elif clip.method == "yt-dlp":
if clip.download_command is None:
raise ValueError(f"yt-dlp clip {clip.id} is missing download_command")
_download_crosscheck(clip.download_command, dry_run=args.dry_run)
elif clip.method == "generated":
print(
f"{clip.id}: generated asset, use "
"'uv run python scripts/generate_synthetic_corpus.py' instead"
)
else:
raise ValueError(f"unsupported clip method: {clip.method!r}")
if __name__ == "__main__":
main()