framesheet 0.1.0 → 0.2.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/README.md +37 -2
- package/dist/compose.d.ts +13 -0
- package/dist/compose.d.ts.map +1 -1
- package/dist/compose.js +35 -5
- package/dist/compose.js.map +1 -1
- package/dist/generated/assets.d.ts +4 -0
- package/dist/generated/assets.d.ts.map +1 -1
- package/dist/generated/assets.js +4 -0
- package/dist/generated/assets.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/inspect.d.ts +14 -1
- package/dist/inspect.d.ts.map +1 -1
- package/dist/inspect.js +25 -2
- package/dist/inspect.js.map +1 -1
- package/dist/marks.d.ts +49 -0
- package/dist/marks.d.ts.map +1 -0
- package/dist/marks.js +460 -0
- package/dist/marks.js.map +1 -0
- package/dist/marks.schema.json +307 -0
- package/dist/media.d.ts +22 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +306 -0
- package/dist/media.js.map +1 -0
- package/dist/models.d.ts +93 -2
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js.map +1 -1
- package/dist/pixels.d.ts +6 -0
- package/dist/pixels.d.ts.map +1 -1
- package/dist/pixels.js +38 -0
- package/dist/pixels.js.map +1 -1
- package/dist/report.schema.json +576 -4
- package/dist/sources.d.ts +11 -0
- package/dist/sources.d.ts.map +1 -1
- package/dist/sources.js +42 -1
- package/dist/sources.js.map +1 -1
- package/dist/video.d.ts +65 -0
- package/dist/video.d.ts.map +1 -0
- package/dist/video.js +440 -0
- package/dist/video.js.map +1 -0
- package/package.json +2 -1
- package/src/compose.ts +40 -6
- package/src/generated/assets.ts +6 -0
- package/src/index.ts +11 -1
- package/src/inspect.ts +34 -2
- package/src/marks.ts +481 -0
- package/src/media.ts +296 -0
- package/src/models.ts +97 -2
- package/src/pixels.ts +36 -0
- package/src/sources.ts +45 -1
- package/src/video.ts +513 -0
package/src/media.ts
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Container probing for video inputs (no ffmpeg): format, codec, size, frame rate
|
|
3
|
+
* (port of media.py). Best effort: unknown fields are `null`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { pyRound } from "./pyfmt.js";
|
|
7
|
+
|
|
8
|
+
export const VIDEO_EXTENSIONS: Record<string, string> = {
|
|
9
|
+
".mp4": "mp4",
|
|
10
|
+
".m4v": "mp4",
|
|
11
|
+
".mov": "mov",
|
|
12
|
+
".webm": "webm",
|
|
13
|
+
".mkv": "mkv",
|
|
14
|
+
".gif": "gif",
|
|
15
|
+
".webp": "webp",
|
|
16
|
+
};
|
|
17
|
+
export const MIME_TYPES: Record<string, string> = {
|
|
18
|
+
mp4: "video/mp4",
|
|
19
|
+
mov: "video/mp4",
|
|
20
|
+
webm: "video/webm",
|
|
21
|
+
mkv: "video/webm",
|
|
22
|
+
gif: "image/gif",
|
|
23
|
+
webp: "image/webp",
|
|
24
|
+
};
|
|
25
|
+
const MP4_CODECS: Record<string, string> = {
|
|
26
|
+
avc1: "h264",
|
|
27
|
+
avc3: "h264",
|
|
28
|
+
hvc1: "hevc",
|
|
29
|
+
hev1: "hevc",
|
|
30
|
+
av01: "av1",
|
|
31
|
+
vp09: "vp9",
|
|
32
|
+
vp08: "vp8",
|
|
33
|
+
mp4v: "mpeg4",
|
|
34
|
+
};
|
|
35
|
+
const MKV_CODECS: Record<string, string> = {
|
|
36
|
+
V_VP8: "vp8",
|
|
37
|
+
V_VP9: "vp9",
|
|
38
|
+
V_AV1: "av1",
|
|
39
|
+
"V_MPEG4/ISO/AVC": "h264",
|
|
40
|
+
"V_MPEGH/ISO/HEVC": "hevc",
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** What the container says about the (first) video track. */
|
|
44
|
+
export interface Probe {
|
|
45
|
+
container: string;
|
|
46
|
+
codec: string | null;
|
|
47
|
+
width: number | null;
|
|
48
|
+
height: number | null;
|
|
49
|
+
fps: number | null;
|
|
50
|
+
frameCount: number | null;
|
|
51
|
+
duration: number | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const isAnimatedImage = (p: Probe) => p.container === "gif" || p.container === "webp";
|
|
55
|
+
|
|
56
|
+
const ascii = (data: Uint8Array, s: number, e: number) => String.fromCharCode(...data.subarray(s, e));
|
|
57
|
+
|
|
58
|
+
/** Container from magic bytes (`null` if not a supported video/animated image). */
|
|
59
|
+
export function sniff(data: Uint8Array): string | null {
|
|
60
|
+
if (ascii(data, 4, 8) === "ftyp") return ascii(data, 8, 12) === "qt " ? "mov" : "mp4";
|
|
61
|
+
if (data[0] === 0x1a && data[1] === 0x45 && data[2] === 0xdf && data[3] === 0xa3) {
|
|
62
|
+
return ascii(data, 0, 64).includes("webm") ? "webm" : "mkv";
|
|
63
|
+
}
|
|
64
|
+
const head = ascii(data, 0, 6);
|
|
65
|
+
if (head === "GIF87a" || head === "GIF89a") return "gif";
|
|
66
|
+
if (ascii(data, 0, 4) === "RIFF" && ascii(data, 8, 12) === "WEBP") return "webp";
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Parse the container header (`container` is used if the magic bytes are unknown). */
|
|
71
|
+
export function probe(data: Uint8Array, container: string | null = null): Probe {
|
|
72
|
+
const kind = sniff(data) ?? container;
|
|
73
|
+
if (kind === null) throw new Error("Not a supported video or animated image (mp4, mov, webm, mkv, gif, webp)");
|
|
74
|
+
const out: Probe = { container: kind, codec: null, width: null, height: null, fps: null, frameCount: null, duration: null };
|
|
75
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
76
|
+
try {
|
|
77
|
+
if (kind === "mp4" || kind === "mov") mp4(data, view, out);
|
|
78
|
+
else if (kind === "webm" || kind === "mkv") matroska(data, view, out);
|
|
79
|
+
else if (kind === "gif") {
|
|
80
|
+
out.codec = "gif";
|
|
81
|
+
out.width = view.getUint16(6, true);
|
|
82
|
+
out.height = view.getUint16(8, true);
|
|
83
|
+
} else if (kind === "webp") {
|
|
84
|
+
out.codec = "webp";
|
|
85
|
+
webp(data, view, out);
|
|
86
|
+
}
|
|
87
|
+
} catch {
|
|
88
|
+
// Truncated or unusual header: keep what was found.
|
|
89
|
+
}
|
|
90
|
+
if (out.fps !== null) out.fps = pyRound(out.fps, 3);
|
|
91
|
+
if (out.duration !== null) out.duration = pyRound(out.duration, 4);
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ISO BMFF -------------------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
const MP4_CONTAINERS = new Set(["moov", "trak", "mdia", "minf", "stbl"]);
|
|
98
|
+
|
|
99
|
+
function* boxes(data: Uint8Array, view: DataView, start: number, end: number): Generator<[string, number, number]> {
|
|
100
|
+
let pos = start;
|
|
101
|
+
while (pos + 8 <= end) {
|
|
102
|
+
let size = view.getUint32(pos);
|
|
103
|
+
const kind = ascii(data, pos + 4, pos + 8);
|
|
104
|
+
let header = 8;
|
|
105
|
+
if (size === 1) {
|
|
106
|
+
size = Number(view.getBigUint64(pos + 8));
|
|
107
|
+
header = 16;
|
|
108
|
+
} else if (size === 0) {
|
|
109
|
+
size = end - pos;
|
|
110
|
+
}
|
|
111
|
+
if (size < header) return;
|
|
112
|
+
yield [kind, pos + header, Math.min(pos + size, end)];
|
|
113
|
+
pos += size;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function mp4(data: Uint8Array, view: DataView, out: Probe): void {
|
|
118
|
+
for (const [kind, start, end] of boxes(data, view, 0, data.length)) {
|
|
119
|
+
if (kind !== "moov") continue;
|
|
120
|
+
for (const [tkind, ts, te] of boxes(data, view, start, end)) {
|
|
121
|
+
if (tkind === "trak" && mp4Track(data, view, ts, te, out)) return;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function mp4Track(data: Uint8Array, view: DataView, start: number, end: number, out: Probe): boolean {
|
|
127
|
+
const found = new Map<string, [number, number]>();
|
|
128
|
+
const walk = (s: number, e: number) => {
|
|
129
|
+
for (const [kind, cs, ce] of boxes(data, view, s, e)) {
|
|
130
|
+
if (MP4_CONTAINERS.has(kind)) walk(cs, ce);
|
|
131
|
+
else if (!found.has(kind)) found.set(kind, [cs, ce]);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
walk(start, end);
|
|
135
|
+
const hdlr = found.get("hdlr");
|
|
136
|
+
if (!hdlr || ascii(data, hdlr[0] + 8, hdlr[0] + 12) !== "vide") return false;
|
|
137
|
+
let timescale = 0;
|
|
138
|
+
const mdhd = found.get("mdhd");
|
|
139
|
+
if (mdhd) {
|
|
140
|
+
const s = mdhd[0];
|
|
141
|
+
let duration: number;
|
|
142
|
+
if (data[s] === 1) {
|
|
143
|
+
timescale = view.getUint32(s + 20);
|
|
144
|
+
duration = Number(view.getBigUint64(s + 24));
|
|
145
|
+
} else {
|
|
146
|
+
timescale = view.getUint32(s + 12);
|
|
147
|
+
duration = view.getUint32(s + 16);
|
|
148
|
+
}
|
|
149
|
+
if (timescale) out.duration = duration / timescale;
|
|
150
|
+
}
|
|
151
|
+
const stsd = found.get("stsd");
|
|
152
|
+
if (stsd) {
|
|
153
|
+
const entry = stsd[0] + 8;
|
|
154
|
+
const fourcc = ascii(data, entry + 4, entry + 8);
|
|
155
|
+
out.codec = MP4_CODECS[fourcc] ?? (fourcc.trim() || null);
|
|
156
|
+
out.width = view.getUint16(entry + 32);
|
|
157
|
+
out.height = view.getUint16(entry + 34);
|
|
158
|
+
}
|
|
159
|
+
const stts = found.get("stts");
|
|
160
|
+
if (stts && timescale) {
|
|
161
|
+
const count = view.getUint32(stts[0] + 4);
|
|
162
|
+
let frames = 0;
|
|
163
|
+
let total = 0;
|
|
164
|
+
for (let i = 0; i < count; i++) {
|
|
165
|
+
const n = view.getUint32(stts[0] + 8 + 8 * i);
|
|
166
|
+
const delta = view.getUint32(stts[0] + 12 + 8 * i);
|
|
167
|
+
frames += n;
|
|
168
|
+
total += n * delta;
|
|
169
|
+
}
|
|
170
|
+
if (frames) out.frameCount = frames;
|
|
171
|
+
if (total) out.fps = (frames * timescale) / total;
|
|
172
|
+
}
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Matroska / WebM ------------------------------------------------------------------------
|
|
177
|
+
|
|
178
|
+
const MKV_ENTER = new Set([0x18538067, 0x1549a966, 0x1654ae6b, 0xe0, 0x1f43b675, 0xa0]);
|
|
179
|
+
const TRACK_ENTRY = 0xae;
|
|
180
|
+
const TIMECODE_SCALE = 0x2ad7b1;
|
|
181
|
+
const DURATION = 0x4489;
|
|
182
|
+
const CODEC_ID = 0x86;
|
|
183
|
+
const TRACK_UINTS: Record<number, string> = {
|
|
184
|
+
0xd7: "number",
|
|
185
|
+
0x83: "type",
|
|
186
|
+
0x23e383: "default_duration",
|
|
187
|
+
0xb0: "width",
|
|
188
|
+
0xba: "height",
|
|
189
|
+
};
|
|
190
|
+
const BLOCKS = new Set([0xa3, 0xa1]);
|
|
191
|
+
|
|
192
|
+
/** EBML variable-length integer -> [value, length, all-ones = unknown size]. */
|
|
193
|
+
function vint(data: Uint8Array, pos: number, strip: boolean): [number, number, boolean] {
|
|
194
|
+
const first = data[pos];
|
|
195
|
+
if (first === undefined) throw new Error("truncated");
|
|
196
|
+
let length = 1;
|
|
197
|
+
let mask = 0x80;
|
|
198
|
+
while (length <= 8 && !(first & mask)) {
|
|
199
|
+
mask >>= 1;
|
|
200
|
+
length++;
|
|
201
|
+
}
|
|
202
|
+
if (length > 8) throw new Error("bad EBML vint");
|
|
203
|
+
let value = strip ? first & (mask - 1) : first;
|
|
204
|
+
for (let i = 1; i < length; i++) {
|
|
205
|
+
const b = data[pos + i];
|
|
206
|
+
if (b === undefined) throw new Error("truncated");
|
|
207
|
+
value = value * 256 + b;
|
|
208
|
+
}
|
|
209
|
+
const unknown = strip && value === 2 ** (7 * length) - 1;
|
|
210
|
+
return [value, length, unknown];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function uint(data: Uint8Array, s: number, e: number): number {
|
|
214
|
+
let v = 0;
|
|
215
|
+
for (let i = s; i < e; i++) v = v * 256 + data[i];
|
|
216
|
+
return v;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function matroska(data: Uint8Array, view: DataView, out: Probe): void {
|
|
220
|
+
const tracks: Record<string, number | string>[] = [];
|
|
221
|
+
const blocks = new Map<number, number>();
|
|
222
|
+
let timescale = 1_000_000;
|
|
223
|
+
let duration: number | null = null;
|
|
224
|
+
const [headerSize, headerLen] = vint(data, 4, true);
|
|
225
|
+
let pos = 4 + headerLen + headerSize;
|
|
226
|
+
const end = data.length;
|
|
227
|
+
while (pos < end) {
|
|
228
|
+
const [eid, ilen] = vint(data, pos, false);
|
|
229
|
+
const [size, slen, unknown] = vint(data, pos + ilen, true);
|
|
230
|
+
const body = pos + ilen + slen;
|
|
231
|
+
if (MKV_ENTER.has(eid)) {
|
|
232
|
+
pos = body;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (eid === TRACK_ENTRY) {
|
|
236
|
+
tracks.push({});
|
|
237
|
+
pos = body;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
if (unknown) break;
|
|
241
|
+
const stop = Math.min(body + size, end);
|
|
242
|
+
const track = tracks.length ? tracks[tracks.length - 1] : {};
|
|
243
|
+
if (eid === TIMECODE_SCALE) timescale = uint(data, body, stop);
|
|
244
|
+
else if (eid === DURATION) duration = size === 4 ? view.getFloat32(body) : view.getFloat64(body);
|
|
245
|
+
else if (eid === CODEC_ID) track.codec = ascii(data, body, stop).replace(/\0+$/, "");
|
|
246
|
+
else if (eid in TRACK_UINTS) track[TRACK_UINTS[eid]] = uint(data, body, stop);
|
|
247
|
+
else if (BLOCKS.has(eid)) {
|
|
248
|
+
const number = vint(data, body, true)[0];
|
|
249
|
+
blocks.set(number, (blocks.get(number) ?? 0) + 1);
|
|
250
|
+
}
|
|
251
|
+
pos = stop;
|
|
252
|
+
}
|
|
253
|
+
if (duration !== null) out.duration = (duration * timescale) / 1e9;
|
|
254
|
+
const video = tracks.find((t) => t.type === 1);
|
|
255
|
+
if (!video) return;
|
|
256
|
+
const codec = String(video.codec ?? "");
|
|
257
|
+
out.codec = MKV_CODECS[codec] ?? (codec.replace(/^V_/, "").toLowerCase() || null);
|
|
258
|
+
out.width = typeof video.width === "number" ? video.width : null;
|
|
259
|
+
out.height = typeof video.height === "number" ? video.height : null;
|
|
260
|
+
const count = typeof video.number === "number" ? blocks.get(video.number) : undefined;
|
|
261
|
+
out.frameCount = count || null;
|
|
262
|
+
const dd = video.default_duration;
|
|
263
|
+
if (typeof dd === "number" && dd > 0) out.fps = 1e9 / dd;
|
|
264
|
+
else if (count && out.duration) out.fps = count / out.duration;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// WebP -----------------------------------------------------------------------------------
|
|
268
|
+
|
|
269
|
+
function webp(data: Uint8Array, view: DataView, out: Probe): void {
|
|
270
|
+
let pos = 12;
|
|
271
|
+
let frames = 0;
|
|
272
|
+
let totalMs = 0;
|
|
273
|
+
const u24 = (p: number) => data[p] | (data[p + 1] << 8) | (data[p + 2] << 16);
|
|
274
|
+
while (pos + 8 <= data.length) {
|
|
275
|
+
const kind = ascii(data, pos, pos + 4);
|
|
276
|
+
const size = view.getUint32(pos + 4, true);
|
|
277
|
+
const body = pos + 8;
|
|
278
|
+
if (kind === "VP8X") {
|
|
279
|
+
out.width = 1 + u24(body + 4);
|
|
280
|
+
out.height = 1 + u24(body + 7);
|
|
281
|
+
} else if (kind === "ANMF") {
|
|
282
|
+
frames++;
|
|
283
|
+
totalMs += u24(body + 12);
|
|
284
|
+
} else if (kind === "VP8 " && out.width === null) {
|
|
285
|
+
out.width = view.getUint16(body + 6, true) & 0x3fff;
|
|
286
|
+
out.height = view.getUint16(body + 8, true) & 0x3fff;
|
|
287
|
+
} else if (kind === "VP8L" && out.width === null) {
|
|
288
|
+
const bits = view.getUint32(body + 1, true);
|
|
289
|
+
out.width = (bits & 0x3fff) + 1;
|
|
290
|
+
out.height = ((bits >>> 14) & 0x3fff) + 1;
|
|
291
|
+
}
|
|
292
|
+
pos = body + size + (size & 1);
|
|
293
|
+
}
|
|
294
|
+
out.frameCount = frames || 1;
|
|
295
|
+
if (totalMs) out.duration = totalMs / 1000;
|
|
296
|
+
}
|
package/src/models.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
export type Severity = "error" | "warning" | "info";
|
|
8
|
-
export type SourceKind = "svg" | "html";
|
|
8
|
+
export type SourceKind = "svg" | "html" | "video";
|
|
9
9
|
export type ColorScheme = "light" | "dark";
|
|
10
10
|
/** An axis-aligned box as `[x, y, width, height]`. */
|
|
11
11
|
export type Rect = [number, number, number, number];
|
|
@@ -47,7 +47,7 @@ export interface AnimationTiming {
|
|
|
47
47
|
|
|
48
48
|
export interface DurationInfo {
|
|
49
49
|
seconds: number;
|
|
50
|
-
method: "override" | "detected" | "fallback";
|
|
50
|
+
method: "override" | "detected" | "fallback" | "media";
|
|
51
51
|
/** True if at least one animation repeats forever (or motion is JS-driven). */
|
|
52
52
|
looping: boolean;
|
|
53
53
|
detail: string;
|
|
@@ -111,6 +111,101 @@ export interface Report {
|
|
|
111
111
|
elements: ElementTrack[];
|
|
112
112
|
loop: LoopInfo;
|
|
113
113
|
console_errors: string[];
|
|
114
|
+
/** Media facts and per-tile metrics (video inputs only). */
|
|
115
|
+
video: VideoInfo | null;
|
|
116
|
+
/** The marks drawn onto the sheet, as validated. */
|
|
117
|
+
marks: Marks | null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** An instant at a position, e.g. a click: drawn on the tile nearest to `t`. */
|
|
121
|
+
export interface MarkPoint {
|
|
122
|
+
t: number;
|
|
123
|
+
x: number;
|
|
124
|
+
y: number;
|
|
125
|
+
label: string | null;
|
|
126
|
+
/** Free-form, e.g. `click`. */
|
|
127
|
+
kind: string | null;
|
|
128
|
+
/** Seconds the point stays relevant (drawn on every tile it overlaps). Default 0. */
|
|
129
|
+
duration: number;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** A time range, optionally with a box: a target element, a zoom, a highlight. */
|
|
133
|
+
export interface MarkRegion {
|
|
134
|
+
x: number | null;
|
|
135
|
+
y: number | null;
|
|
136
|
+
w: number | null;
|
|
137
|
+
h: number | null;
|
|
138
|
+
start: number;
|
|
139
|
+
end: number;
|
|
140
|
+
label: string | null;
|
|
141
|
+
/** Free-form, e.g. `zoom`. `target` boxes are checked against span boxes. */
|
|
142
|
+
kind: string | null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A labelled time range, e.g. a caption; the optional box is its screen area. */
|
|
146
|
+
export interface MarkSpan {
|
|
147
|
+
x: number | null;
|
|
148
|
+
y: number | null;
|
|
149
|
+
w: number | null;
|
|
150
|
+
h: number | null;
|
|
151
|
+
start: number;
|
|
152
|
+
end: number;
|
|
153
|
+
label: string;
|
|
154
|
+
kind: string | null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Annotations drawn onto video tiles and the timeline (schema: framesheet/schema/marks.schema.json).
|
|
159
|
+
* Times are seconds of the media; coordinates are fractions (0-1) of the video frame, or
|
|
160
|
+
* pixels of `frame` if given.
|
|
161
|
+
*/
|
|
162
|
+
export interface Marks {
|
|
163
|
+
frame: { width: number; height: number } | null;
|
|
164
|
+
points: MarkPoint[];
|
|
165
|
+
regions: MarkRegion[];
|
|
166
|
+
spans: MarkSpan[];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** One dense video sample: share of pixels changed since the previous sample. */
|
|
170
|
+
export interface ActivitySample {
|
|
171
|
+
t: number;
|
|
172
|
+
changed_ratio: number;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Per-tile numbers for videos, so an agent can score without looking. */
|
|
176
|
+
export interface VideoTile {
|
|
177
|
+
index: number;
|
|
178
|
+
t: number;
|
|
179
|
+
/** Presentation time of the decoded frame (`null` if the browser did not say). */
|
|
180
|
+
media_time: number | null;
|
|
181
|
+
black: boolean;
|
|
182
|
+
/** One flat colour that is not black (e.g. a white page before load). */
|
|
183
|
+
blank: boolean;
|
|
184
|
+
/** Near-identical to the previous tile (changes < 0.05% of pixels). */
|
|
185
|
+
duplicate: boolean;
|
|
186
|
+
/** Mean luma gradient (0-510); compare tiles of one video: drops = blur/upscaling. */
|
|
187
|
+
sharpness: number;
|
|
188
|
+
/** Labels of the marks active on this tile. */
|
|
189
|
+
marks: string[];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Media facts and dense-sample metrics of a video or animated image. */
|
|
193
|
+
export interface VideoInfo {
|
|
194
|
+
container: string;
|
|
195
|
+
codec: string | null;
|
|
196
|
+
decoder: "video" | "image-decoder";
|
|
197
|
+
width: number;
|
|
198
|
+
height: number;
|
|
199
|
+
media_duration: number | null;
|
|
200
|
+
fps: number | null;
|
|
201
|
+
frame_count: number | null;
|
|
202
|
+
start: number;
|
|
203
|
+
end: number;
|
|
204
|
+
/** Seconds without visible change that count as a static stretch. */
|
|
205
|
+
static_threshold: number;
|
|
206
|
+
/** Every sampled time in `[start, end]` (tiles + dense grid), in order. */
|
|
207
|
+
samples: ActivitySample[];
|
|
208
|
+
tiles: VideoTile[];
|
|
114
209
|
}
|
|
115
210
|
|
|
116
211
|
/** PNG bytes of the three images (plus every sampled frame if requested). */
|
package/src/pixels.ts
CHANGED
|
@@ -246,3 +246,39 @@ function outline(
|
|
|
246
246
|
}
|
|
247
247
|
}
|
|
248
248
|
}
|
|
249
|
+
|
|
250
|
+
/** A frame is black/blank if this share of pixels has one colour (4 bits per channel). */
|
|
251
|
+
export const UNIFORM_SHARE = 0.995;
|
|
252
|
+
|
|
253
|
+
/** `[black, blank]`: one colour (4 bits/channel) covers >= 99.5% of the pixels (= video.py). */
|
|
254
|
+
export function frameStats(frame: Frame): [boolean, boolean] {
|
|
255
|
+
const counts = new Uint32Array(4096);
|
|
256
|
+
const d = frame.data;
|
|
257
|
+
const n = frame.width * frame.height;
|
|
258
|
+
for (let i = 0, p = 0; i < n; i++, p += 4) counts[((d[p] >> 4) << 8) | ((d[p + 1] >> 4) << 4) | (d[p + 2] >> 4)]++;
|
|
259
|
+
// Python: max((count, (r, g, b))) -> ties go to the larger colour.
|
|
260
|
+
let best = 0;
|
|
261
|
+
for (let b = 1; b < 4096; b++) if (counts[b] >= counts[best]) best = b;
|
|
262
|
+
const uniform = counts[best] / n >= UNIFORM_SHARE;
|
|
263
|
+
const black = uniform && Math.max(best >> 8, (best >> 4) & 15, best & 15) <= 1;
|
|
264
|
+
return [black, uniform && !black];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Mean absolute luma gradient (`|dx| + |dy|` per pixel, 0-510), Pillow "L" luma. */
|
|
268
|
+
export function sharpness(frame: Frame): number {
|
|
269
|
+
const { width: w, height: h, data: d } = frame;
|
|
270
|
+
const luma = new Uint8Array(w * h);
|
|
271
|
+
for (let i = 0, p = 0; i < luma.length; i++, p += 4) {
|
|
272
|
+
luma[i] = (d[p] * 19595 + d[p + 1] * 38470 + d[p + 2] * 7471 + 0x8000) >> 16;
|
|
273
|
+
}
|
|
274
|
+
let total = 0;
|
|
275
|
+
for (let y = 0; y < h; y++) {
|
|
276
|
+
const row = y * w;
|
|
277
|
+
for (let x = 0; x < w; x++) {
|
|
278
|
+
const v = luma[row + x];
|
|
279
|
+
if (x + 1 < w) total += Math.abs(luma[row + x + 1] - v);
|
|
280
|
+
if (y + 1 < h) total += Math.abs(luma[row + w + x] - v);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return pyRound(total / (w * h), 3);
|
|
284
|
+
}
|
package/src/sources.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/** Input handling: files, markup strings and URLs (port of sources.py). */
|
|
2
2
|
|
|
3
|
-
import { readFileSync, statSync } from "node:fs";
|
|
3
|
+
import { closeSync, openSync, readFileSync, readSync, statSync } from "node:fs";
|
|
4
4
|
import { extname, resolve as resolvePath } from "node:path";
|
|
5
5
|
import { pathToFileURL } from "node:url";
|
|
6
6
|
import type { ParsedDocument } from "./checks.js";
|
|
7
|
+
import { sniff, VIDEO_EXTENSIONS } from "./media.js";
|
|
7
8
|
import type { SourceKind } from "./models.js";
|
|
8
9
|
import { pyRound } from "./pyfmt.js";
|
|
9
10
|
|
|
@@ -24,6 +25,38 @@ export interface Source {
|
|
|
24
25
|
text: string | null;
|
|
25
26
|
/** Markup served via page.route at `url` (markup-string inputs only). */
|
|
26
27
|
body?: string;
|
|
28
|
+
/** Video/animated image bytes (`kind === "video"`), served to the wrapper page. */
|
|
29
|
+
media?: Buffer;
|
|
30
|
+
/** Video container (`mp4`, `webm`, `gif`, ...) for video sources. */
|
|
31
|
+
container?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Video wrapper pages and media are served from this virtual (secure) origin. */
|
|
35
|
+
export const VIDEO_ORIGIN = "https://framesheet.invalid";
|
|
36
|
+
export const VIDEO_PAGE =
|
|
37
|
+
"<!DOCTYPE html><html><head><meta charset='utf-8'><title>framesheet video</title></head><body></body></html>";
|
|
38
|
+
|
|
39
|
+
/** A video/animated image, played in a wrapper page on {@link VIDEO_ORIGIN}. */
|
|
40
|
+
export function videoSource(label: string, data: Buffer, container: string | null = null): Source {
|
|
41
|
+
const kind = sniff(data) ?? container;
|
|
42
|
+
if (kind === null) throw new Error(`Not a supported video or animated image: ${label}`);
|
|
43
|
+
return { label, kind: "video", url: `${VIDEO_ORIGIN}/video.html`, text: null, media: data, container: kind };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function videoExt(name: string): string | null {
|
|
47
|
+
return VIDEO_EXTENSIONS[extname(name.split("?")[0]).toLowerCase()] ?? null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Download a video URL (errors are raised: there is nothing to inspect without it). */
|
|
51
|
+
export async function fetchBytes(url: string, timeoutMs = 60_000): Promise<Buffer> {
|
|
52
|
+
let response: Response;
|
|
53
|
+
try {
|
|
54
|
+
response = await fetch(url, { headers: { "User-Agent": "framesheet" }, signal: AbortSignal.timeout(timeoutMs) });
|
|
55
|
+
} catch (err) {
|
|
56
|
+
throw new Error(`Cannot download ${url}: ${(err as Error).message}`);
|
|
57
|
+
}
|
|
58
|
+
if (!response.ok) throw new Error(`Cannot download ${url}: HTTP ${response.status}`);
|
|
59
|
+
return Buffer.from(await response.arrayBuffer());
|
|
27
60
|
}
|
|
28
61
|
|
|
29
62
|
export function looksLikeSvg(text: string): boolean {
|
|
@@ -64,6 +97,8 @@ export async function fetchText(url: string, timeoutMs = 15_000): Promise<string
|
|
|
64
97
|
export async function resolveSource(source: string): Promise<Source> {
|
|
65
98
|
if (source.trimStart().startsWith("<")) return snippet(source);
|
|
66
99
|
if (/^https?:\/\//.test(source)) {
|
|
100
|
+
const container = videoExt(source);
|
|
101
|
+
if (container) return videoSource(source, await fetchBytes(source), container);
|
|
67
102
|
const text = await fetchText(source);
|
|
68
103
|
const byName = source.split("?")[0].toLowerCase().endsWith(".svg");
|
|
69
104
|
const kind: SourceKind = byName || (text !== null && looksLikeSvg(text)) ? "svg" : "html";
|
|
@@ -77,6 +112,15 @@ export async function resolveSource(source: string): Promise<Source> {
|
|
|
77
112
|
isFile = false;
|
|
78
113
|
}
|
|
79
114
|
if (!isFile) throw new Error(`Input not found: ${source}`);
|
|
115
|
+
const head = Buffer.alloc(64);
|
|
116
|
+
const fd = openSync(path, "r");
|
|
117
|
+
try {
|
|
118
|
+
readSync(fd, head, 0, 64, 0);
|
|
119
|
+
} finally {
|
|
120
|
+
closeSync(fd);
|
|
121
|
+
}
|
|
122
|
+
const container = videoExt(path);
|
|
123
|
+
if (container || sniff(head)) return videoSource(source, readFileSync(path), container);
|
|
80
124
|
const text = readFileSync(path, "utf8");
|
|
81
125
|
const ext = extname(path).toLowerCase();
|
|
82
126
|
let kind: SourceKind;
|