-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_packaging.py
More file actions
399 lines (364 loc) · 11.5 KB
/
Copy pathtest_packaging.py
File metadata and controls
399 lines (364 loc) · 11.5 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
import pytest
from conftest import run_cli
from generate_fixtures import generate_image, generate_music
from video_editor import media
TIMELINE = [
{"source": "src-1", "in": 0.5, "out": 2.3, "reason": "a"},
{"source": "src-1", "in": 3.0, "out": 4.4, "reason": "b"},
{"source": "src-1", "in": 5.2, "out": 6.8, "reason": "c"},
]
@pytest.fixture(scope="module")
def pkg(
main_video_module: Path, tmp_path_factory: pytest.TempPathFactory
) -> dict[str, Any]:
"""Profile YAML, music asset, plan, preview render manifest, transcript."""
tmp = tmp_path_factory.mktemp("packaging")
music = generate_music(tmp / "music.wav")
profile_path = tmp / "profile.yaml"
profile_path.write_text(
f"""
schema_version: "1"
profiles:
master:
canvas: {{width: 640, height: 360, frame_rate: 30}}
video_codec: {{name: libx264, crf: 22, preset: veryfast}}
audio_codec: {{name: aac, bitrate: 160k}}
loudness: {{integrated_lufs: -16, true_peak_dbtp: -1.5, lra: 11}}
subtitles: {{mode: mux}}
music:
path: {music.name}
gain_db: -20
ducking: {{threshold: 0.02, ratio: 8}}
"""
)
plan = {
"schema_version": "1",
"plan_id": "pkg-plan",
"sources": [{"id": "src-1", "path": str(main_video_module)}],
"timeline": TIMELINE,
}
plan_path = tmp / "plan.json"
plan_path.write_text(json.dumps(plan))
preview = tmp / "preview.mp4"
code, _ = run_cli(
["render", "preview", "--plan", str(plan_path), "--output", str(preview)]
)
assert code == 0
from video_editor.provenance import sha256_file
transcript = {
"schema_version": "1",
"source": {
"path": str(main_video_module),
"sha256": sha256_file(main_video_module),
"source_id": "src-1",
},
"language": "en",
"backend": "fixture",
"model": "fake",
"created_at": "2026-07-12T00:00:00+00:00",
"segments": [
{
"start": 0.5,
"end": 2.3,
"text": "hello there world",
"words": [
{"text": "hello", "start": 0.6, "end": 1.0, "confidence": 0.9},
{"text": "there", "start": 1.1, "end": 1.5, "confidence": 0.9},
{"text": "world", "start": 1.6, "end": 2.2, "confidence": 0.9},
],
},
{
"start": 2.4,
"end": 2.9,
"text": "dropped words",
"words": [
{"text": "dropped", "start": 2.4, "end": 2.6, "confidence": 0.9},
{"text": "words", "start": 2.65, "end": 2.9, "confidence": 0.9},
],
},
{
"start": 3.1,
"end": 4.3,
"text": "second kept segment",
"words": [
{"text": "second", "start": 3.1, "end": 3.5, "confidence": 0.9},
{"text": "kept", "start": 3.6, "end": 3.9, "confidence": 0.9},
{"text": "segment", "start": 4.0, "end": 4.3, "confidence": 0.9},
],
},
],
}
transcript_path = tmp / "transcript.json"
transcript_path.write_text(json.dumps(transcript))
return {
"tmp": tmp,
"profile": profile_path,
"plan": plan_path,
"manifest": tmp / "preview.mp4.manifest.json",
"transcript": transcript_path,
}
def test_render_master_with_profile_and_music(pkg: dict[str, Any]) -> None:
out = pkg["tmp"] / "master.mp4"
code, envelope = run_cli(
[
"render",
"master",
"--plan",
str(pkg["plan"]),
"--profile",
str(pkg["profile"]),
"--profile-name",
"master",
"--output",
str(out),
]
)
assert code == 0
info = media.probe(out)
video = next(s for s in info["streams"] if s["type"] == "video")
assert (video["width"], video["height"]) == (640, 360)
assert info["duration_seconds"] == pytest.approx(4.8, abs=0.3)
code, envelope = run_cli(
[
"output",
"validate",
"--input",
str(out),
"--profile",
str(pkg["profile"]),
"--profile-name",
"master",
"--expect-duration",
"4.8",
]
)
assert code == 0
assert envelope["data"]["passed"] is True, envelope["data"]["issues"]
def test_render_master_unknown_profile(pkg: dict[str, Any]) -> None:
code, envelope = run_cli(
[
"render",
"master",
"--plan",
str(pkg["plan"]),
"--profile",
str(pkg["profile"]),
"--profile-name",
"nope",
"--output",
str(pkg["tmp"] / "x.mp4"),
]
)
assert code != 0
assert "unknown render profile" in envelope["error"]["message"]
def test_profile_missing_music_rejected(pkg: dict[str, Any], tmp_path: Path) -> None:
bad = tmp_path / "bad.yaml"
bad.write_text(
'schema_version: "1"\n'
"profiles:\n master:\n canvas: {width: 640, height: 360}\n"
"music:\n path: missing.wav\n"
)
code, envelope = run_cli(
[
"render",
"master",
"--plan",
str(pkg["plan"]),
"--profile",
str(bad),
"--profile-name",
"master",
"--output",
str(tmp_path / "x.mp4"),
]
)
assert code != 0
assert "music file not found" in envelope["error"]["message"]
def test_subtitles_create_maps_through_cuts(
pkg: dict[str, Any], tmp_path: Path
) -> None:
srt, vtt = tmp_path / "subs.srt", tmp_path / "subs.vtt"
code, envelope = run_cli(
[
"subtitles",
"create",
"--manifest",
str(pkg["manifest"]),
"--transcript",
str(pkg["transcript"]),
"--output-srt",
str(srt),
"--output-vtt",
str(vtt),
]
)
assert code == 0
data = envelope["data"]
# "dropped words" (2.4-2.9) is removed; two cues survive.
assert data["cue_count"] == 2
# First cue: words 0.6-2.2 in a clip starting at source 0.5 -> output 0.1-1.7
assert data["first_cue_start"] == pytest.approx(0.1, abs=0.01)
# Second cue: words 3.1-4.3 in clip [3.0, 4.4) at output offset 1.8 -> 1.9-3.1
assert data["last_cue_end"] == pytest.approx(3.1, abs=0.01)
text = srt.read_text()
assert "hello there world" in text
assert "dropped" not in text
assert vtt.read_text().startswith("WEBVTT")
def test_subtitles_create_reflows_for_short_form(
pkg: dict[str, Any], tmp_path: Path
) -> None:
srt = tmp_path / "short.srt"
code, envelope = run_cli(
[
"subtitles",
"create",
"--manifest",
str(pkg["manifest"]),
"--transcript",
str(pkg["transcript"]),
"--output-srt",
str(srt),
"--output-vtt",
str(tmp_path / "short.vtt"),
"--max-words",
"2",
"--max-chars",
"12",
"--max-duration",
"1.0",
]
)
assert code == 0
assert envelope["data"]["cue_count"] > 2
cue_lines = [
line
for line in srt.read_text().splitlines()
if line and "-->" not in line and not line.isdigit()
]
assert all(len(line.split()) <= 2 for line in cue_lines)
assert all(len(line) <= 12 for line in cue_lines)
def test_subtitles_render_mux_and_validate(pkg: dict[str, Any], tmp_path: Path) -> None:
srt = tmp_path / "subs.srt"
run_cli(
[
"subtitles",
"create",
"--manifest",
str(pkg["manifest"]),
"--transcript",
str(pkg["transcript"]),
"--output-srt",
str(srt),
"--output-vtt",
str(tmp_path / "subs.vtt"),
]
)
out = tmp_path / "subtitled.mp4"
code, _ = run_cli(
[
"subtitles",
"render",
"--input",
str(pkg["tmp"] / "preview.mp4"),
"--subtitles",
str(srt),
"--output",
str(out),
"--mode",
"mux",
]
)
assert code == 0
types = {s["type"] for s in media.probe(out)["streams"]}
assert "subtitle" in types
code, envelope = run_cli(
[
"output",
"validate",
"--input",
str(out),
"--expect-subtitles",
"--subtitles",
str(srt),
]
)
assert code == 0
assert envelope["data"]["passed"] is True, envelope["data"]["issues"]
def test_subtitles_render_rejects_style_in_mux_mode(
pkg: dict[str, Any], tmp_path: Path
) -> None:
srt = tmp_path / "subs.srt"
srt.write_text("1\n00:00:00,000 --> 00:00:01,000\nhello\n")
code, envelope = run_cli(
[
"subtitles",
"render",
"--input",
str(pkg["tmp"] / "preview.mp4"),
"--subtitles",
str(srt),
"--output",
str(tmp_path / "styled.mp4"),
"--mode",
"mux",
"--font",
"Arial",
]
)
assert code != 0
assert "style options require --mode burn" in envelope["error"]["message"]
def test_asset_inspect(tmp_path: Path, audio_only: Path) -> None:
image = generate_image(tmp_path / "logo.png")
code, envelope = run_cli(["asset", "inspect", "--input", str(image)])
assert code == 0
assert envelope["data"]["kind"] == "image"
code, envelope = run_cli(["asset", "inspect", "--input", str(audio_only)])
assert code == 0
assert envelope["data"]["kind"] == "audio"
font = tmp_path / "font.ttf"
font.write_bytes(b"\x00\x01\x00\x00fake")
code, envelope = run_cli(["asset", "inspect", "--input", str(font)])
assert code == 0
assert envelope["data"]["kind"] == "font"
weird = tmp_path / "thing.xyz"
weird.write_text("x")
code, envelope = run_cli(["asset", "inspect", "--input", str(weird)])
assert code != 0
assert "unrecognized asset type" in envelope["error"]["message"]
def test_output_validate_flags_duration_mismatch(pkg: dict[str, Any]) -> None:
code, envelope = run_cli(
[
"output",
"validate",
"--input",
str(pkg["tmp"] / "preview.mp4"),
"--expect-duration",
"60",
]
)
assert code == 0
data = envelope["data"]
assert data["passed"] is False
assert any("duration" in issue for issue in data["issues"])
assert data["validation_scope"] == {
"technical": "performed",
"visual_framing": "not_performed",
"editorial": "not_performed",
}
def test_output_validate_expect_canvas(pkg: dict[str, Any]) -> None:
preview = str(pkg["tmp"] / "preview.mp4")
code, envelope = run_cli(
["output", "validate", "--input", preview, "--expect-canvas", "640x360"]
)
assert code == 0
assert envelope["data"]["passed"] is True
code, envelope = run_cli(
["output", "validate", "--input", preview, "--expect-canvas", "1080x1920"]
)
assert code == 0
assert envelope["data"]["passed"] is False
assert any("canvas" in issue for issue in envelope["data"]["issues"])