@openartifactsai/motion-stage 0.0.0-stage → 0.23.0-rc.0
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.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +70 -4
- package/src/build.d.ts +18 -0
- package/src/build.js +73 -0
- package/src/host.d.ts +37 -0
- package/src/host.js +76 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +6 -0
- package/src/layers.d.ts +30 -0
- package/src/layers.js +56 -0
- package/src/offline/audio.d.ts +19 -0
- package/src/offline/audio.js +20 -0
- package/src/offline/data.d.ts +26 -0
- package/src/offline/data.js +71 -0
- package/src/offline/main.d.ts +9 -0
- package/src/offline/main.js +52 -0
- package/src/offline/player.d.ts +23 -0
- package/src/offline/player.js +157 -0
- package/src/protocol/clip-guards.d.ts +5 -0
- package/src/protocol/clip-guards.js +22 -0
- package/src/protocol/guards.d.ts +6 -0
- package/src/protocol/guards.js +257 -0
- package/src/protocol/layer-guards.d.ts +7 -0
- package/src/protocol/layer-guards.js +100 -0
- package/src/protocol/layers.d.ts +135 -0
- package/src/protocol/layers.js +1 -0
- package/src/protocol/types.d.ts +340 -0
- package/src/protocol/types.js +7 -0
- package/src/protocol.d.ts +2 -0
- package/src/protocol.js +2 -0
- package/src/raster.d.ts +4 -0
- package/src/raster.js +2 -0
- package/src/runtime/assets.d.ts +19 -0
- package/src/runtime/assets.js +63 -0
- package/src/runtime/caption-box.d.ts +3 -0
- package/src/runtime/caption-box.js +16 -0
- package/src/runtime/captions.d.ts +31 -0
- package/src/runtime/captions.js +115 -0
- package/src/runtime/clip-captions.d.ts +9 -0
- package/src/runtime/clip-captions.js +27 -0
- package/src/runtime/clips.d.ts +83 -0
- package/src/runtime/clips.js +259 -0
- package/src/runtime/console.d.ts +13 -0
- package/src/runtime/console.js +99 -0
- package/src/runtime/cut.d.ts +7 -0
- package/src/runtime/cut.js +63 -0
- package/src/runtime/determinism.d.ts +12 -0
- package/src/runtime/determinism.js +47 -0
- package/src/runtime/film.d.ts +43 -0
- package/src/runtime/film.js +31 -0
- package/src/runtime/focus.d.ts +8 -0
- package/src/runtime/focus.js +71 -0
- package/src/runtime/fonts.d.ts +22 -0
- package/src/runtime/fonts.js +109 -0
- package/src/runtime/frame.d.ts +9 -0
- package/src/runtime/frame.js +140 -0
- package/src/runtime/fx/backdrop.d.ts +60 -0
- package/src/runtime/fx/backdrop.js +264 -0
- package/src/runtime/fx/color.d.ts +19 -0
- package/src/runtime/fx/color.js +84 -0
- package/src/runtime/fx/css-color.d.ts +6 -0
- package/src/runtime/fx/css-color.js +44 -0
- package/src/runtime/fx/curve.d.ts +12 -0
- package/src/runtime/fx/curve.js +34 -0
- package/src/runtime/fx/gl.d.ts +29 -0
- package/src/runtime/fx/gl.js +207 -0
- package/src/runtime/fx/glsl.d.ts +35 -0
- package/src/runtime/fx/glsl.js +113 -0
- package/src/runtime/fx/index.d.ts +23 -0
- package/src/runtime/fx/index.js +151 -0
- package/src/runtime/fx/key.d.ts +11 -0
- package/src/runtime/fx/key.js +21 -0
- package/src/runtime/fx/numbers.d.ts +14 -0
- package/src/runtime/fx/numbers.js +57 -0
- package/src/runtime/fx/pace.d.ts +17 -0
- package/src/runtime/fx/pace.js +26 -0
- package/src/runtime/fx/paint.d.ts +28 -0
- package/src/runtime/fx/paint.js +150 -0
- package/src/runtime/guard.d.ts +4 -0
- package/src/runtime/guard.js +141 -0
- package/src/runtime/hand-titles/index.d.ts +9 -0
- package/src/runtime/hand-titles/index.js +73 -0
- package/src/runtime/hand-titles/layer.d.ts +20 -0
- package/src/runtime/hand-titles/layer.js +118 -0
- package/src/runtime/hand-titles/place.d.ts +27 -0
- package/src/runtime/hand-titles/place.js +45 -0
- package/src/runtime/held-store.d.ts +35 -0
- package/src/runtime/held-store.js +63 -0
- package/src/runtime/held.d.ts +19 -0
- package/src/runtime/held.js +225 -0
- package/src/runtime/kit/camera.d.ts +59 -0
- package/src/runtime/kit/camera.js +81 -0
- package/src/runtime/kit/chip.d.ts +25 -0
- package/src/runtime/kit/chip.js +75 -0
- package/src/runtime/kit/count.d.ts +37 -0
- package/src/runtime/kit/count.js +100 -0
- package/src/runtime/kit/cursor.d.ts +46 -0
- package/src/runtime/kit/cursor.js +108 -0
- package/src/runtime/kit/dom.d.ts +14 -0
- package/src/runtime/kit/dom.js +46 -0
- package/src/runtime/kit/geometry.d.ts +24 -0
- package/src/runtime/kit/geometry.js +56 -0
- package/src/runtime/kit/index.d.ts +113 -0
- package/src/runtime/kit/index.js +157 -0
- package/src/runtime/kit/mark-strokes.d.ts +65 -0
- package/src/runtime/kit/mark-strokes.js +226 -0
- package/src/runtime/kit/marks.d.ts +21 -0
- package/src/runtime/kit/marks.js +165 -0
- package/src/runtime/kit/math.d.ts +23 -0
- package/src/runtime/kit/math.js +35 -0
- package/src/runtime/kit/paper.d.ts +7 -0
- package/src/runtime/kit/paper.js +27 -0
- package/src/runtime/kit/random.d.ts +8 -0
- package/src/runtime/kit/random.js +32 -0
- package/src/runtime/kit/text-fx.d.ts +18 -0
- package/src/runtime/kit/text-fx.js +55 -0
- package/src/runtime/kit/typing.d.ts +21 -0
- package/src/runtime/kit/typing.js +42 -0
- package/src/runtime/laid/layer.d.ts +44 -0
- package/src/runtime/laid/layer.js +370 -0
- package/src/runtime/layers/describe.d.ts +41 -0
- package/src/runtime/layers/describe.js +327 -0
- package/src/runtime/layers/item.d.ts +123 -0
- package/src/runtime/layers/item.js +150 -0
- package/src/runtime/layers/parts.d.ts +53 -0
- package/src/runtime/layers/parts.js +158 -0
- package/src/runtime/layers/rows.d.ts +4 -0
- package/src/runtime/layers/rows.js +233 -0
- package/src/runtime/layers/scan.d.ts +24 -0
- package/src/runtime/layers/scan.js +204 -0
- package/src/runtime/layers/walk.d.ts +16 -0
- package/src/runtime/layers/walk.js +337 -0
- package/src/runtime/load.d.ts +10 -0
- package/src/runtime/load.js +267 -0
- package/src/runtime/main.d.ts +1 -0
- package/src/runtime/main.js +90 -0
- package/src/runtime/modules.d.ts +23 -0
- package/src/runtime/modules.js +53 -0
- package/src/runtime/port.d.ts +8 -0
- package/src/runtime/port.js +46 -0
- package/src/runtime/questions.d.ts +24 -0
- package/src/runtime/questions.js +162 -0
- package/src/runtime/recut.d.ts +81 -0
- package/src/runtime/recut.js +325 -0
- package/src/runtime/render.d.ts +80 -0
- package/src/runtime/render.js +358 -0
- package/src/runtime/retouch/ask.d.ts +17 -0
- package/src/runtime/retouch/ask.js +171 -0
- package/src/runtime/retouch/entry.d.ts +77 -0
- package/src/runtime/retouch/entry.js +1 -0
- package/src/runtime/retouch/judge.d.ts +25 -0
- package/src/runtime/retouch/judge.js +209 -0
- package/src/runtime/retouch/look.d.ts +39 -0
- package/src/runtime/retouch/look.js +135 -0
- package/src/runtime/retouch/own-styles.d.ts +19 -0
- package/src/runtime/retouch/own-styles.js +141 -0
- package/src/runtime/retouch/parts.d.ts +22 -0
- package/src/runtime/retouch/parts.js +166 -0
- package/src/runtime/retouch/retouch.d.ts +37 -0
- package/src/runtime/retouch/retouch.js +238 -0
- package/src/runtime/retouch/seat.d.ts +40 -0
- package/src/runtime/retouch/seat.js +228 -0
- package/src/runtime/retouch/state.d.ts +37 -0
- package/src/runtime/retouch/state.js +153 -0
- package/src/runtime/retouch/type-style.d.ts +19 -0
- package/src/runtime/retouch/type-style.js +111 -0
- package/src/runtime/scan.d.ts +2 -0
- package/src/runtime/scan.js +49 -0
- package/src/runtime/scenes/card.d.ts +12 -0
- package/src/runtime/scenes/card.js +56 -0
- package/src/runtime/scenes/install.d.ts +69 -0
- package/src/runtime/scenes/install.js +528 -0
- package/src/runtime/scenes/read.d.ts +27 -0
- package/src/runtime/scenes/read.js +73 -0
- package/src/runtime/scenes/scoped.d.ts +13 -0
- package/src/runtime/scenes/scoped.js +46 -0
- package/src/runtime/score.d.ts +32 -0
- package/src/runtime/score.js +262 -0
- package/src/runtime/sketch/pen.d.ts +3 -0
- package/src/runtime/sketch/pen.js +371 -0
- package/src/runtime/sketch/strokes.d.ts +41 -0
- package/src/runtime/sketch/strokes.js +187 -0
- package/src/runtime/snapshot/draw.d.ts +14 -0
- package/src/runtime/snapshot/draw.js +125 -0
- package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
- package/src/runtime/snapshot/hold-first-draw.js +128 -0
- package/src/runtime/snapshot/lift.d.ts +41 -0
- package/src/runtime/snapshot/lift.js +161 -0
- package/src/runtime/snapshot/plate.d.ts +20 -0
- package/src/runtime/snapshot/plate.js +79 -0
- package/src/runtime/snapshot/serialize.d.ts +51 -0
- package/src/runtime/snapshot/serialize.js +286 -0
- package/src/runtime/snapshot/styles.d.ts +8 -0
- package/src/runtime/snapshot/styles.js +109 -0
- package/src/runtime/snapshot/xml.d.ts +6 -0
- package/src/runtime/snapshot/xml.js +110 -0
- package/src/runtime/stage-size.d.ts +7 -0
- package/src/runtime/stage-size.js +3 -0
- package/src/runtime/stage.d.ts +27 -0
- package/src/runtime/stage.js +154 -0
- package/src/runtime/stretch.d.ts +13 -0
- package/src/runtime/stretch.js +16 -0
- package/src/runtime/texts.d.ts +24 -0
- package/src/runtime/texts.js +211 -0
- package/src/runtime/video-sync.d.ts +51 -0
- package/src/runtime/video-sync.js +218 -0
- package/src/stills.d.ts +90 -0
- package/src/stills.js +329 -0
- package/src/tools/film.d.ts +13 -0
- package/src/tools/film.js +116 -0
- package/src/tools/levels.d.ts +8 -0
- package/src/tools/levels.js +28 -0
- package/src/tools/main.d.ts +1 -0
- package/src/tools/main.js +223 -0
- package/src/tools/score.d.ts +4 -0
- package/src/tools/score.js +35 -0
- package/src/tools/types.d.ts +84 -0
- package/src/tools/types.js +2 -0
- package/src/tools.d.ts +6 -0
- package/src/tools.js +4 -0
- package/src/virtual.d.ts +11 -0
- package/src/vite.d.ts +8 -0
- package/src/vite.js +43 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { clipClock, clipSecondAt } from "./clips.js";
|
|
2
|
+
/** What `kit.seek(video, s)` asked a clip to show in this render, instead of film time less its start. */
|
|
3
|
+
export const videoSeeks = new Map();
|
|
4
|
+
const watched = new WeakSet();
|
|
5
|
+
const warned = new WeakMap();
|
|
6
|
+
const slow = new WeakSet();
|
|
7
|
+
const seekMarks = new WeakMap();
|
|
8
|
+
/** How long after a seek its frame is waited for, at most. */
|
|
9
|
+
export const FRAME_SHOWN_WAIT_MS = 1500;
|
|
10
|
+
/** The event a clip gets once the frame of its last seek is shown. */
|
|
11
|
+
export const FRAME_SHOWN_EVENT = 'filmframeshown';
|
|
12
|
+
/**
|
|
13
|
+
* Only WebKit says a seek is over before its frame shows; elsewhere the frame callback need not come for a seek that
|
|
14
|
+
* lands on the frame shown already (Chrome), so the wait is left to WebKit. Told apart by a CSS property only it has.
|
|
15
|
+
*/
|
|
16
|
+
let webKit = null;
|
|
17
|
+
function inWebKit() {
|
|
18
|
+
if (webKit === null) {
|
|
19
|
+
try {
|
|
20
|
+
webKit = typeof CSS !== 'undefined' && CSS.supports('-webkit-nbsp-mode', 'space');
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
webKit = false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return webKit;
|
|
27
|
+
}
|
|
28
|
+
function markSeek(video) {
|
|
29
|
+
if (typeof video.requestVideoFrameCallback !== 'function' || !inWebKit())
|
|
30
|
+
return;
|
|
31
|
+
const n = (seekMarks.get(video)?.n ?? 0) + 1;
|
|
32
|
+
const mark = { n, shown: false, at: performance.now() };
|
|
33
|
+
seekMarks.set(video, mark);
|
|
34
|
+
video.requestVideoFrameCallback(() => {
|
|
35
|
+
if (seekMarks.get(video)?.n !== n)
|
|
36
|
+
return;
|
|
37
|
+
mark.shown = true;
|
|
38
|
+
video.dispatchEvent(new Event(FRAME_SHOWN_EVENT));
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Whether the clip's last seek, made less than `ms` ago, has not shown its frame yet. A hidden page shows no frames,
|
|
43
|
+
* so there it is never waited for.
|
|
44
|
+
*/
|
|
45
|
+
export function frameUnshown(video, ms) {
|
|
46
|
+
const mark = seekMarks.get(video);
|
|
47
|
+
if (mark === undefined || mark.shown)
|
|
48
|
+
return false;
|
|
49
|
+
if (typeof document !== 'undefined' && document.hidden)
|
|
50
|
+
return false;
|
|
51
|
+
return performance.now() - mark.at < ms;
|
|
52
|
+
}
|
|
53
|
+
export function warnOnce(video, key, message) {
|
|
54
|
+
const said = warned.get(video) ?? new Set();
|
|
55
|
+
warned.set(video, said);
|
|
56
|
+
if (said.has(key))
|
|
57
|
+
return;
|
|
58
|
+
said.add(key);
|
|
59
|
+
console.warn('video ' + (video.id ? '#' + video.id + ' ' : '') + message);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The clip's own second at film time `t` (kit.video's `from`, `dur` and `speed` applied), looped or held at its end once
|
|
63
|
+
* its duration is known. Half a millisecond on, so a frame boundary reached through float arithmetic lands on that frame.
|
|
64
|
+
*/
|
|
65
|
+
export function videoTime(video, t) {
|
|
66
|
+
const explicit = videoSeeks.has(video);
|
|
67
|
+
const timed = clipSecondAt(video, t);
|
|
68
|
+
let at = explicit ? (videoSeeks.get(video) ?? 0) : timed.second;
|
|
69
|
+
const duration = video.duration;
|
|
70
|
+
let live = explicit ? at >= 0 : timed.on;
|
|
71
|
+
if (duration > 0 && isFinite(duration)) {
|
|
72
|
+
if (video.loop) {
|
|
73
|
+
if (at >= 0)
|
|
74
|
+
at = at % duration;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
live = live && at < duration;
|
|
78
|
+
at = Math.min(at, duration - 0.001);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { at: Math.max(0, at) + 0.0005, live, explicit };
|
|
82
|
+
}
|
|
83
|
+
export function watchVideo(video) {
|
|
84
|
+
if (watched.has(video))
|
|
85
|
+
return;
|
|
86
|
+
watched.add(video);
|
|
87
|
+
video.playsInline = true;
|
|
88
|
+
video.autoplay = false;
|
|
89
|
+
const failed = () => {
|
|
90
|
+
console.error('video ' +
|
|
91
|
+
(video.id ? '#' + video.id + ' ' : '') +
|
|
92
|
+
'did not load (' +
|
|
93
|
+
(video.error ? 'code ' + video.error.code : 'no source') +
|
|
94
|
+
'): use an uploaded .mp4/.webm asset, a data: or a blob: URL');
|
|
95
|
+
};
|
|
96
|
+
video.addEventListener('error', failed);
|
|
97
|
+
video.addEventListener('seeking', () => markSeek(video));
|
|
98
|
+
if (video.error)
|
|
99
|
+
failed();
|
|
100
|
+
}
|
|
101
|
+
/** How long a small frame (a still for the timeline, a check of the picture) waits for each clip's frame. */
|
|
102
|
+
export const SMALL_CLIP_WAIT_MS = 2000;
|
|
103
|
+
/** Whether a clip on the page has not reached the frame it was last sought to (a frame drawn now may show it stale). */
|
|
104
|
+
export function clipsBehind() {
|
|
105
|
+
for (const video of document.querySelectorAll('video')) {
|
|
106
|
+
if ((!video.currentSrc && !video.src) || video.error || video.getClientRects().length === 0)
|
|
107
|
+
continue;
|
|
108
|
+
if (video.seeking || video.readyState < 2)
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Resolves once the clip shows the frame it was sought to, or failed. A clip that misses the deadline is warned
|
|
115
|
+
* about once and afterwards waited on only briefly. quick: a small frame's wait, SMALL_CLIP_WAIT_MS at most, which
|
|
116
|
+
* leaves the slow mark alone.
|
|
117
|
+
*/
|
|
118
|
+
function settled(video, quick) {
|
|
119
|
+
const ms = quick ? SMALL_CLIP_WAIT_MS : slow.has(video) ? 250 : 4000;
|
|
120
|
+
const sought = () => !video.seeking && video.readyState >= 2;
|
|
121
|
+
// The frame is waited for as long as Safari has been seen to take to show it, and no longer.
|
|
122
|
+
const ready = () => sought() && !frameUnshown(video, Math.min(ms, FRAME_SHOWN_WAIT_MS));
|
|
123
|
+
if ((!video.currentSrc && !video.src) || video.error || ready())
|
|
124
|
+
return null;
|
|
125
|
+
const events = ['seeked', 'loadeddata', 'error', FRAME_SHOWN_EVENT];
|
|
126
|
+
return new Promise((resolve) => {
|
|
127
|
+
const done = (ok) => {
|
|
128
|
+
clearTimeout(timer);
|
|
129
|
+
clearTimeout(frameTimer);
|
|
130
|
+
for (const name of events)
|
|
131
|
+
video.removeEventListener(name, check);
|
|
132
|
+
if (!quick) {
|
|
133
|
+
if (ok)
|
|
134
|
+
slow.delete(video);
|
|
135
|
+
else {
|
|
136
|
+
slow.add(video);
|
|
137
|
+
warnOnce(video, 'slow', 'did not reach its frame within ' + ms + ' ms; exported frames may show a stale picture of it');
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
resolve();
|
|
141
|
+
};
|
|
142
|
+
const check = () => {
|
|
143
|
+
if (video.error || ready())
|
|
144
|
+
done(true);
|
|
145
|
+
};
|
|
146
|
+
// A seek that is over but whose frame never showed (one that landed on the frame shown already) is not slow.
|
|
147
|
+
const timer = setTimeout(() => done(sought()), ms);
|
|
148
|
+
// No event marks the end of the wait for a frame that never shows: look again once it is over.
|
|
149
|
+
const frameTimer = setTimeout(check, Math.min(ms, FRAME_SHOWN_WAIT_MS) + 1);
|
|
150
|
+
for (const name of events)
|
|
151
|
+
video.addEventListener(name, check);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
/** A playing clip this far or more from its time is sought to it. */
|
|
155
|
+
export const RESEEK_SECONDS = 0.25;
|
|
156
|
+
/** Nearer than that but this far or more, it plays a little faster or slower until it catches up. */
|
|
157
|
+
export const NUDGE_FROM_SECONDS = 0.04;
|
|
158
|
+
/** By at most this share of its rate. */
|
|
159
|
+
export const MOST_NUDGE = 0.05;
|
|
160
|
+
/**
|
|
161
|
+
* The rate a playing clip plays at: the film's, or, while it is `drift` seconds behind (positive) or ahead of its time
|
|
162
|
+
* by NUDGE_FROM_SECONDS or more, faster or slower by half the drift as a share, MOST_NUDGE at most. So a clip whose
|
|
163
|
+
* sound narration plays stays within a frame or two of it, unheard, without a seek's stutter.
|
|
164
|
+
*/
|
|
165
|
+
export function nudgedRate(drift, base) {
|
|
166
|
+
if (!(Math.abs(drift) >= NUDGE_FROM_SECONDS))
|
|
167
|
+
return base;
|
|
168
|
+
return base * (1 + Math.max(-MOST_NUDGE, Math.min(MOST_NUDGE, drift / 2)));
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* play: clips play along, corrected when they drift; hold: paused on their frame; frame: an exported frame,
|
|
172
|
+
* with a promise that settles once every clip shows its own.
|
|
173
|
+
*/
|
|
174
|
+
export function syncVideos(stage, t, mode, clock, quick = false) {
|
|
175
|
+
if (!stage)
|
|
176
|
+
return null;
|
|
177
|
+
const waits = [];
|
|
178
|
+
const eps = 0.5 / (clock.fps || 60);
|
|
179
|
+
// Videos laid over the film by hand follow played time on their own (laid/layer.ts).
|
|
180
|
+
for (const video of stage.querySelectorAll('video:not([data-film-placed] video)')) {
|
|
181
|
+
const wait = syncVideo(video, t, mode, eps, clipClock(video, clock), quick);
|
|
182
|
+
if (wait)
|
|
183
|
+
waits.push(wait);
|
|
184
|
+
}
|
|
185
|
+
return waits.length ? Promise.all(waits) : null;
|
|
186
|
+
}
|
|
187
|
+
function syncVideo(video, t, mode, eps, clock, quick) {
|
|
188
|
+
watchVideo(video);
|
|
189
|
+
video.muted = true;
|
|
190
|
+
if (video.error)
|
|
191
|
+
return null;
|
|
192
|
+
const time = videoTime(video, t);
|
|
193
|
+
if (mode === 'play' && time.live && !time.explicit) {
|
|
194
|
+
const steady = !video.paused && !video.seeking && video.readyState >= 3;
|
|
195
|
+
const drift = time.at - video.currentTime;
|
|
196
|
+
const rate = nudgedRate(steady ? drift : 0, clock.retimed ? clock.rate : 1);
|
|
197
|
+
if (Math.abs(video.playbackRate - rate) > 1e-4)
|
|
198
|
+
video.playbackRate = rate;
|
|
199
|
+
if ((video.paused || steady) && Math.abs(drift) > RESEEK_SECONDS)
|
|
200
|
+
video.currentTime = time.at;
|
|
201
|
+
if (video.paused) {
|
|
202
|
+
video.play().catch((error) => {
|
|
203
|
+
const name = error instanceof Error ? error.name : String(error);
|
|
204
|
+
if (name !== 'AbortError')
|
|
205
|
+
warnOnce(video, 'play', 'could not play: ' + name);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
if (!video.paused)
|
|
211
|
+
video.pause();
|
|
212
|
+
if (Math.abs(video.currentTime - time.at) > eps) {
|
|
213
|
+
video.currentTime = time.at;
|
|
214
|
+
// Marked now, not when 'seeking' arrives, so the wait below already knows of this seek.
|
|
215
|
+
markSeek(video);
|
|
216
|
+
}
|
|
217
|
+
return mode === 'frame' ? settled(video, quick) : null;
|
|
218
|
+
}
|
package/src/stills.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stills of a video artifact, drawn as its export draws them, by a headless Chromium driving the tools page (see
|
|
3
|
+
* `./tools`). It needs only Node (22 or later: it uses the built-in WebSocket) and a Chromium: `CHROME_PATH`, else one
|
|
4
|
+
* from Playwright's browser cache, else Chrome or Chromium where the system keeps it. It speaks the DevTools protocol
|
|
5
|
+
* itself, so it installs nothing. Shared by `scripts/stills.mjs` and the `open-artifacts stills` command.
|
|
6
|
+
*/
|
|
7
|
+
import type { RenderMode } from './protocol.js';
|
|
8
|
+
import type { SceneLevels, ScoreReport, ToolsLogLine } from './tools/types.js';
|
|
9
|
+
/** A stills request that cannot be met: printed as it is, without a stack. */
|
|
10
|
+
export declare class StillsError extends Error {
|
|
11
|
+
}
|
|
12
|
+
export interface StillsRequest {
|
|
13
|
+
/** The artifact folder: db/ (film/, modules/, assets/) and _blobs/. */
|
|
14
|
+
root: string;
|
|
15
|
+
/** Played seconds. */
|
|
16
|
+
times: number[];
|
|
17
|
+
/** Times the film's own size, above 0 and at most 4. */
|
|
18
|
+
scale: number;
|
|
19
|
+
mode: RenderMode;
|
|
20
|
+
/** Where the PNGs go; a new temporary folder when `null`. */
|
|
21
|
+
out: string | null;
|
|
22
|
+
/** A version other than the first, by its id; the first when left out. */
|
|
23
|
+
version?: string;
|
|
24
|
+
/** An aspect ratio the film lists besides its own (`9:16`); its own when left out. */
|
|
25
|
+
ratio?: string;
|
|
26
|
+
/** Print one JSON object at the end ({@link StillsJson}) instead of lines. */
|
|
27
|
+
json?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** What `stills --json` prints: one object, once everything is drawn. */
|
|
30
|
+
export interface StillsJson {
|
|
31
|
+
problems: string[];
|
|
32
|
+
/** Set when the film did not load; nothing else is then filled in. */
|
|
33
|
+
error?: string;
|
|
34
|
+
film: {
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
fps: number;
|
|
38
|
+
duration: number;
|
|
39
|
+
scenes: unknown;
|
|
40
|
+
} | null;
|
|
41
|
+
/** The version drawn (`null`: the first) and the aspect ratio drawn (`null`: the film's own). */
|
|
42
|
+
view: {
|
|
43
|
+
version: string | null;
|
|
44
|
+
ratio: string | null;
|
|
45
|
+
};
|
|
46
|
+
stills: Array<{
|
|
47
|
+
t: number;
|
|
48
|
+
file: string;
|
|
49
|
+
} | {
|
|
50
|
+
t: number;
|
|
51
|
+
error: string;
|
|
52
|
+
}>;
|
|
53
|
+
score: ScoreReport | null;
|
|
54
|
+
/** The band's levels per scene; null for a score with no band, or when they could not be measured. */
|
|
55
|
+
levels: SceneLevels[] | null;
|
|
56
|
+
log: ToolsLogLine[];
|
|
57
|
+
}
|
|
58
|
+
export declare const STILLS_MAX_SCALE = 4;
|
|
59
|
+
/** The tools page's file name beside a built `open-artifacts` command. */
|
|
60
|
+
export declare const STILLS_TOOLS_PAGE = "motion-tools.html";
|
|
61
|
+
/** Played seconds, comma-separated (`0.8,2.4,5`); `null` when any is not a second of the film. */
|
|
62
|
+
export declare function parseStillTimes(text: string): number[] | null;
|
|
63
|
+
/** The scale a still is drawn at, or `null` outside (0, 4]. */
|
|
64
|
+
export declare function parseStillScale(text: string | undefined): number | null;
|
|
65
|
+
/** The PNG a time is written to. */
|
|
66
|
+
export declare const stillFileName: (t: number) => string;
|
|
67
|
+
/** The film's documents (film/, modules/, assets/ and its versions; never people's own data/) and the uploads its assets name. */
|
|
68
|
+
export declare function readStillsFilm(root: string, warn?: (line: string) => void): {
|
|
69
|
+
documents: Record<string, string>;
|
|
70
|
+
blobs: Record<string, string>;
|
|
71
|
+
};
|
|
72
|
+
/** The documents the view draws: version `version` (the first when left out) at aspect ratio `ratio` (its own when left out). */
|
|
73
|
+
export declare function stillsDocumentsFor(documents: Readonly<Record<string, string>>, view: {
|
|
74
|
+
version?: string;
|
|
75
|
+
ratio?: string;
|
|
76
|
+
}): Record<string, string>;
|
|
77
|
+
/** A Chrome or Chromium to draw with: `CHROME_PATH`, Playwright's cache, then the system's. */
|
|
78
|
+
export declare function findChromium(env?: NodeJS.ProcessEnv): string;
|
|
79
|
+
/**
|
|
80
|
+
* One scene's levels as a line: `levels dawn 0-2.5s: all -1 dB; drums -4 (loudest -1), keys -12 (loudest -9)`, every
|
|
81
|
+
* number in dB against the whole band's average over the film.
|
|
82
|
+
*/
|
|
83
|
+
export declare function levelsLine(scene: SceneLevels): string;
|
|
84
|
+
/**
|
|
85
|
+
* Draws the stills asked for with the tools page's HTML (`buildToolsPage`, or the copy a build ships), printing
|
|
86
|
+
* through `say`: the film's problems, its size and length, each PNG's path, the score's counts, the band's levels
|
|
87
|
+
* scene by scene (when the score has a band) and the film's own warnings and errors; with `json`, all of it as one
|
|
88
|
+
* {@link StillsJson} at the end. Resolves to the exit code: 1 when the film did not load.
|
|
89
|
+
*/
|
|
90
|
+
export declare function drawStills(request: StillsRequest, toolsPage: string, say?: (line: string) => void): Promise<number>;
|
package/src/stills.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stills of a video artifact, drawn as its export draws them, by a headless Chromium driving the tools page (see
|
|
3
|
+
* `./tools`). It needs only Node (22 or later: it uses the built-in WebSocket) and a Chromium: `CHROME_PATH`, else one
|
|
4
|
+
* from Playwright's browser cache, else Chrome or Chromium where the system keeps it. It speaks the DevTools protocol
|
|
5
|
+
* itself, so it installs nothing. Shared by `scripts/stills.mjs` and the `open-artifacts stills` command.
|
|
6
|
+
*/
|
|
7
|
+
import { spawn } from 'node:child_process';
|
|
8
|
+
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
9
|
+
import { homedir, tmpdir } from 'node:os';
|
|
10
|
+
import { join, resolve } from 'node:path';
|
|
11
|
+
import { pathToFileURL } from 'node:url';
|
|
12
|
+
import { BAND_TRACKS, documentsInViewRatio, filmExtraRatios, FILM_COLLECTIONS, isPlainObject, readFilmVersions, VERSION_COLLECTIONS, versionFilmDocuments } from '@openartifactsai/motion-core';
|
|
13
|
+
/** A stills request that cannot be met: printed as it is, without a stack. */
|
|
14
|
+
export class StillsError extends Error {
|
|
15
|
+
}
|
|
16
|
+
export const STILLS_MAX_SCALE = 4;
|
|
17
|
+
/** The tools page's file name beside a built `open-artifacts` command. */
|
|
18
|
+
export const STILLS_TOOLS_PAGE = 'motion-tools.html';
|
|
19
|
+
/** Played seconds, comma-separated (`0.8,2.4,5`); `null` when any is not a second of the film. */
|
|
20
|
+
export function parseStillTimes(text) {
|
|
21
|
+
const times = text.split(',').map((part) => (part.trim() === '' ? Number.NaN : Number(part)));
|
|
22
|
+
return times.length > 0 && times.every((t) => Number.isFinite(t) && t >= 0) ? times : null;
|
|
23
|
+
}
|
|
24
|
+
/** The scale a still is drawn at, or `null` outside (0, 4]. */
|
|
25
|
+
export function parseStillScale(text) {
|
|
26
|
+
if (text === undefined)
|
|
27
|
+
return 1;
|
|
28
|
+
const scale = Number(text);
|
|
29
|
+
return scale > 0 && scale <= STILLS_MAX_SCALE ? scale : null;
|
|
30
|
+
}
|
|
31
|
+
/** The PNG a time is written to. */
|
|
32
|
+
export const stillFileName = (t) => 't_' + t.toFixed(3) + '.png';
|
|
33
|
+
const plainFile = (path) => lstatSync(path, { throwIfNoEntry: false })?.isFile() === true;
|
|
34
|
+
/** The film's documents (film/, modules/, assets/ and its versions; never people's own data/) and the uploads its assets name. */
|
|
35
|
+
export function readStillsFilm(root, warn = () => undefined) {
|
|
36
|
+
const db = join(root, 'db');
|
|
37
|
+
if (!plainFile(join(db, 'film', 'meta.json')))
|
|
38
|
+
throw new StillsError(root + ' is not a motion artifact: it has no db/film/meta.json.');
|
|
39
|
+
const documents = {};
|
|
40
|
+
for (const collection of [...FILM_COLLECTIONS, ...VERSION_COLLECTIONS]) {
|
|
41
|
+
const folder = join(db, collection);
|
|
42
|
+
if (!existsSync(folder))
|
|
43
|
+
continue;
|
|
44
|
+
for (const name of readdirSync(folder)) {
|
|
45
|
+
if (!name.endsWith('.json'))
|
|
46
|
+
continue;
|
|
47
|
+
const path = join(folder, name);
|
|
48
|
+
if (!plainFile(path)) {
|
|
49
|
+
warn('skipped ' + path + ': not a plain file');
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
documents[collection + '/' + name.slice(0, -'.json'.length)] = readFileSync(path, 'utf8');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const blobs = {};
|
|
56
|
+
const uploads = join(root, '_blobs');
|
|
57
|
+
const stored = existsSync(uploads) ? readdirSync(uploads) : [];
|
|
58
|
+
for (const [path, text] of Object.entries(documents)) {
|
|
59
|
+
if (!path.startsWith('assets/'))
|
|
60
|
+
continue;
|
|
61
|
+
let ref = '';
|
|
62
|
+
try {
|
|
63
|
+
ref = String(JSON.parse(text).ref ?? '');
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const id = /^\/?_blob\/([0-9a-f]{32})$/.exec(ref)?.[1];
|
|
69
|
+
if (id === undefined || id in blobs)
|
|
70
|
+
continue;
|
|
71
|
+
const file = stored.find((name) => name === id || name.startsWith(id + '.'));
|
|
72
|
+
if (file !== undefined && plainFile(join(uploads, file)))
|
|
73
|
+
blobs[id] = readFileSync(join(uploads, file)).toString('base64');
|
|
74
|
+
}
|
|
75
|
+
return { documents, blobs };
|
|
76
|
+
}
|
|
77
|
+
const parsed = (text) => {
|
|
78
|
+
try {
|
|
79
|
+
const value = JSON.parse(text);
|
|
80
|
+
return isPlainObject(value) ? value : null;
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
/** The documents the view draws: version `version` (the first when left out) at aspect ratio `ratio` (its own when left out). */
|
|
87
|
+
export function stillsDocumentsFor(documents, view) {
|
|
88
|
+
const isFilm = (path) => FILM_COLLECTIONS.some((collection) => path.startsWith(collection + '/'));
|
|
89
|
+
if (view.version === undefined && view.ratio === undefined)
|
|
90
|
+
return Object.fromEntries(Object.entries(documents).filter(([path]) => isFilm(path)));
|
|
91
|
+
const stored = new Map();
|
|
92
|
+
for (const [path, text] of Object.entries(documents)) {
|
|
93
|
+
const value = parsed(text);
|
|
94
|
+
if (value !== null)
|
|
95
|
+
stored.set(path, value);
|
|
96
|
+
}
|
|
97
|
+
const versions = readFilmVersions(stored);
|
|
98
|
+
const version = view.version ?? null;
|
|
99
|
+
if (version !== null && !versions.records.some((record) => record.id === version)) {
|
|
100
|
+
const ids = versions.records.map((record) => record.id);
|
|
101
|
+
throw new StillsError(ids.length === 0 ? 'The film has no other versions.' : `No version ${version}: the film's other versions are ${ids.join(', ')}.`);
|
|
102
|
+
}
|
|
103
|
+
const film = versionFilmDocuments(stored, version, versions);
|
|
104
|
+
const ratio = view.ratio ?? null;
|
|
105
|
+
if (ratio !== null) {
|
|
106
|
+
const listed = filmExtraRatios(film).map((shape) => shape.ratio);
|
|
107
|
+
if (!listed.includes(ratio))
|
|
108
|
+
throw new StillsError(listed.length === 0 ? 'The film is laid out in its own aspect ratio only.' : `The film is not laid out in ${ratio}: it lists ${listed.join(', ')}.`);
|
|
109
|
+
}
|
|
110
|
+
const drawn = documentsInViewRatio(film, { version, ratio });
|
|
111
|
+
return Object.fromEntries([...drawn].filter(([path]) => isFilm(path)).map(([path, value]) => [path, JSON.stringify(value)]));
|
|
112
|
+
}
|
|
113
|
+
const safeList = (dir) => {
|
|
114
|
+
try {
|
|
115
|
+
return readdirSync(dir);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
/** A Chrome or Chromium to draw with: `CHROME_PATH`, Playwright's cache, then the system's. */
|
|
122
|
+
export function findChromium(env = process.env) {
|
|
123
|
+
if (env.CHROME_PATH) {
|
|
124
|
+
if (existsSync(env.CHROME_PATH))
|
|
125
|
+
return env.CHROME_PATH;
|
|
126
|
+
throw new StillsError('CHROME_PATH does not exist: ' + env.CHROME_PATH);
|
|
127
|
+
}
|
|
128
|
+
const caches = [env.PLAYWRIGHT_BROWSERS_PATH, join(homedir(), 'Library', 'Caches', 'ms-playwright'), join(homedir(), '.cache', 'ms-playwright'), '/opt/pw-browsers'].filter((path) => path !== undefined && path !== '' && existsSync(path));
|
|
129
|
+
const found = [];
|
|
130
|
+
for (const cache of caches) {
|
|
131
|
+
for (const build of safeList(cache).sort().reverse()) {
|
|
132
|
+
const home = join(cache, build);
|
|
133
|
+
if (build.startsWith('chromium_headless_shell-'))
|
|
134
|
+
for (const sub of safeList(home))
|
|
135
|
+
found.push(join(home, sub, 'chrome-headless-shell'));
|
|
136
|
+
else if (build.startsWith('chromium-')) {
|
|
137
|
+
found.push(join(home, 'chrome-linux', 'chrome'), join(home, 'chrome-linux64', 'chrome'));
|
|
138
|
+
for (const sub of safeList(home))
|
|
139
|
+
found.push(join(home, sub, 'Chromium.app', 'Contents', 'MacOS', 'Chromium'));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
found.push('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/Applications/Chromium.app/Contents/MacOS/Chromium', '/usr/bin/google-chrome', '/usr/bin/google-chrome-stable', '/usr/bin/chromium', '/usr/bin/chromium-browser');
|
|
144
|
+
const binary = found.find((path) => existsSync(path));
|
|
145
|
+
if (binary === undefined)
|
|
146
|
+
throw new StillsError('No Chromium found. Set CHROME_PATH to a Chrome or Chromium binary.');
|
|
147
|
+
return binary;
|
|
148
|
+
}
|
|
149
|
+
// The film's code gets no network: every host resolves to nothing, through a proxy that is not there.
|
|
150
|
+
const CHROMIUM_ARGS = [
|
|
151
|
+
'--headless=new',
|
|
152
|
+
'--remote-debugging-port=0',
|
|
153
|
+
'--no-first-run',
|
|
154
|
+
'--no-default-browser-check',
|
|
155
|
+
'--disable-extensions',
|
|
156
|
+
'--disable-background-networking',
|
|
157
|
+
'--disable-component-update',
|
|
158
|
+
'--mute-audio',
|
|
159
|
+
'--hide-scrollbars',
|
|
160
|
+
'--force-color-profile=srgb',
|
|
161
|
+
'--font-render-hinting=none',
|
|
162
|
+
'--disable-lcd-text',
|
|
163
|
+
'--window-size=1920,1080',
|
|
164
|
+
'--host-resolver-rules=MAP * ^NOTFOUND',
|
|
165
|
+
'--proxy-server=http://127.0.0.1:9',
|
|
166
|
+
'--proxy-bypass-list=<-loopback>',
|
|
167
|
+
];
|
|
168
|
+
function launch(binary, profile) {
|
|
169
|
+
// Keep this temporary profile independent of the macOS login keychain: under a HOME that has none, Chrome stops to
|
|
170
|
+
// ask the person for one.
|
|
171
|
+
const keychain = process.platform === 'darwin' ? ['--use-mock-keychain'] : [];
|
|
172
|
+
const child = spawn(binary, [...CHROMIUM_ARGS, ...keychain, '--user-data-dir=' + profile, 'about:blank'], { stdio: ['ignore', 'ignore', 'pipe'] });
|
|
173
|
+
return new Promise((done, fail) => {
|
|
174
|
+
let said = '';
|
|
175
|
+
const timer = setTimeout(() => fail(new Error('Chromium did not start within 30 s:\n' + said.slice(-2000))), 30000);
|
|
176
|
+
child.stderr?.on('data', (chunk) => {
|
|
177
|
+
said += String(chunk);
|
|
178
|
+
const url = /DevTools listening on (ws:\/\/\S+)/.exec(said)?.[1];
|
|
179
|
+
if (url === undefined)
|
|
180
|
+
return;
|
|
181
|
+
clearTimeout(timer);
|
|
182
|
+
done({ child, url });
|
|
183
|
+
});
|
|
184
|
+
child.on('error', (error) => {
|
|
185
|
+
clearTimeout(timer);
|
|
186
|
+
fail(error);
|
|
187
|
+
});
|
|
188
|
+
child.on('exit', (code) => fail(new Error('Chromium exited (' + String(code) + '):\n' + said.slice(-2000))));
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
/** The DevTools protocol over one WebSocket, commands sent to one page's session. */
|
|
192
|
+
async function connect(url) {
|
|
193
|
+
const socket = new WebSocket(url);
|
|
194
|
+
await new Promise((done, fail) => {
|
|
195
|
+
socket.onopen = () => done();
|
|
196
|
+
socket.onerror = () => fail(new Error('could not reach Chromium at ' + url));
|
|
197
|
+
});
|
|
198
|
+
let id = 0;
|
|
199
|
+
const pending = new Map();
|
|
200
|
+
socket.onmessage = (event) => {
|
|
201
|
+
const message = JSON.parse(String(event.data));
|
|
202
|
+
const waiter = message.id === undefined ? undefined : pending.get(message.id);
|
|
203
|
+
if (waiter === undefined || message.id === undefined)
|
|
204
|
+
return;
|
|
205
|
+
pending.delete(message.id);
|
|
206
|
+
if (message.error)
|
|
207
|
+
waiter.fail(new Error(message.error.message));
|
|
208
|
+
else
|
|
209
|
+
waiter.done(message.result);
|
|
210
|
+
};
|
|
211
|
+
const send = (method, params = {}, sessionId) => new Promise((done, fail) => {
|
|
212
|
+
pending.set(++id, { done: (value) => done(value), fail });
|
|
213
|
+
socket.send(JSON.stringify({ id, method, params, ...(sessionId ? { sessionId } : {}) }));
|
|
214
|
+
});
|
|
215
|
+
const { targetId } = await send('Target.createTarget', { url: 'about:blank' });
|
|
216
|
+
const { sessionId } = await send('Target.attachToTarget', { targetId, flatten: true });
|
|
217
|
+
return {
|
|
218
|
+
navigate: (to) => send('Page.navigate', { url: to }, sessionId),
|
|
219
|
+
async evaluate(expression) {
|
|
220
|
+
const { result, exceptionDetails } = await send('Runtime.evaluate', { expression, awaitPromise: true, returnByValue: true }, sessionId);
|
|
221
|
+
if (exceptionDetails)
|
|
222
|
+
throw new Error(exceptionDetails.exception?.description ?? exceptionDetails.text);
|
|
223
|
+
return result.value;
|
|
224
|
+
},
|
|
225
|
+
async close() {
|
|
226
|
+
await send('Browser.close').catch(() => undefined);
|
|
227
|
+
socket.close();
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
const messageOf = (error) => (error instanceof Error ? error.message : String(error));
|
|
232
|
+
const signed = (db) => (db > 0 ? '+' : '') + String(db);
|
|
233
|
+
const seconds = (t) => String(Math.round(t * 100) / 100);
|
|
234
|
+
/**
|
|
235
|
+
* One scene's levels as a line: `levels dawn 0-2.5s: all -1 dB; drums -4 (loudest -1), keys -12 (loudest -9)`, every
|
|
236
|
+
* number in dB against the whole band's average over the film.
|
|
237
|
+
*/
|
|
238
|
+
export function levelsLine(scene) {
|
|
239
|
+
const tracks = BAND_TRACKS.flatMap((track) => {
|
|
240
|
+
const level = scene.tracks[track];
|
|
241
|
+
return level === undefined ? [] : [track + ' ' + signed(level[0]) + ' (loudest ' + signed(level[1]) + ')'];
|
|
242
|
+
});
|
|
243
|
+
return ('levels ' + scene.scene + ' ' + seconds(scene.at) + '-' + seconds(scene.at + scene.dur) + 's: all ' + signed(scene.all) + ' dB; ' +
|
|
244
|
+
(tracks.length === 0 ? 'no track heard' : tracks.join(', ')));
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Draws the stills asked for with the tools page's HTML (`buildToolsPage`, or the copy a build ships), printing
|
|
248
|
+
* through `say`: the film's problems, its size and length, each PNG's path, the score's counts, the band's levels
|
|
249
|
+
* scene by scene (when the score has a band) and the film's own warnings and errors; with `json`, all of it as one
|
|
250
|
+
* {@link StillsJson} at the end. Resolves to the exit code: 1 when the film did not load.
|
|
251
|
+
*/
|
|
252
|
+
export async function drawStills(request, toolsPage, say = (line) => console.log(line)) {
|
|
253
|
+
const root = resolve(request.root);
|
|
254
|
+
const film = readStillsFilm(root, (line) => console.warn(line));
|
|
255
|
+
const view = { ...(request.version !== undefined ? { version: request.version } : {}), ...(request.ratio !== undefined ? { ratio: request.ratio } : {}) };
|
|
256
|
+
const files = { documents: stillsDocumentsFor(film.documents, view), blobs: film.blobs, mode: request.mode };
|
|
257
|
+
const binary = findChromium();
|
|
258
|
+
const work = mkdtempSync(join(tmpdir(), 'film-tools-'));
|
|
259
|
+
const out = request.out === null ? mkdtempSync(join(tmpdir(), 'film-stills-')) : resolve(request.out);
|
|
260
|
+
mkdirSync(out, { recursive: true });
|
|
261
|
+
const page = join(work, 'tools.html');
|
|
262
|
+
writeFileSync(page, toolsPage);
|
|
263
|
+
const json = request.json === true;
|
|
264
|
+
const report = { problems: [], film: null, view: { version: request.version ?? null, ratio: request.ratio ?? null }, stills: [], score: null, levels: null, log: [] };
|
|
265
|
+
const line = (text) => {
|
|
266
|
+
if (!json)
|
|
267
|
+
say(text);
|
|
268
|
+
};
|
|
269
|
+
let child = null;
|
|
270
|
+
let browser = null;
|
|
271
|
+
try {
|
|
272
|
+
const launched = await launch(binary, join(work, 'profile'));
|
|
273
|
+
child = launched.child;
|
|
274
|
+
browser = await connect(launched.url);
|
|
275
|
+
await browser.navigate(pathToFileURL(page).href);
|
|
276
|
+
for (let i = 0; i < 100 && (await browser.evaluate('typeof window.filmTools')) !== 'object'; i++)
|
|
277
|
+
await new Promise((next) => setTimeout(next, 100));
|
|
278
|
+
const loaded = await browser.evaluate('window.filmTools.load(' + JSON.stringify(files) + ')');
|
|
279
|
+
report.problems = loaded.problems;
|
|
280
|
+
for (const problem of loaded.problems)
|
|
281
|
+
line('problem: ' + problem);
|
|
282
|
+
if ('error' in loaded)
|
|
283
|
+
throw new Error('the film did not load: ' + loaded.error.message);
|
|
284
|
+
const { width, height, fps, duration, scenes } = loaded.meta;
|
|
285
|
+
report.film = { width, height, fps, duration, scenes };
|
|
286
|
+
line(JSON.stringify(report.film));
|
|
287
|
+
for (const t of request.times) {
|
|
288
|
+
try {
|
|
289
|
+
const png = await browser.evaluate('window.filmTools.frame(' + String(t) + ', { scale: ' + String(request.scale) + ' })');
|
|
290
|
+
const file = join(out, stillFileName(t));
|
|
291
|
+
writeFileSync(file, Buffer.from(png.slice(png.indexOf(',') + 1), 'base64'));
|
|
292
|
+
report.stills.push({ t, file });
|
|
293
|
+
line(file);
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
report.stills.push({ t, error: messageOf(error) });
|
|
297
|
+
line('frame ' + String(t) + ' failed: ' + messageOf(error));
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
report.score = await browser.evaluate('window.filmTools.scoreReport()');
|
|
301
|
+
line('score: ' + JSON.stringify(report.score));
|
|
302
|
+
// Levels are extra: a page that cannot measure them leaves them out and the stills stand.
|
|
303
|
+
report.levels = await browser.evaluate('window.filmTools.soundLevels?.() ?? null').catch(() => null);
|
|
304
|
+
for (const scene of report.levels ?? [])
|
|
305
|
+
line(levelsLine(scene));
|
|
306
|
+
report.log = (await browser.evaluate('window.filmTools.consoleLog()')).filter((entry) => entry.level !== 'log');
|
|
307
|
+
for (const entry of report.log)
|
|
308
|
+
line('film ' + entry.level + ': ' + entry.text);
|
|
309
|
+
if (json)
|
|
310
|
+
say(JSON.stringify(report));
|
|
311
|
+
return 0;
|
|
312
|
+
}
|
|
313
|
+
catch (error) {
|
|
314
|
+
console.error(messageOf(error));
|
|
315
|
+
if (json)
|
|
316
|
+
say(JSON.stringify({ ...report, error: messageOf(error) }));
|
|
317
|
+
return 1;
|
|
318
|
+
}
|
|
319
|
+
finally {
|
|
320
|
+
await browser?.close();
|
|
321
|
+
const running = child;
|
|
322
|
+
if (running !== null && running.exitCode === null)
|
|
323
|
+
await new Promise((done) => {
|
|
324
|
+
running.once('exit', () => done());
|
|
325
|
+
running.kill();
|
|
326
|
+
});
|
|
327
|
+
rmSync(work, { recursive: true, force: true });
|
|
328
|
+
}
|
|
329
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { StageFilm } from '../protocol.js';
|
|
2
|
+
import type { ToolsFiles } from './types.js';
|
|
3
|
+
export interface ToolsFilm {
|
|
4
|
+
/** null: there is nothing to load, and `problems` says why first. */
|
|
5
|
+
film: StageFilm | null;
|
|
6
|
+
title: string;
|
|
7
|
+
problems: string[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The film the tools page loads, read from its files as the page reads its store. `retouched.json` stays among
|
|
11
|
+
* the modules: the stage lays it from there at load, as the page's own `retouch` message would.
|
|
12
|
+
*/
|
|
13
|
+
export declare function toolsFilm(files: ToolsFiles): ToolsFilm;
|