diff --git a/sdk/goal/src/packs/reading.ts b/sdk/goal/src/packs/reading.ts index 7a28c584..66f32946 100644 --- a/sdk/goal/src/packs/reading.ts +++ b/sdk/goal/src/packs/reading.ts @@ -1,7 +1,7 @@ // The reading pack (11-30, Shane: "the words on screen relative to duration ... if a wordy CTA or component or scene is // meant to be fully read or if it just adds glossable content or context, and if it's on screen long enough to be -// read"). From the scene reader's text (the farm's texts, two frames a second), every block of text that isn't a -// caption (captions are read while they're heard: the captions pack holds them) is timed against the words it holds. +// read"). From the render's own texts when its manifest is here (exactly what it drew, five times a second, no farm +// call), else the scene reader's (the farm's texts, two frames a second), every block of text that isn't a caption (captions are read while they're heard: the captions pack holds them) is timed against the words it holds. // // time to read: READ_BASE s to find it, then WPS words a second (a feed viewer's skim of short ad text; subtitle // standards sit near 3 words a second). A block up for under its time is a warning; under SHORT of it an error, @@ -12,7 +12,7 @@ import { check, found } from "raea/check" import type { FileFacts } from "../facts.ts" import type { Word } from "../media.ts" -import { captionsOf, fit, type Frame, LEAST, letters, type Read, similar } from "./captions.ts" +import { captionsOf, fit, type Frame, framesOfManifest, LEAST, letters, type Read, similar } from "./captions.ts" export type Block = { text: string; words: number; from: number; to: number; size?: number } export type Finding = { level: "error" | "warn"; at: number; says: string; block: Block } @@ -104,16 +104,33 @@ export function reading(blocks: Block[]): Finding[] { return out } +/** a render manifest's texts as `framesOfManifest` steps them, each a line as tall as one of its lines: a text set on + * three lines has one ink box over all three, and its size (fine print or a headline) is a line's, not the box's */ +export function readingFrames(manifest: unknown): { frames: Frame[]; rate: number } | null { + const own = framesOfManifest(manifest) + const m = manifest as { fps?: number; frames?: { text?: { text?: string; ink?: number[]; box?: number[]; opacity?: number; lines?: number }[] }[] } | null + if (!own || !m?.frames) return null + const step = Math.max(1, Math.round((Number(m.fps) || 30) / own.rate)) + const frames = own.frames.map((fr, k) => { + const texts = (m.frames![k * step]?.text ?? []).filter((t) => { const b = t.ink ?? t.box; return !!b && b.length >= 4 && !!String(t.text ?? "").trim() && (t.opacity ?? 1) > 0.2 }) // framesOfManifest's own test, so each line is its text + return { h: fr.h, lines: fr.lines.map((l, i) => [l[0], l[1], l[2], l[3] / Math.max(1, Number(texts[i]?.lines) || 1), l[4], l[5]] as Read) } + }) + return { frames, rate: own.rate } +} + export default check( { id: "reading", text: "every block of text on screen that isn't a caption is up long enough to read (half a second, then 3 words a second), or is plainly texture", over: "file", at: ["render", "publish", "done"], needs: ["words", "scene"] }, async (f) => { if (!f.made || f.kind !== "video") return - const sc = await f.scene() - const frames: Frame[] = sc.scenes.map((s) => ({ h: s?.h ?? 1, lines: (s?.texts ?? []) as Read[] })) + // the render's own texts when its manifest is here (no farm call, and never the footage's own text), else the export + // read frame by frame; captions are found in the same frames the captions pack reads + const own = f.manifest ? readingFrames(f.manifest) : null + const sc = own ? null : await f.scene() + const frames: Frame[] = own ? own.frames : sc!.scenes.map((s) => ({ h: s?.h ?? 1, lines: (s?.texts ?? []) as Read[] })) if (!frames.some((x) => x.lines.length)) return - const rate = sc.rate || 2 + const rate = own ? own.rate : sc!.rate || 2 const heard = await f.words() - const caps = captionsOf(frames, rate, heard) + const caps = captionsOf(own ? framesOfManifest(f.manifest)!.frames : frames, rate, heard) const isCaption = (t: number, key: string) => caps.some((c) => t >= c.from - 0.01 && t < c.to && (similar(c.key, key) || c.key.includes(key))) for (const x of reading(blocksOf(frames, rate, isCaption, heard))) found({ level: x.level, says: x.says, seconds: x.at }) }, diff --git a/sdk/goal/test/reading.test.ts b/sdk/goal/test/reading.test.ts index 55e4513f..08670a2e 100644 --- a/sdk/goal/test/reading.test.ts +++ b/sdk/goal/test/reading.test.ts @@ -33,3 +33,24 @@ test("a frame full of lines (a shelf of labels) is left out, and words said as t const heard = "so good I want to eat it".split(" ").map((t, i) => ({ text: t, start: 0.2 + i * 0.3, end: 0.45 + i * 0.3 })) expect(blocksOf(frames(4, (t) => (t < 0.5 ? [line(300, "SO GOOD I WANT TO EAT IT")] : [])), 2, none, heard)).toEqual([]) }) + +test("a render's manifest is read as the texts it drew: a three-line card's size is a line's, and its time is exact", async () => { + const { readingFrames } = await import("../src/packs/reading.ts") + const card = { text: "How does your group do Secret Santa this year? Names in a hat or chaos", ink: [60, 300, 900, 288], opacity: 1, lines: 3 } + const frames = Array.from({ length: 60 }, (_, i) => ({ frame: i, text: [{ ...card, opacity: i < 30 ? 1 : 0.1 }] })) + const own = readingFrames({ fps: 30, height: 1920, frames })! + expect(own.rate).toBe(5) + expect(own.frames[0].lines[0][3]).toBe(96) + // up exactly a second (frames 0-29), gone once it fades below 0.2: 15 words in a second is an error + const blocks = blocksOf(own.frames, own.rate, none) + expect(blocks.length).toBe(1) + expect(blocks[0].to - blocks[0].from).toBeCloseTo(1, 5) + expect(reading(blocks)[0].level).toBe("error") + // fine print a line 24 px tall is texture: a warning that asks + const fine = { text: "Terms apply. See site for details on the offer and its dates", ink: [60, 1800, 600, 24], opacity: 1, lines: 1 } + const small = readingFrames({ fps: 30, height: 1920, frames: Array.from({ length: 60 }, (_, i) => ({ frame: i, text: i < 15 ? [fine] : [] })) })! + const f = reading(blocksOf(small.frames, small.rate, none)) + expect(f.length).toBe(1) + expect(f[0].level).toBe("warn") + expect(f[0].says).toContain("texture") +})