visualkit 2.0.1__py3-none-any.whl → 2.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- visualkit/__init__.py +28 -0
- visualkit/coded_visual/capture.py +78 -11
- visualkit/coded_visual/compiler.py +215 -5
- visualkit/engine/pipeline.py +38 -5
- visualkit/exporters/_ffmpeg.py +56 -0
- visualkit/exporters/_render_plan.py +440 -0
- visualkit/exporters/resolve.py +2 -1
- visualkit/exporters/single_clip.py +188 -0
- visualkit/exporters/video.py +92 -98
- visualkit/models/__init__.py +26 -0
- visualkit/models/animation.py +208 -0
- visualkit/models/clips/__init__.py +4 -2
- visualkit/models/clips/base.py +42 -0
- visualkit/models/clips/coded_visual.py +109 -5
- visualkit/models/clips/compound.py +50 -4
- visualkit/models/clips/media.py +42 -1
- visualkit/models/clips/text.py +77 -7
- visualkit/models/clips/visual.py +195 -3
- visualkit/models/effects.py +266 -0
- visualkit/models/keyframes.py +433 -0
- visualkit/models/timeline.py +110 -8
- {visualkit-2.0.1.dist-info → visualkit-2.1.0.dist-info}/METADATA +25 -5
- visualkit-2.1.0.dist-info/RECORD +42 -0
- visualkit-2.0.1.dist-info/RECORD +0 -36
- {visualkit-2.0.1.dist-info → visualkit-2.1.0.dist-info}/WHEEL +0 -0
- {visualkit-2.0.1.dist-info → visualkit-2.1.0.dist-info}/licenses/AUTHORS.rst +0 -0
- {visualkit-2.0.1.dist-info → visualkit-2.1.0.dist-info}/licenses/LICENSE +0 -0
- {visualkit-2.0.1.dist-info → visualkit-2.1.0.dist-info}/top_level.txt +0 -0
visualkit/__init__.py
CHANGED
|
@@ -3,24 +3,37 @@ from importlib.metadata import PackageNotFoundError, version
|
|
|
3
3
|
from visualkit.engine import AssetResolver, DictAssetResolver, TimelinePipeline
|
|
4
4
|
from visualkit.exporters import BaseExporter, DaVinciResolveExporter, FFmpegVideoExporter
|
|
5
5
|
from visualkit.models import (
|
|
6
|
+
KEYFRAMEABLE_PROPERTIES,
|
|
7
|
+
MASK_PROPERTIES,
|
|
8
|
+
AddClipResult,
|
|
9
|
+
AnimationPreset,
|
|
6
10
|
AudioClip,
|
|
7
11
|
AudioContent,
|
|
8
12
|
AudioProperties,
|
|
9
13
|
AudioTrack,
|
|
10
14
|
BaseClip,
|
|
15
|
+
ChromaKey,
|
|
11
16
|
Clip,
|
|
17
|
+
ClipAnimation,
|
|
12
18
|
CodedVisualClip,
|
|
13
19
|
CompileStatus,
|
|
14
20
|
CompoundAudioClip,
|
|
15
21
|
CompoundClip,
|
|
22
|
+
CurveSpec,
|
|
23
|
+
Easing,
|
|
16
24
|
ExposedParameter,
|
|
17
25
|
InsertMode,
|
|
26
|
+
Keyframe,
|
|
27
|
+
LintIssue,
|
|
28
|
+
Mask,
|
|
18
29
|
MediaClip,
|
|
19
30
|
Position,
|
|
31
|
+
PropertyCurve,
|
|
20
32
|
RenderMode,
|
|
21
33
|
Size,
|
|
22
34
|
Source,
|
|
23
35
|
TextClip,
|
|
36
|
+
TextGradient,
|
|
24
37
|
TextStyle,
|
|
25
38
|
Timeline,
|
|
26
39
|
Track,
|
|
@@ -31,6 +44,7 @@ from visualkit.models import (
|
|
|
31
44
|
VideoTrack,
|
|
32
45
|
VisualClip,
|
|
33
46
|
VisualContent,
|
|
47
|
+
color_to_rgb,
|
|
34
48
|
)
|
|
35
49
|
from visualkit.utils.exceptions import (
|
|
36
50
|
BrowserNotFoundError,
|
|
@@ -52,6 +66,7 @@ except PackageNotFoundError: # running from a source checkout that is not insta
|
|
|
52
66
|
|
|
53
67
|
__all__ = [
|
|
54
68
|
"__version__",
|
|
69
|
+
"AddClipResult",
|
|
55
70
|
"AudioClip",
|
|
56
71
|
"AudioContent",
|
|
57
72
|
"AudioProperties",
|
|
@@ -64,6 +79,7 @@ __all__ = [
|
|
|
64
79
|
"CompoundClip",
|
|
65
80
|
"ExposedParameter",
|
|
66
81
|
"InsertMode",
|
|
82
|
+
"LintIssue",
|
|
67
83
|
"MediaClip",
|
|
68
84
|
"Position",
|
|
69
85
|
"RenderMode",
|
|
@@ -97,4 +113,16 @@ __all__ = [
|
|
|
97
113
|
"ExportError",
|
|
98
114
|
"CodedVisualCompileError",
|
|
99
115
|
"BrowserNotFoundError",
|
|
116
|
+
"TextGradient",
|
|
117
|
+
"AnimationPreset",
|
|
118
|
+
"ClipAnimation",
|
|
119
|
+
"ChromaKey",
|
|
120
|
+
"Mask",
|
|
121
|
+
"MASK_PROPERTIES",
|
|
122
|
+
"color_to_rgb",
|
|
123
|
+
"Easing",
|
|
124
|
+
"Keyframe",
|
|
125
|
+
"PropertyCurve",
|
|
126
|
+
"CurveSpec",
|
|
127
|
+
"KEYFRAMEABLE_PROPERTIES",
|
|
100
128
|
]
|
|
@@ -77,6 +77,27 @@ def frame_times(duration: float, fps: float) -> list[float]:
|
|
|
77
77
|
return [i / fps for i in range(count)]
|
|
78
78
|
|
|
79
79
|
|
|
80
|
+
def encoder_args(alpha: bool, width: int = 0, height: int = 0) -> list[str]:
|
|
81
|
+
"""Output codec arguments (everything between the input and the output path)."""
|
|
82
|
+
if alpha:
|
|
83
|
+
# Lossless and keeps partial alpha. 4:2:0 subsamples chroma over 2x2 blocks, so an odd
|
|
84
|
+
# last row/column would share a block with a padding pixel and come back with a wrong
|
|
85
|
+
# colour; odd sizes therefore use 4:4:4 (exact, and no padding to change the size).
|
|
86
|
+
odd = bool(width % 2 or height % 2)
|
|
87
|
+
return ["-c:v", "ffv1", "-level", "3", "-pix_fmt", "yuva444p" if odd else "yuva420p"]
|
|
88
|
+
return [
|
|
89
|
+
"-c:v",
|
|
90
|
+
"libx264",
|
|
91
|
+
"-pix_fmt",
|
|
92
|
+
"yuv420p",
|
|
93
|
+
"-movflags",
|
|
94
|
+
"+faststart",
|
|
95
|
+
# yuv420p needs even dimensions.
|
|
96
|
+
"-vf",
|
|
97
|
+
"pad=ceil(iw/2)*2:ceil(ih/2)*2",
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
|
|
80
101
|
def capture_video(
|
|
81
102
|
*,
|
|
82
103
|
html_path: Path,
|
|
@@ -87,8 +108,16 @@ def capture_video(
|
|
|
87
108
|
fps: float,
|
|
88
109
|
ffmpeg: str = "ffmpeg",
|
|
89
110
|
timeout: float = 300.0,
|
|
111
|
+
alpha: bool = True,
|
|
90
112
|
) -> None:
|
|
91
|
-
"""Render ``html_path`` for ``duration`` seconds at ``fps`` into ``out_file
|
|
113
|
+
"""Render ``html_path`` for ``duration`` seconds at ``fps`` into ``out_file``.
|
|
114
|
+
|
|
115
|
+
With ``alpha=True`` (the default) the video is lossless FFV1 in Matroska
|
|
116
|
+
(``yuva420p``, or ``yuva444p`` for odd sizes), so a transparent page background stays transparent and
|
|
117
|
+
chroma-key/mask edges are not smeared by compression. ``alpha=False`` writes
|
|
118
|
+
the previous flat H.264 MP4 (transparency flattened to black), which is what
|
|
119
|
+
the DaVinci Resolve export keeps using. ``out_file``'s suffix should match
|
|
120
|
+
(``.mkv`` / ``.mp4``).
|
|
92
121
|
|
|
93
122
|
Frames are streamed straight into ffmpeg's stdin as PNGs; nothing is written
|
|
94
123
|
to disk per frame. Raises `CodedVisualCompileError` (with ffmpeg's stderr) on failure.
|
|
@@ -97,7 +126,7 @@ def capture_video(
|
|
|
97
126
|
out_file.parent.mkdir(parents=True, exist_ok=True)
|
|
98
127
|
if out_file.exists():
|
|
99
128
|
out_file.unlink()
|
|
100
|
-
tmp_out = out_file.with_suffix(".part.
|
|
129
|
+
tmp_out = out_file.with_suffix(f".part{out_file.suffix}")
|
|
101
130
|
|
|
102
131
|
times = frame_times(duration, fps)
|
|
103
132
|
cmd = [
|
|
@@ -114,15 +143,7 @@ def capture_video(
|
|
|
114
143
|
"png",
|
|
115
144
|
"-i",
|
|
116
145
|
"-",
|
|
117
|
-
|
|
118
|
-
"libx264",
|
|
119
|
-
"-pix_fmt",
|
|
120
|
-
"yuv420p",
|
|
121
|
-
"-movflags",
|
|
122
|
-
"+faststart",
|
|
123
|
-
# yuv420p needs even dimensions.
|
|
124
|
-
"-vf",
|
|
125
|
-
"pad=ceil(iw/2)*2:ceil(ih/2)*2",
|
|
146
|
+
*encoder_args(alpha, width, height),
|
|
126
147
|
str(tmp_out),
|
|
127
148
|
]
|
|
128
149
|
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
@@ -184,6 +205,52 @@ def _abort(proc: subprocess.Popen, partial: Path) -> None:
|
|
|
184
205
|
partial.unlink()
|
|
185
206
|
|
|
186
207
|
|
|
208
|
+
def capture_frame(
|
|
209
|
+
html_path: Path,
|
|
210
|
+
out_png: Path,
|
|
211
|
+
width: int,
|
|
212
|
+
height: int,
|
|
213
|
+
*,
|
|
214
|
+
time: float = 0.0,
|
|
215
|
+
timeout: float = 60.0,
|
|
216
|
+
) -> None:
|
|
217
|
+
"""Render a single frame of `html_path` at virtual time `time` (seconds) to `out_png`.
|
|
218
|
+
|
|
219
|
+
Unlike `visualkit.coded_visual.browser.screenshot` (which always captures
|
|
220
|
+
at t=0 via the Chrome CLI), this seeks the page's virtual clock and every
|
|
221
|
+
CSS/Web Animation to `time` first -- the same deterministic stepping
|
|
222
|
+
`capture_video` uses per-frame -- so it can preview any instant of an
|
|
223
|
+
animated visual, not just its first frame. Needs the optional
|
|
224
|
+
``playwright`` package; raises `CodedVisualCompileError` if it is missing
|
|
225
|
+
or the capture fails.
|
|
226
|
+
"""
|
|
227
|
+
sync_playwright = _require_playwright()
|
|
228
|
+
out_png.parent.mkdir(parents=True, exist_ok=True)
|
|
229
|
+
# page.clock.run_for only accepts int (whole) ticks -- see capture_video's
|
|
230
|
+
# identical rounding, which avoids passing a bare float here.
|
|
231
|
+
ms = int(round(max(0.0, time) * 1000.0))
|
|
232
|
+
|
|
233
|
+
with sync_playwright() as pw:
|
|
234
|
+
pw_browser = _launch(pw)
|
|
235
|
+
try:
|
|
236
|
+
context, page = _new_page(pw_browser, html_path, width, height)
|
|
237
|
+
try:
|
|
238
|
+
if ms > 0:
|
|
239
|
+
page.clock.run_for(ms)
|
|
240
|
+
page.evaluate(_SEEK_JS, float(ms))
|
|
241
|
+
png = page.screenshot(type="png", omit_background=True, animations="allow")
|
|
242
|
+
finally:
|
|
243
|
+
context.close()
|
|
244
|
+
except Exception as err:
|
|
245
|
+
raise CodedVisualCompileError(
|
|
246
|
+
f"Failed to capture frame of {html_path.name} at t={time}s: {err}"
|
|
247
|
+
) from err
|
|
248
|
+
finally:
|
|
249
|
+
pw_browser.close()
|
|
250
|
+
|
|
251
|
+
out_png.write_bytes(png)
|
|
252
|
+
|
|
253
|
+
|
|
187
254
|
def detect_motion(html_path: Path, width: int, height: int, *, timeout: float = 120.0) -> bool:
|
|
188
255
|
"""True if the page renders differently at two virtual times (i.e. it is animated).
|
|
189
256
|
|
|
@@ -29,18 +29,45 @@ from visualkit.models.clips.base import Size
|
|
|
29
29
|
from visualkit.models.clips.coded_visual import (
|
|
30
30
|
CodedVisualClip,
|
|
31
31
|
CompileStatus,
|
|
32
|
+
LintIssue,
|
|
32
33
|
RenderMode,
|
|
33
34
|
aspect_ratio_for,
|
|
34
35
|
parse_aspect_ratio,
|
|
35
36
|
)
|
|
36
|
-
from visualkit.utils.exceptions import CodedVisualCompileError
|
|
37
|
+
from visualkit.utils.exceptions import CodedVisualCompileError, CodedVisualError
|
|
37
38
|
|
|
38
39
|
_PLACEHOLDER = re.compile(r"\{\{\s*([A-Za-z_][\w.\-]*)\s*\}\}")
|
|
39
40
|
_RAW_PLACEHOLDER = re.compile(r"\{\{\{\s*([A-Za-z_][\w.\-]*)\s*\}\}\}")
|
|
40
41
|
_HEAD_OPEN = re.compile(r"<head(\s[^>]*)?>", re.IGNORECASE)
|
|
41
42
|
_HTML_OPEN = re.compile(r"<html(\s[^>]*)?>", re.IGNORECASE)
|
|
43
|
+
_CANVAS_CLASS = re.compile(r'class\s*=\s*["\'][^"\']*\bvisualkit-canvas\b[^"\']*["\']', re.IGNORECASE)
|
|
42
44
|
_STILL_IMAGE_SUFFIXES = {".png", ".jpg", ".jpeg", ".webp"}
|
|
43
45
|
|
|
46
|
+
# What validate_bundle() treats as "a reference to a local file": the usual
|
|
47
|
+
# HTML attributes that load an asset, and CSS url(...). Regex-based, like
|
|
48
|
+
# every other check in this file -- not a full HTML/CSS parser, so an
|
|
49
|
+
# unusual construct (e.g. a URL built up by JavaScript) can be missed.
|
|
50
|
+
_LOCAL_ASSET_ATTR = re.compile(
|
|
51
|
+
r'\b(?:src|href|poster)\s*=\s*(?P<q>["\'])(?P<url>[^"\']+)(?P=q)', re.IGNORECASE
|
|
52
|
+
)
|
|
53
|
+
_CSS_URL = re.compile(r'url\(\s*(?P<q>["\']?)(?P<url>[^"\')]+)(?P=q)\s*\)', re.IGNORECASE)
|
|
54
|
+
_URL_HAS_SCHEME = re.compile(r"^[a-zA-Z][a-zA-Z0-9+.\-]*:")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _is_local_reference(url: str) -> bool:
|
|
58
|
+
"""True if `url` looks like a relative reference to a file next to the
|
|
59
|
+
bundle's entrypoint, rather than a remote URL, a data URI, an anchor,
|
|
60
|
+
or an unresolved `{{ variable }}`."""
|
|
61
|
+
url = url.strip()
|
|
62
|
+
if not url or url.startswith("#"):
|
|
63
|
+
return False
|
|
64
|
+
if _PLACEHOLDER.search(url) or _RAW_PLACEHOLDER.search(url):
|
|
65
|
+
return False # substituted at render time; nothing to check here yet
|
|
66
|
+
if url.startswith("//") or _URL_HAS_SCHEME.match(url):
|
|
67
|
+
return False # protocol-relative or scheme:// (http, https, data, mailto, ...)
|
|
68
|
+
return True
|
|
69
|
+
|
|
70
|
+
|
|
44
71
|
# Files inside a bundle that affect rendering; hashed into the cache key.
|
|
45
72
|
_BUNDLE_HASH_LIMIT_BYTES = 64 * 1024 * 1024
|
|
46
73
|
|
|
@@ -68,6 +95,12 @@ def _stringify(value: Any) -> str:
|
|
|
68
95
|
return str(value)
|
|
69
96
|
|
|
70
97
|
|
|
98
|
+
#: Bumped whenever the encoded *format* of a rendered video changes (codec, pixel format,
|
|
99
|
+
#: container). The cache key otherwise hashes only the inputs, so without this a stale render in
|
|
100
|
+
#: an old format could be served after an upgrade.
|
|
101
|
+
VIDEO_FORMAT_VERSION = "2-ffv1-yuva420p"
|
|
102
|
+
|
|
103
|
+
|
|
71
104
|
class CodedVisualCompiler:
|
|
72
105
|
"""Renders `CodedVisualClip`s to PNG (still) or video (animated) media."""
|
|
73
106
|
|
|
@@ -105,7 +138,7 @@ class CodedVisualCompiler:
|
|
|
105
138
|
hasher.update(
|
|
106
139
|
f"{canvas_size.width:g}x{canvas_size.height:g}|{aspect_ratio}|{fps:g}|{duration:g}".encode()
|
|
107
140
|
)
|
|
108
|
-
hasher.update(f"|{render_mode}|{bundle_digest}".encode())
|
|
141
|
+
hasher.update(f"|{render_mode}|{bundle_digest}|{VIDEO_FORMAT_VERSION}".encode())
|
|
109
142
|
return hasher.hexdigest()
|
|
110
143
|
|
|
111
144
|
@staticmethod
|
|
@@ -324,6 +357,126 @@ class CodedVisualCompiler:
|
|
|
324
357
|
)
|
|
325
358
|
return target_html, cache_key
|
|
326
359
|
|
|
360
|
+
# ------------------------------------------------------------------ lint
|
|
361
|
+
def validate_bundle(self, clip: CodedVisualClip) -> list[LintIssue]:
|
|
362
|
+
"""Check that every local asset the bundle's HTML/CSS references
|
|
363
|
+
(an `<img src="logo.svg">`, a CSS `url(chart.js)`, ...) actually
|
|
364
|
+
exists next to the entrypoint. Pure Python, no Chrome.
|
|
365
|
+
|
|
366
|
+
A no-op (always `[]`) for a single-file HTML source -- there's
|
|
367
|
+
nothing beside it to reference. For a directory bundle, a missing
|
|
368
|
+
asset otherwise only surfaces as a broken `<img>`/404 in the
|
|
369
|
+
render, not an error.
|
|
370
|
+
|
|
371
|
+
Only scans the entrypoint HTML itself, not files it links to --
|
|
372
|
+
`url(...)` inside a separately-linked `style.css` isn't followed,
|
|
373
|
+
so a bad reference three files deep can still slip through.
|
|
374
|
+
"""
|
|
375
|
+
issues: list[LintIssue] = []
|
|
376
|
+
try:
|
|
377
|
+
info = self._resolve_inputs(clip)
|
|
378
|
+
except CodedVisualError as err:
|
|
379
|
+
issues.append(LintIssue(severity="error", code="resolve_failed", message=str(err)))
|
|
380
|
+
return issues
|
|
381
|
+
|
|
382
|
+
if not Path(clip.source.source).expanduser().resolve().is_dir():
|
|
383
|
+
return issues
|
|
384
|
+
|
|
385
|
+
bundle_dir = info["entrypoint"].parent
|
|
386
|
+
raw_html = info["raw_html"]
|
|
387
|
+
checked: set[str] = set()
|
|
388
|
+
for pattern in (_LOCAL_ASSET_ATTR, _CSS_URL):
|
|
389
|
+
for match in pattern.finditer(raw_html):
|
|
390
|
+
url = match.group("url")
|
|
391
|
+
if not _is_local_reference(url) or url in checked:
|
|
392
|
+
continue
|
|
393
|
+
checked.add(url)
|
|
394
|
+
relative_path = url.split("?", 1)[0].split("#", 1)[0]
|
|
395
|
+
asset_path = (bundle_dir / relative_path).resolve()
|
|
396
|
+
if not asset_path.exists():
|
|
397
|
+
issues.append(
|
|
398
|
+
LintIssue(
|
|
399
|
+
severity="error",
|
|
400
|
+
code="missing_asset",
|
|
401
|
+
message=(
|
|
402
|
+
f"Referenced local asset '{url}' does not exist (expected at "
|
|
403
|
+
f"{asset_path}, alongside {info['entrypoint'].name})."
|
|
404
|
+
),
|
|
405
|
+
)
|
|
406
|
+
)
|
|
407
|
+
return issues
|
|
408
|
+
|
|
409
|
+
def lint(self, clip: CodedVisualClip) -> list[LintIssue]:
|
|
410
|
+
"""Pure-Python pre-flight checks for `clip`, independent of Chrome.
|
|
411
|
+
|
|
412
|
+
Catches the majority of agent-authoring mistakes before spending a
|
|
413
|
+
browser launch on them: a missing `.visualkit-canvas` wrapper, a
|
|
414
|
+
`{{ variable }}`/`{{{ variable }}}` that doesn't match any declared
|
|
415
|
+
variable, a referenced local asset that doesn't exist (see
|
|
416
|
+
`validate_bundle`), or a source/manifest that fails to load at
|
|
417
|
+
all. Returns a list of `LintIssue`s (empty means nothing was
|
|
418
|
+
found); never raises for anything a real compile could hit --
|
|
419
|
+
those become an "error"-severity issue instead.
|
|
420
|
+
"""
|
|
421
|
+
issues: list[LintIssue] = []
|
|
422
|
+
try:
|
|
423
|
+
info = self._resolve_inputs(clip)
|
|
424
|
+
except CodedVisualError as err:
|
|
425
|
+
issues.append(LintIssue(severity="error", code="resolve_failed", message=str(err)))
|
|
426
|
+
return issues
|
|
427
|
+
|
|
428
|
+
raw_html = info["raw_html"]
|
|
429
|
+
variables = info["variables"]
|
|
430
|
+
|
|
431
|
+
if not _CANVAS_CLASS.search(raw_html):
|
|
432
|
+
issues.append(
|
|
433
|
+
LintIssue(
|
|
434
|
+
severity="error",
|
|
435
|
+
code="missing_canvas",
|
|
436
|
+
message=(
|
|
437
|
+
'No element with class="visualkit-canvas" found. Without it, '
|
|
438
|
+
"auto-scaling/fit has nothing to act on and the render is likely "
|
|
439
|
+
"blank or wrongly sized. Wrap the visual's root element, e.g. "
|
|
440
|
+
'<div class="visualkit-canvas">...</div>.'
|
|
441
|
+
),
|
|
442
|
+
)
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
referenced: set[str] = set()
|
|
446
|
+
for pattern, braces in ((_RAW_PLACEHOLDER, "{{{ }}}"), (_PLACEHOLDER, "{{ }}")):
|
|
447
|
+
for match in pattern.finditer(raw_html):
|
|
448
|
+
name = match.group(1)
|
|
449
|
+
referenced.add(name)
|
|
450
|
+
if name not in variables:
|
|
451
|
+
issues.append(
|
|
452
|
+
LintIssue(
|
|
453
|
+
severity="error",
|
|
454
|
+
code="unresolved_variable",
|
|
455
|
+
message=(
|
|
456
|
+
f"{braces} placeholder references '{name}', which is not a "
|
|
457
|
+
"declared clip or manifest variable -- it will render "
|
|
458
|
+
f"literally as '{match.group(0)}' instead of being substituted."
|
|
459
|
+
),
|
|
460
|
+
)
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
unused = sorted(set(variables) - referenced)
|
|
464
|
+
if unused:
|
|
465
|
+
issues.append(
|
|
466
|
+
LintIssue(
|
|
467
|
+
severity="warning",
|
|
468
|
+
code="unused_variable",
|
|
469
|
+
message=(
|
|
470
|
+
f"Declared variable(s) {', '.join(unused)} aren't referenced by any "
|
|
471
|
+
"{{ }}/{{{ }}} placeholder in the HTML. Fine if they're read from "
|
|
472
|
+
"window.__VARIABLES__ in JavaScript instead."
|
|
473
|
+
),
|
|
474
|
+
)
|
|
475
|
+
)
|
|
476
|
+
|
|
477
|
+
issues.extend(self.validate_bundle(clip))
|
|
478
|
+
return issues
|
|
479
|
+
|
|
327
480
|
# ------------------------------------------------------------------ rendering
|
|
328
481
|
@staticmethod
|
|
329
482
|
def _find_chrome_executable() -> str | None:
|
|
@@ -342,20 +495,51 @@ class CodedVisualCompiler:
|
|
|
342
495
|
clip.compile_status = CompileStatus.READY
|
|
343
496
|
return out
|
|
344
497
|
|
|
498
|
+
def preview_image(self, clip: CodedVisualClip, *, force: bool = False) -> Path:
|
|
499
|
+
"""Render `clip` as a single still frame and return its PNG path,
|
|
500
|
+
without recording it as the clip's compiled media -- unlike
|
|
501
|
+
`render_to_image`, `clip.media_source`/`compile_status` are left
|
|
502
|
+
exactly as they were before the call.
|
|
503
|
+
|
|
504
|
+
This is the same lightweight Chrome-CLI screenshot `render_to_image`
|
|
505
|
+
uses (no `playwright` dependency), so it's the cheapest possible
|
|
506
|
+
correctness check for a coded visual's own HTML/CSS/variables --
|
|
507
|
+
e.g. a missing `.visualkit-canvas` wrapper or a malformed
|
|
508
|
+
`{{ variable }}` -- before spending a real `compile()` or timeline
|
|
509
|
+
round trip on it.
|
|
510
|
+
|
|
511
|
+
For an *animated* visual this is not a guaranteed frame-0 capture:
|
|
512
|
+
Chrome is given a `--virtual-time-budget` to run in before the
|
|
513
|
+
screenshot (see `browser.screenshot`), so an in-progress animation
|
|
514
|
+
may show partway through, same as `render_to_image` itself. Use
|
|
515
|
+
`preview_frame(0.0)` instead if you need a deterministic first
|
|
516
|
+
frame of an animated visual.
|
|
517
|
+
"""
|
|
518
|
+
prev_status, prev_media = clip.compile_status, clip.media_source
|
|
519
|
+
try:
|
|
520
|
+
return self.render_to_image(clip, force=force)
|
|
521
|
+
finally:
|
|
522
|
+
clip.compile_status, clip.media_source = prev_status, prev_media
|
|
523
|
+
|
|
345
524
|
def render_to_video(
|
|
346
525
|
self,
|
|
347
526
|
clip: CodedVisualClip,
|
|
348
527
|
output_path: str | Path | None = None,
|
|
349
528
|
fps: float | None = None,
|
|
350
529
|
force: bool = False,
|
|
530
|
+
alpha: bool = True,
|
|
351
531
|
) -> Path:
|
|
352
|
-
"""Render `clip`'s animation to a video file (see `visualkit.coded_visual.capture`).
|
|
532
|
+
"""Render `clip`'s animation to a video file (see `visualkit.coded_visual.capture`).
|
|
533
|
+
|
|
534
|
+
``alpha=True`` (default) writes ``render.mkv`` (lossless FFV1 with transparency);
|
|
535
|
+
``alpha=False`` writes the flat H.264 ``render.mp4``.
|
|
536
|
+
"""
|
|
353
537
|
from visualkit.coded_visual.capture import capture_video
|
|
354
538
|
|
|
355
539
|
info = self._resolve_inputs(clip)
|
|
356
540
|
target_html, cache_key = self._write_bundle(clip, info)
|
|
357
541
|
bundle_dir = target_html.parent
|
|
358
|
-
cached = bundle_dir / "render.mp4"
|
|
542
|
+
cached = bundle_dir / ("render.mkv" if alpha else "render.mp4")
|
|
359
543
|
|
|
360
544
|
if force or not cached.exists():
|
|
361
545
|
if shutil.which(self.ffmpeg) is None:
|
|
@@ -369,6 +553,7 @@ class CodedVisualCompiler:
|
|
|
369
553
|
fps=fps or info["fps"],
|
|
370
554
|
ffmpeg=self.ffmpeg,
|
|
371
555
|
timeout=self.render_timeout,
|
|
556
|
+
alpha=alpha,
|
|
372
557
|
)
|
|
373
558
|
|
|
374
559
|
out = cached
|
|
@@ -380,6 +565,30 @@ class CodedVisualCompiler:
|
|
|
380
565
|
clip.compile_status = CompileStatus.READY
|
|
381
566
|
return out
|
|
382
567
|
|
|
568
|
+
def preview_frame(self, clip: CodedVisualClip, time: float = 0.0, *, force: bool = False) -> Path:
|
|
569
|
+
"""Render a single frame of `clip` at virtual time `time` seconds, without
|
|
570
|
+
encoding a video and without affecting `clip.media_source`/`compile_status`.
|
|
571
|
+
|
|
572
|
+
Cheap way to check an individual instant of an animated coded visual
|
|
573
|
+
(e.g. mid-way through its motion) before committing to a full
|
|
574
|
+
`render_to_video`/`compile()` call. Needs the optional `playwright`
|
|
575
|
+
package, same as `render_to_video`; still visuals can also just use
|
|
576
|
+
`render_to_image` (no browser-clock stepping needed for a static page).
|
|
577
|
+
Cached per `(cache_key, time)` under the bundle dir, so repeated
|
|
578
|
+
previews of the same frame are free.
|
|
579
|
+
"""
|
|
580
|
+
from visualkit.coded_visual.capture import capture_frame
|
|
581
|
+
|
|
582
|
+
info = self._resolve_inputs(clip)
|
|
583
|
+
target_html, cache_key = self._write_bundle(clip, info)
|
|
584
|
+
# Slug the timestamp into the filename (3dp is sub-frame precision at any
|
|
585
|
+
# realistic fps) so distinct preview times don't collide or overwrite.
|
|
586
|
+
out = target_html.parent / f"preview_{max(0.0, time):.3f}.png"
|
|
587
|
+
if force or not out.exists():
|
|
588
|
+
width, height = int(info["canvas"].width), int(info["canvas"].height)
|
|
589
|
+
capture_frame(target_html, out, width, height, time=time, timeout=self.render_timeout)
|
|
590
|
+
return out
|
|
591
|
+
|
|
383
592
|
def is_animated(self, clip: CodedVisualClip) -> bool:
|
|
384
593
|
"""Decide whether `clip` needs video (True) or a still image (False).
|
|
385
594
|
|
|
@@ -410,6 +619,7 @@ class CodedVisualCompiler:
|
|
|
410
619
|
clip: CodedVisualClip,
|
|
411
620
|
force: bool = False,
|
|
412
621
|
render_video: bool | None = None,
|
|
622
|
+
alpha: bool = True,
|
|
413
623
|
) -> str:
|
|
414
624
|
"""Render `clip` to media and return the media path.
|
|
415
625
|
|
|
@@ -425,7 +635,7 @@ class CodedVisualCompiler:
|
|
|
425
635
|
else:
|
|
426
636
|
animated = render_video
|
|
427
637
|
path = (
|
|
428
|
-
self.render_to_video(clip, force=force)
|
|
638
|
+
self.render_to_video(clip, force=force, alpha=alpha)
|
|
429
639
|
if animated
|
|
430
640
|
else self.render_to_image(clip, force=force)
|
|
431
641
|
)
|
visualkit/engine/pipeline.py
CHANGED
|
@@ -19,7 +19,7 @@ from visualkit.models.clips.coded_visual import CodedVisualClip, CompileStatus
|
|
|
19
19
|
from visualkit.models.clips.compound import CompoundAudioClip, CompoundClip
|
|
20
20
|
from visualkit.models.clips.media import MediaClip
|
|
21
21
|
from visualkit.models.clips.text import TextClip
|
|
22
|
-
from visualkit.models.clips.visual import Transform
|
|
22
|
+
from visualkit.models.clips.visual import Transform, VisualClip
|
|
23
23
|
from visualkit.models.timeline import Timeline
|
|
24
24
|
from visualkit.utils.exceptions import TimelineValidationError
|
|
25
25
|
from visualkit.utils.time import Time
|
|
@@ -111,19 +111,24 @@ class TimelinePipeline:
|
|
|
111
111
|
timeline: Timeline,
|
|
112
112
|
force: bool = False,
|
|
113
113
|
render_video: bool | None = None,
|
|
114
|
+
alpha: bool = True,
|
|
114
115
|
) -> Timeline:
|
|
115
116
|
"""Compile every CodedVisualClip in `timeline`, including inside compounds (mutates `timeline`).
|
|
116
117
|
|
|
117
118
|
`render_video`: ``None`` lets each clip's ``render_mode`` decide (a still
|
|
118
119
|
visual becomes a PNG, an animated one a video); ``True``/``False`` force it.
|
|
120
|
+
`alpha`: animated visuals keep transparency (lossless FFV1 ``.mkv``); ``False`` writes
|
|
121
|
+
the flat H.264 ``.mp4`` instead.
|
|
119
122
|
"""
|
|
120
123
|
for track in timeline.video_tracks:
|
|
121
124
|
for clip in track.clips:
|
|
122
125
|
if isinstance(clip, CodedVisualClip):
|
|
123
126
|
if force or clip.compile_status != CompileStatus.READY or not clip.media_source:
|
|
124
|
-
self.compiler.compile(clip, force=force, render_video=render_video)
|
|
127
|
+
self.compiler.compile(clip, force=force, render_video=render_video, alpha=alpha)
|
|
125
128
|
elif isinstance(clip, CompoundClip) and clip.inner_timeline:
|
|
126
|
-
self.compile_coded_visuals(
|
|
129
|
+
self.compile_coded_visuals(
|
|
130
|
+
clip.inner_timeline, force=force, render_video=render_video, alpha=alpha
|
|
131
|
+
)
|
|
127
132
|
return timeline
|
|
128
133
|
|
|
129
134
|
def process(
|
|
@@ -131,12 +136,13 @@ class TimelinePipeline:
|
|
|
131
136
|
timeline: Timeline,
|
|
132
137
|
force_compile: bool = False,
|
|
133
138
|
render_video: bool | None = None,
|
|
139
|
+
alpha: bool = True,
|
|
134
140
|
) -> Timeline:
|
|
135
141
|
"""Resolve variables -> compile coded visuals -> flatten, **without mutating `timeline`**."""
|
|
136
142
|
self.validate(timeline)
|
|
137
143
|
working = timeline.model_copy(deep=True)
|
|
138
144
|
self.resolve_variables(working)
|
|
139
|
-
self.compile_coded_visuals(working, force=force_compile, render_video=render_video)
|
|
145
|
+
self.compile_coded_visuals(working, force=force_compile, render_video=render_video, alpha=alpha)
|
|
140
146
|
return self.flatten(working)
|
|
141
147
|
|
|
142
148
|
# ------------------------------------------------------------------ flatten
|
|
@@ -290,6 +296,19 @@ class TimelinePipeline:
|
|
|
290
296
|
if not self._place_child(expanded, compound_offset, effective_speed, clip_end):
|
|
291
297
|
continue
|
|
292
298
|
if not comp_transform.is_identity and hasattr(expanded, "transform"):
|
|
299
|
+
if isinstance(expanded, VisualClip) and (expanded.keyframes or expanded.animation):
|
|
300
|
+
# Composing an animated child with a parent transform would mean composing
|
|
301
|
+
# two curves per property; keyed values also ignore the static transform the
|
|
302
|
+
# parent's offset/scale/rotation would be folded into. Refuse rather than
|
|
303
|
+
# render the animation silently wrong. `animation` (fade/slide/pop/wipe)
|
|
304
|
+
# compiles to the same kind of curve, so it shares the same refusal.
|
|
305
|
+
raise NotImplementedError(
|
|
306
|
+
f"Clip '{expanded.id}' has keyframes and sits inside CompoundClip "
|
|
307
|
+
f"'{compound.id}', whose transform is not the identity. Animated clips "
|
|
308
|
+
"inside a transformed compound are not supported yet: leave the compound's "
|
|
309
|
+
"transform at its defaults (animate the inner clips instead), or move the "
|
|
310
|
+
"animated clip out of the compound."
|
|
311
|
+
)
|
|
293
312
|
expanded.transform = compose_transforms(comp_transform, expanded.transform)
|
|
294
313
|
target_timeline.video_tracks[dest_v_idx].add_clip(expanded)
|
|
295
314
|
|
|
@@ -325,7 +344,21 @@ class TimelinePipeline:
|
|
|
325
344
|
if end > clip_end:
|
|
326
345
|
duration = Time(clip_end.value - start.value)
|
|
327
346
|
clip.timeline_start = start
|
|
328
|
-
clip
|
|
347
|
+
if isinstance(clip, VisualClip):
|
|
348
|
+
# Keyframe times live on the inner timeline, which the compound plays at
|
|
349
|
+
# `effective_speed` -- so they scale with the clip's duration (the clip's own `speed`
|
|
350
|
+
# is not involved), and anything now past the trimmed end is cut.
|
|
351
|
+
retimed = (
|
|
352
|
+
{
|
|
353
|
+
name: curve.time_scaled(1 / speed).head(duration.value)
|
|
354
|
+
for name, curve in clip.keyframes.items()
|
|
355
|
+
}
|
|
356
|
+
if clip.keyframes
|
|
357
|
+
else None
|
|
358
|
+
)
|
|
359
|
+
clip.set_duration_and_keyframes(duration, retimed)
|
|
360
|
+
else:
|
|
361
|
+
clip.duration = duration
|
|
329
362
|
clip.speed = float(_frac(clip.speed) * speed)
|
|
330
363
|
return True
|
|
331
364
|
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Small ffmpeg plumbing shared by the video exporter and the single-clip preview."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
import subprocess
|
|
8
|
+
import tempfile
|
|
9
|
+
from contextlib import contextmanager
|
|
10
|
+
from functools import lru_cache
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Iterator
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def option_for_ffmpeg_version(version_line: str) -> str:
|
|
16
|
+
"""The flag that reads a filtergraph from a file, for the ffmpeg build named by `version_line`.
|
|
17
|
+
|
|
18
|
+
``-filter_complex_script`` exists through ffmpeg 6 and is deprecated from 7 in favour of
|
|
19
|
+
``-/filter_complex``. Unparseable version strings (distro suffixes, git builds) are treated
|
|
20
|
+
as modern except for a plain ``version 4/5/6`` prefix.
|
|
21
|
+
"""
|
|
22
|
+
match = re.search(r"version\s+n?(\d+)", version_line)
|
|
23
|
+
if match:
|
|
24
|
+
return "-/filter_complex" if int(match.group(1)) >= 7 else "-filter_complex_script"
|
|
25
|
+
if re.search(r"version\s+N-\d+", version_line): # git master build
|
|
26
|
+
return "-/filter_complex"
|
|
27
|
+
return "-filter_complex_script"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@lru_cache(maxsize=8)
|
|
31
|
+
def filter_script_option(ffmpeg: str = "ffmpeg") -> str:
|
|
32
|
+
"""Probe `ffmpeg -version` once and pick the right filtergraph-file flag."""
|
|
33
|
+
try:
|
|
34
|
+
out = subprocess.run([ffmpeg, "-version"], capture_output=True, check=False, timeout=15).stdout
|
|
35
|
+
except (OSError, subprocess.SubprocessError):
|
|
36
|
+
return "-filter_complex_script"
|
|
37
|
+
first = out.decode("utf-8", errors="replace").splitlines()[:1]
|
|
38
|
+
return option_for_ffmpeg_version(first[0] if first else "")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@contextmanager
|
|
42
|
+
def filtergraph_script(graph: str, *, directory: Path, stem: str) -> Iterator[Path]:
|
|
43
|
+
"""Write `graph` to a temp script file in `directory`, and always delete it afterwards.
|
|
44
|
+
|
|
45
|
+
Passing the graph on the command line overflows the OS argument limit at a few thousand
|
|
46
|
+
keyframes ("Argument list too long"); a script file does not. The file lives next to the
|
|
47
|
+
output so it is on the same filesystem and easy to spot if a crash ever leaves one behind.
|
|
48
|
+
"""
|
|
49
|
+
fd, name = tempfile.mkstemp(dir=directory, prefix=f".{stem}.", suffix=".filtergraph.txt")
|
|
50
|
+
path = Path(name)
|
|
51
|
+
try:
|
|
52
|
+
with os.fdopen(fd, "w", encoding="utf-8") as fh:
|
|
53
|
+
fh.write(graph)
|
|
54
|
+
yield path
|
|
55
|
+
finally:
|
|
56
|
+
path.unlink(missing_ok=True)
|