diff --git a/apps/skills/tests/author-verify.test.ts b/apps/skills/tests/author-verify.test.ts index 8e0d0160..e3baaced 100644 --- a/apps/skills/tests/author-verify.test.ts +++ b/apps/skills/tests/author-verify.test.ts @@ -139,7 +139,8 @@ describe.concurrent('every type file', () => { for (const path of paths(source.params)) if (!/^https?:/.test(path)) await writeFile(join(folder, path), sample(path)) const output = join(folder, 'example.h5p') const result = await buildPackage(source, output, { base: folder }) - expect(result.notes).toEqual([]) + // The demo's clip, every video's sample, has no sound, and the build says so (#218). + expect(result.notes.filter(({ message }) => !/\.mp4 has no sound$/.test(message))).toEqual([]) await checksAsSaved(output) await playsBothWays(output) }, 180_000) diff --git a/design/author.md b/design/author.md index c07f5bf2..a112979e 100644 --- a/design/author.md +++ b/design/author.md @@ -76,8 +76,20 @@ text to the content check's rules, and `apps/skills` plays every example untrust address downloaded at build time. It lands under `content/images|videos|audios|files/` with its MIME type, an image's size read from its header (PNG, JPEG, GIF, BMP; a JPEG's segments walked to its frame header, which a colour profile can push past 64 kB), and its copyright, on H5P's - own list of types (no SVG, no WebP). A download is given up after a minute with no byte, not - after a fixed time. A YouTube, Vimeo, Panopto or Echo360 address stays an address, as the editor + own list of types (no SVG, no WebP). A video's or a recording's container is read: an mp4 or an + m4a by its index, after `ftyp` or, as in an older file, `moov`, `mdat`, `free` or `wide`, any + other by its first bytes. One its field does + not take is refused (a video takes MP4, WebM and Ogg; a recording MP3, M4A, WAV, Ogg and WebM): + a web page at an address that ends `.mp4`, as a wiki's file page or a share link answers, built + and passed the verifier before (#218). An mp4's index is read through the normalizer's + `mp4Info`, the walk its faststart uses, in Node and on a page alike. No index to read is a + problem. Where the index lists tracks, no sound or no picture is a note, a silent video being + one an author can mean, and so is HEVC. Where it gives a length, that holds an Interactive + Video's times (`videoTimes`): a start, a bookmark or an end screen past the end is refused, an + interaction that ends past it is not, since it shows to the end. No time is named in a list + where an item was refused, since the list's indices no longer match the source's. A download + is given up after a minute with no byte, not after a fixed time. A YouTube, Vimeo, Panopto or + Echo360 address stays an address, as the editor embeds it, and only where H5P.Video's own pattern for that host takes it, since an address none matches goes to its HTML5 player, which cannot play a page: a Short or a live stream is written as its video's watch address, and another form on those hosts is refused by name. diff --git a/docs/compatibility.md b/docs/compatibility.md index 19587004..8691d092 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -121,8 +121,9 @@ with the specification is a conformance fix. ### The normalizer, `@missing-elements/h5p-normalize` The command's options, and `normalizeArchive()`, `formatBytes()` and `archiveFingerprint()`, with -their types, and `crc32()` from `@missing-elements/h5p-normalize/crc32`, which embed and h5p-author -take through a range. The content of a package it rewrites never changes. How it lays out the zip may change in a minor, as +their types, `crc32()` from `@missing-elements/h5p-normalize/crc32`, which embed and h5p-author take +through a range, and `mp4Info()` from `@missing-elements/h5p-normalize/mp4`, which h5p-author takes +the same way. The content of a package it rewrites never changes. How it lays out the zip may change in a minor, as long as the player streams the result at least as well. ### The verifier, `@missing-elements/h5p-verify` diff --git a/packages/author/README.md b/packages/author/README.md index 99a9f9ea..b62a17c7 100644 --- a/packages/author/README.md +++ b/packages/author/README.md @@ -64,8 +64,11 @@ What `params` takes is what H5P's editor would let an author save in the same fo - **Sub-content**: `{ "library": "H5P.MultiChoice", "params": { … }, "metadata": { "title": "…" } }`, filled in the same way, at the pack's version. - **Files**: `{ "path": "photo.jpg" }`, a file beside the source or an `http(s)` address, with an - optional `copyright`. Each is copied into the package with its type and, for a picture, its size; - the package is then laid out to stream, media stored and an mp4's index first. A YouTube, Vimeo, + optional `copyright`. Each is copied into the package with its type and, for a picture, its size. + A video or a recording is read from its first bytes, and one that is not what its field takes, a + web page at a file's address among them, is refused; an mp4's index says whether it has sound + and, where it gives one, its length, past which an Interactive Video's times are refused. The + package is then laid out to stream, media stored and an mp4's index first. A YouTube, Vimeo, Panopto or Echo360 address stays where it is, if it is one H5P.Video plays: a Short's is taken as its video's, and another address on those sites is refused. A download is given up once nothing has arrived for a minute, however long it runs. diff --git a/packages/author/lib/browser.mjs b/packages/author/lib/browser.mjs index c6594909..bf14f9a1 100644 --- a/packages/author/lib/browser.mjs +++ b/packages/author/lib/browser.mjs @@ -115,6 +115,7 @@ function pageMedia(files) { keep: (body) => new Response(body).blob(), async open(file) { return { + size: file.size, read: async (offset, length) => new Uint8Array(await file.slice(offset, offset + length).arrayBuffer()), close: async () => {} } diff --git a/packages/author/lib/build.mjs b/packages/author/lib/build.mjs index 2b91e102..07d7fbce 100644 --- a/packages/author/lib/build.mjs +++ b/packages/author/lib/build.mjs @@ -10,7 +10,7 @@ import { quizProblems, quizSource } from './quiz.mjs' import { outlineOf } from './outline.mjs' import { SourceError } from './source-error.mjs' import { escapeText } from './text.mjs' -import { LAYOUTS, placesOf } from './types.mjs' +import { LAYOUTS, placesOf, videoTimes } from './types.mjs' export { SourceError } @@ -310,7 +310,7 @@ export async function composePackage(source, pack, media, said = []) { // Media: copied, described, put in place of what was written. for (const { field, value, path } of generator.media) { - const resolved = await media.resolve(field, value, path, report) + const resolved = await media.resolve(field, value, path, report, note) for (const key of Object.keys(value)) delete value[key] Object.assign(value, resolved) } @@ -337,6 +337,12 @@ export async function composePackage(source, pack, media, said = []) { if (!known && !shortened(path)) problems.push({ path, message }) } for (const added of checked.notes) if (!notes.some(({ path, message }) => path === added.path && message === added.message)) notes.push(added) + // Times past the end of a video whose length was read, which only a build, with the file, knows; + // none in a list an item of which was refused, whose indices are no longer the source's. + const timed = (/** @type {string} */ path, /** @type {string} */ message) => { if (!shortened(path)) report(path, message) } + for (const place of placesOf(main, checked.params)) { + if (place.library === 'H5P.InteractiveVideo') videoTimes(place.params, inSource(place.path), (file) => media.lengthOf(file), timed) + } if (said.length || problems.length) throw new SourceError([...said, ...problems]) const content = checked.params diff --git a/packages/author/lib/media.mjs b/packages/author/lib/media.mjs index d4a737fa..95f3ee8d 100644 --- a/packages/author/lib/media.mjs +++ b/packages/author/lib/media.mjs @@ -1,3 +1,4 @@ +import { mp4Info } from '@missing-elements/h5p-normalize/mp4' import { escapeText } from './text.mjs' /** @@ -29,6 +30,15 @@ const TYPES = { /** The folder under `content/` H5P's editor keeps each kind in (h5peditor-file.class.php). */ const FOLDERS = { image: 'images', video: 'videos', audio: 'audios', file: 'files' } +/** The containers a video and a recording come in, among H5P's types, and how to name them. */ +const PLAYS = { + video: { takes: ['mp4', 'webm', 'ogg'], what: 'an MP4, WebM or Ogg video' }, + audio: { takes: ['mp3', 'mp4', 'wav', 'ogg', 'webm'], what: 'an MP3, M4A, WAV, Ogg or WebM recording' } +} + +/** Codecs of an mp4's picture that not every browser plays, by the name its index gives them. */ +const NOT_EVERYWHERE = /** @type {Record} */ ({ hvc1: 'HEVC (H.265)', hev1: 'HEVC (H.265)' }) + /** Whether a host name is one of these domains or under one. @param {string} host @param {string[]} domains */ const under = (host, domains) => domains.some((domain) => host === domain || host.endsWith(`.${domain}`)) @@ -100,11 +110,11 @@ const IDLE_MS = 60_000 * name(source: string): string, * local(source: string): Promise<{ file: F } | { missing: string }>, * keep(body: ReadableStream, extension: string): Promise, - * open(file: F): Promise<{ read(offset: number, length: number): Promise, close(): Promise }> + * open(file: F): Promise<{ size: number, read(offset: number, length: number): Promise, close(): Promise }> * }} MediaAccess * `what`, what a source's path names, for problems; `name`, the file's own name it names; * `local`, that file, or why there is none; `keep`, a download's bytes kept; `open`, a kept - * file's bytes read from an offset, fewer at its end + * file's length, and its bytes read from an offset, fewer at its end */ /** @@ -176,6 +186,35 @@ export async function imageSize(read) { return null } +/** + * Whether bytes start with an MPEG audio frame's header a decoder takes: the sync bits, then no + * reserved version, layer, bit rate or sample rate. + * @param {Uint8Array} head + */ +const mpegFrame = (head) => head.length >= 4 && head[0] === 0xff && (head[1] & 0xe0) === 0xe0 && (head[1] & 0x18) !== 0x08 && + (head[1] & 0x06) !== 0 && (head[2] & 0xf0) !== 0xf0 && (head[2] & 0x0c) !== 0x0c + +/** + * What a video's or a recording's first bytes say it is: its container, a web page, or none known. + * An mp4 or an m4a starts with `ftyp`, WebM with EBML's mark, Ogg with `OggS`, WAV with RIFF's + * `WAVE`, MP3 with an ID3 tag or a frame header; a page with `<`, in UTF-8 or, after its + * byte-order mark, in UTF-16. + * @param {Uint8Array} head the file's first 16 bytes, or all of a shorter one + * @returns {'mp4' | 'webm' | 'ogg' | 'wav' | 'mp3' | 'page' | undefined} + */ +function containerOf(head) { + const ascii = (/** @type {number} */ offset, /** @type {number} */ length) => String.fromCharCode(...head.subarray(offset, offset + length)) + if (ascii(4, 4) === 'ftyp') return 'mp4' + if (head[0] === 0x1a && head[1] === 0x45 && head[2] === 0xdf && head[3] === 0xa3) return 'webm' + if (ascii(0, 4) === 'OggS') return 'ogg' + if (ascii(0, 4) === 'RIFF' && ascii(8, 4) === 'WAVE') return 'wav' + if (ascii(0, 3) === 'ID3' || mpegFrame(head)) return 'mp3' + const text = ascii(0, head.length) + // UTF-16 read as its characters by leaving out the zero bytes, which only it has here. + const page = /^(?:\xff\xfe|\xfe\xff)/.test(text) ? text.slice(2).replaceAll('\0', '') : text.replace(/^\xef\xbb\xbf/, '') + return /^\s*} */ this.names = new Set() + /** @type {Map} a video's or a recording's length in seconds, by its path in the package, where its index gave one */ + this.lengths = new Map() + } + + /** + * A video's or a recording's length in seconds, by its path in the package, where it was read. + * @param {string} path + */ + lengthOf(path) { + return this.lengths.get(path) } /** * A written file made what H5P stores: `{ path, mime, width?, height?, copyright }`, its bytes - * staged for the package. + * staged for the package. A picture's size is read from its bytes, and a video's or a + * recording's container; an mp4's index too, for its length and what its tracks hold. * @param {any} field the image, video, audio or file field * @param {{ path: string, copyright?: any }} value as written * @param {string} at where it is written, for problems * @param {(path: string, message: string) => void} report + * @param {(path: string, message: string) => void} [note] what is worth saying of a file and no reason not to build * @returns {Promise>} */ - async resolve(field, value, at, report) { + async resolve(field, value, at, report, note = () => {}) { const kind = /** @type {'image' | 'video' | 'audio' | 'file'} */ (field.type) const source = value.path.trim() const copyright = this.copyright(value.copyright, `${at}.copyright`, report) @@ -245,6 +296,14 @@ export class MediaStore { } finally { await file.close() } + } else if (kind === 'video' || kind === 'audio') { + const file = await this.access.open(staged.from) + try { + const length = await this.#played(file, kind, source, web, `${at}.path`, report, note) + if (length) this.lengths.set(name, length) + } finally { + await file.close() + } } this.files.push({ name, from: staged.from }) this.known.set(key, described) @@ -283,6 +342,46 @@ export class MediaStore { return out } + /** + * A video's or a recording's bytes held to what its field takes: its container, from its first + * bytes, a problem where it is none of those; for an mp4, its index, a problem where there is + * none to read, and a note for a file with no sound, no picture, or a picture not every browser + * plays. A file whose index lists no track says nothing either way. + * @param {{ size: number, read(offset: number, length: number): Promise }} file + * @param {'video' | 'audio'} kind + * @param {string} source as written + * @param {boolean} web named by a web address + * @param {string} at + * @param {(path: string, message: string) => void} report + * @param {(path: string, message: string) => void} note + * @returns {Promise} its length in seconds, where its index gives one + */ + async #played(file, kind, source, web, at, report, note) { + const { takes, what } = PLAYS[kind] + // An mp4 by its index, as the normalizer reads one, after `ftyp` or an older file's `moov`, + // `mdat`, `free` or `wide`; any other by its first bytes. + const info = await mp4Info((offset, length) => file.read(offset, length), file.size) + const container = info ? 'mp4' : containerOf(await file.read(0, 16)) + if (container === 'page') { + report(at, `${source} is a web page, not ${kind === 'video' ? 'a video' : 'a recording'}${web ? ': give the address of the file itself' : ''}`) + return undefined + } + if (!container || !takes.includes(container)) { + report(at, `${source} is not ${what}: its first bytes are none of theirs`) + return undefined + } + if (!info) { + if (container === 'mp4') report(at, `${source} has no index (moov) a player can read: is the file whole?`) + return undefined + } + const held = new Set(info.tracks.map((track) => track.kind)) + if (held.size && !held.has('audio')) note(at, `${source} has no sound`) + if (held.size && kind === 'video' && !held.has('video')) note(at, `${source} has no picture`) + const codecs = new Set(info.tracks.filter((track) => track.kind === 'video').map((track) => NOT_EVERYWHERE[track.codec]).filter(Boolean)) + for (const codec of codecs) note(at, `${source} is ${codec}, which not every browser plays; H.264 plays in all of them`) + return info.duration + } + /** * @param {string} source * @param {Record} types diff --git a/packages/author/lib/node.mjs b/packages/author/lib/node.mjs index 69c9a707..46de1c67 100644 --- a/packages/author/lib/node.mjs +++ b/packages/author/lib/node.mjs @@ -136,7 +136,12 @@ export function nodeMedia({ base, temporary, within }) { }, async open(file) { const handle = await open(file, 'r') + const { size } = await handle.stat().catch(async (error) => { + await handle.close() + throw error + }) return { + size, async read(offset, length) { const bytes = new Uint8Array(length) const { bytesRead } = await handle.read(bytes, 0, length, offset) diff --git a/packages/author/lib/types.mjs b/packages/author/lib/types.mjs index bb6ba4a4..d4a2decd 100644 --- a/packages/author/lib/types.mjs +++ b/packages/author/lib/types.mjs @@ -447,6 +447,35 @@ const interactiveVideo = { } } +/** + * An Interactive Video's times held to its video's length, where the build read one (an mp4's + * index gives it): an interaction that starts past the end never shows, a bookmark or an end + * screen past it is never reached, a start past it plays nothing. An interaction may end past it, + * and shows to the end. + * @param {any} params an Interactive Video's, as H5P stores them + * @param {string} path where they are + * @param {(file: string) => number | undefined} lengthOf a video's length in seconds, by its path in the package + * @param {(path: string, message: string) => void} report + */ +export function videoTimes(params, path, lengthOf, report) { + const files = params?.interactiveVideo?.video?.files + const length = (Array.isArray(files) ? files : []).map((file) => (typeof file?.path === 'string' ? lengthOf(file.path) : undefined)).find((each) => each !== undefined) + if (length === undefined) return + const end = `the video ends at ${Math.round(length * 100) / 100} s` + const late = (/** @type {unknown} */ time) => typeof time === 'number' && time > length + const list = (/** @type {unknown} */ value) => (Array.isArray(value) ? value : []) + const assets = params.interactiveVideo.assets + list(assets?.interactions).forEach((interaction, i) => { + if (late(interaction?.duration?.from)) report(`${path}.interactiveVideo.assets.interactions[${i}].duration`, `starts at ${interaction.duration.from} s, after ${end}`) + }) + for (const key of ['bookmarks', 'endscreens']) { + list(assets?.[key]).forEach((mark, i) => { + if (late(mark?.time)) report(`${path}.interactiveVideo.assets.${key}[${i}].time`, `is ${mark.time} s, after ${end}`) + }) + } + if (late(params.override?.startVideoAt)) report(`${path}.override.startVideoAt`, `is ${params.override.startVideoAt} s, after ${end}`) +} + /* ---------------------------------------------------------------- Branching Scenario */ /** diff --git a/packages/author/tests/browser/page.test.ts b/packages/author/tests/browser/page.test.ts index 2204a327..15ce8376 100644 --- a/packages/author/tests/browser/page.test.ts +++ b/packages/author/tests/browser/page.test.ts @@ -2,6 +2,7 @@ import { pack } from '@missing-elements/h5p-libraries/install' import { describe, expect, it } from 'vitest' import { commands } from 'vitest/browser' import { buildPackage, openPack } from '../../lib/browser.mjs' +import { mp4 } from '../helpers/mp4' // The page's build in Chromium, as a page runs it: the pack a bundler emits, a picture the page // was given as a `File`, the package back as a `Blob`. Its bytes go to Node, where the verifier @@ -77,4 +78,23 @@ describe('the build in a page', () => { await libraries.close() } }) + + it('reads a video\'s index from the File it was given, as a build in Node reads it from disk (#218)', async () => { + const libraries = await openPack(pack) + try { + const clip = new File([mp4(false, { seconds: 30, tracks: [['vide', 'avc1']] })], 'clip.mp4', { type: 'video/mp4' }) + const source = (from: number) => ({ + ...META, + type: 'H5P.InteractiveVideo', + params: { interactiveVideo: { video: { files: [{ path: 'clip.mp4' }] }, assets: { interactions: [{ duration: { from, to: from + 2 }, label: 'Look', action: { library: 'H5P.Text', params: { text: 'Look' } } }] } } } + }) + const built = await buildPackage(source(5), { libraries, files: { 'clip.mp4': clip } }) + expect(built.notes.filter(({ path }) => path.endsWith('.path'))).toEqual([{ path: 'params.interactiveVideo.video.files[0].path', message: 'clip.mp4 has no sound' }]) + await expect(buildPackage(source(40), { libraries, files: { 'clip.mp4': clip } })).rejects.toMatchObject({ + problems: [{ path: 'params.interactiveVideo.assets.interactions[0].duration', message: 'starts at 40 s, after the video ends at 30 s' }] + }) + } finally { + await libraries.close() + } + }) }) diff --git a/packages/author/tests/build.test.ts b/packages/author/tests/build.test.ts index 805980a5..70d900d9 100644 --- a/packages/author/tests/build.test.ts +++ b/packages/author/tests/build.test.ts @@ -561,6 +561,8 @@ describe('media', () => { if (request.url === '/pictures/map.png') response.writeHead(200, { 'content-type': 'image/png' }).end(png(40, 20)) else if (request.url === '/picture') response.writeHead(200, { 'content-type': 'image/png' }).end(png(10, 30)) else if (request.url === '/page.html') response.writeHead(200, { 'content-type': 'text/html' }).end('

no

') + // A file's page on a wiki, at an address that ends as the file's name does (#218). + else if (request.url === '/wiki/File:clip.mp4') response.writeHead(200, { 'content-type': 'text/html' }).end('File:clip.mp4') else if (request.url === '/odd%zz.png') response.writeHead(200, { 'content-type': 'image/png' }).end(png(20, 10)) else if (request.url === '/trickle.png') { // Slow, and never silent for long: eight pieces, 100 ms apart. @@ -687,6 +689,67 @@ describe('media', () => { ]) }) + it('reads a video\'s and a recording\'s first bytes, and refuses one that is not what the field takes (#218)', async () => { + await writeFile(join(dir, 'page.mp4'), 'clip') + await writeFile(join(dir, 'song.mp3'), 'not a recording at all') + await writeFile(join(dir, 'cut.mp4'), mp4(true).subarray(0, 40)) + // A page in UTF-16: its byte-order mark, FF FE, once passed for an MP3 frame's sync bits. + await writeFile(join(dir, 'wide.mp3'), Buffer.from('\ufeffsong', 'utf16le')) + const video = (path: string) => ({ content: { library: 'H5P.Video', params: { sources: { path } } } }) + const audio = (path: string) => ({ content: { library: 'H5P.Audio', params: { files: { path } } } }) + expect(await problems({ + ...META, + type: 'H5P.Column', + params: { content: [video('page.mp4'), video(`${origin}/wiki/File:clip.mp4`), audio('song.mp3'), video('cut.mp4'), audio('wide.mp3')] } + }, { base: dir })).toEqual([ + 'params.content[0].content.params.sources.path: page.mp4 is a web page, not a video', + `params.content[1].content.params.sources.path: ${origin}/wiki/File:clip.mp4 is a web page, not a video: give the address of the file itself`, + 'params.content[2].content.params.files.path: song.mp3 is not an MP3, M4A, WAV, Ogg or WebM recording: its first bytes are none of theirs', + 'params.content[3].content.params.sources.path: cut.mp4 has no index (moov) a player can read: is the file whole?', + 'params.content[4].content.params.files.path: wide.mp3 is a web page, not a recording' + ]) + }) + + it('says of an mp4 that it has no sound, no picture, or a picture not every browser plays (#218)', async () => { + await writeFile(join(dir, 'silent.mp4'), mp4(false, { seconds: 30, tracks: [['vide', 'avc1']] })) + await writeFile(join(dir, 'radio.mp4'), mp4(true, { seconds: 30, tracks: [['soun', 'mp4a']] })) + await writeFile(join(dir, 'phone.mp4'), mp4(true, { seconds: 30, tracks: [['vide', 'hvc1'], ['soun', 'mp4a']] })) + await writeFile(join(dir, 'fine.mp4'), mp4(true, { seconds: 30, tracks: [['vide', 'avc1'], ['soun', 'mp4a']] })) + // An older file with no `ftyp`, its index first: an mp4 by its index, as the normalizer reads it. + await writeFile(join(dir, 'old.mp4'), mp4(true, { seconds: 30, tracks: [['vide', 'avc1'], ['soun', 'mp4a']] }).subarray(24)) + const { notes } = await build({ + ...META, + type: 'H5P.Column', + params: { content: ['silent.mp4', 'radio.mp4', 'phone.mp4', 'fine.mp4', 'old.mp4'].map((path) => ({ content: { library: 'H5P.Video', params: { sources: { path } } } })) } + }, { base: dir }) + expect(notes.filter(({ path }) => path.endsWith('.sources.path')).map(({ path, message }) => `${path}: ${message}`)).toEqual([ + 'params.content[0].content.params.sources.path: silent.mp4 has no sound', + 'params.content[1].content.params.sources.path: radio.mp4 has no picture', + 'params.content[2].content.params.sources.path: phone.mp4 is HEVC (H.265), which not every browser plays; H.264 plays in all of them' + ]) + }) + + it('refuses an Interactive Video\'s time past the end of a video whose length it read (#218)', async () => { + await writeFile(join(dir, 'short.mp4'), mp4(true, { seconds: 30.0333, tracks: [['vide', 'avc1'], ['soun', 'mp4a']] })) + const text = (from: number, to: number) => ({ duration: { from, to }, label: 'Look', action: { library: 'H5P.Text', params: { text: 'Look' } } }) + const source = (interactions: unknown[], bookmarks: unknown[], startVideoAt?: number) => ({ + ...META, + type: 'H5P.InteractiveVideo', + params: { interactiveVideo: { video: { files: [{ path: 'short.mp4' }] }, assets: { interactions, bookmarks } }, ...(startVideoAt === undefined ? {} : { override: { startVideoAt, retryButton: 'on' } }) } + }) + expect(await problems(source([text(5, 40), text(500, 600)], [{ time: 10, label: 'In' }, { time: 31, label: 'Out' }], 45), { base: dir })).toEqual([ + 'params.interactiveVideo.assets.interactions[1].duration: starts at 500 s, after the video ends at 30.03 s', + 'params.interactiveVideo.assets.bookmarks[1].time: is 31 s, after the video ends at 30.03 s', + 'params.override.startVideoAt: is 45 s, after the video ends at 30.03 s' + ]) + // Within its length, an interaction that ends past it included, it builds. + expect((await build(source([text(5, 40)], [{ time: 10, label: 'In' }]), { base: dir })).content.interactiveVideo.assets.interactions).toHaveLength(1) + // An item refused leaves a list whose indices are not the source's: no time is named in it. + expect(await problems(source(['a stray line', text(500, 600)], []), { base: dir })).toEqual([ + expect.stringMatching(/^params\.interactiveVideo\.assets\.interactions\[0\]: expected an object/) + ]) + }) + it('gives a download up once nothing has arrived for a while, however long a slow one takes', async () => { const reported: string[] = [] const report = (path: string, message: string) => reported.push(`${path}: ${message}`) diff --git a/packages/author/tests/helpers/media.ts b/packages/author/tests/helpers/media.ts index bd026825..e0f852a4 100644 --- a/packages/author/tests/helpers/media.ts +++ b/packages/author/tests/helpers/media.ts @@ -37,16 +37,4 @@ export function jpeg(width: number, height: number, padding = 0) { return Buffer.concat([...segments, frame, Buffer.from([0xff, 0xd9])]) } -/** An mp4 as boxes, its index after its media unless `faststart`. */ -export function mp4(faststart: boolean) { - const box = (type: string, body: Buffer) => { - const head = Buffer.alloc(8) - head.writeUInt32BE(8 + body.length) - head.write(type, 4) - return Buffer.concat([head, body]) - } - const ftyp = box('ftyp', Buffer.from('isom\0\0\0\0isomavc1')) - const moov = box('moov', box('mvhd', Buffer.alloc(100))) - const mdat = box('mdat', Buffer.alloc(4096, 7)) - return Buffer.concat(faststart ? [ftyp, moov, mdat] : [ftyp, mdat, moov]) -} +export { mp4 } from './mp4' diff --git a/packages/author/tests/helpers/mp4.ts b/packages/author/tests/helpers/mp4.ts new file mode 100644 index 00000000..4853cf8c --- /dev/null +++ b/packages/author/tests/helpers/mp4.ts @@ -0,0 +1,40 @@ +/** + * An mp4 as boxes, its index after its media unless `faststart`. With `seconds` its index says how + * long it is, and with `tracks` what each track holds, by its handler and its codec, where a reader + * looks for them; without, it says neither. Nothing here decodes, and nothing needs Node: the + * page's build is given one too. + */ +export function mp4(faststart: boolean, { seconds, tracks = [] }: { seconds?: number, tracks?: [handler: string, codec: string][] } = {}) { + const text = (value: string) => new TextEncoder().encode(value) + const concat = (parts: Uint8Array[]) => { + const out = new Uint8Array(parts.reduce((total, part) => total + part.length, 0)) + let at = 0 + for (const part of parts) { + out.set(part, at) + at += part.length + } + return out + } + const box = (type: string, ...body: Uint8Array[]) => { + const inner = concat(body) + const out = new Uint8Array(8 + inner.length) + new DataView(out.buffer).setUint32(0, out.length) + out.set(text(type), 4) + out.set(inner, 8) + return out + } + const words = (...values: number[]) => { + const out = new Uint8Array(values.length * 4) + values.forEach((value, i) => new DataView(out.buffer).setUint32(i * 4, value)) + return out + } + const ftyp = box('ftyp', text('isom\0\0\0\0isomavc1')) + // Version 0: flags, two times, the timescale and the duration, then the rest of the header. + const mvhd = box('mvhd', seconds === undefined ? new Uint8Array(100) : concat([words(0, 0, 0, 1000, Math.round(seconds * 1000)), new Uint8Array(80)])) + const trak = ([handler, codec]: [string, string]) => box('trak', box('mdia', + box('hdlr', words(0, 0), text(handler), new Uint8Array(13)), + box('minf', box('stbl', box('stsd', words(0, 1, 16), text(codec), new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1])))))) + const moov = box('moov', mvhd, ...tracks.map(trak)) + const mdat = box('mdat', new Uint8Array(4096).fill(7)) + return concat(faststart ? [ftyp, moov, mdat] : [ftyp, mdat, moov]) +} diff --git a/packages/normalize/README.md b/packages/normalize/README.md index adfdadec..34a41ba8 100644 --- a/packages/normalize/README.md +++ b/packages/normalize/README.md @@ -56,9 +56,16 @@ knows which folders those are. TypeScript declarations ship with the package: `Report` and `EntryResult` describe what it returns. -Node only, not a browser. `crc32(bytes, previous)` from `@missing-elements/h5p-normalize/crc32` +`normalizeArchive()` is Node only, not a browser. `crc32(bytes, previous)` from `@missing-elements/h5p-normalize/crc32` is the CRC-32 it writes entries with, for code that writes zips beside it, in a page too: zlib's in -Node, a table elsewhere. Why deflated video cannot stream: the demo's +Node, a table elsewhere. + +`mp4Info(read, size)` from `@missing-elements/h5p-normalize/mp4` says what an mp4's index holds: its +length in seconds, and each track's kind and codec. `read(offset, length)` gives the file's bytes, +fewer at its end, so it runs on a file in Node or a `Blob` in a page. It reads atoms' headers and a +few small atoms, never the media. + +Why deflated video cannot stream: the demo's [normalize page](https://github.com/missing-elements/h5p-offline-player/blob/main/apps/demo/demo/normalize.html). ## Licence diff --git a/packages/normalize/lib/mp4-faststart.mjs b/packages/normalize/lib/mp4-faststart.mjs index 63cd748f..3b7b423d 100644 --- a/packages/normalize/lib/mp4-faststart.mjs +++ b/packages/normalize/lib/mp4-faststart.mjs @@ -10,13 +10,11 @@ * whole. Only `moov` — a few megabytes at most — is read into memory, because it is rewritten. */ import { formatBytes } from './format.mjs' +import { topLevelAtoms } from './mp4.mjs' /** Atoms whose children hold the chunk offset tables. Nothing under any other branch does. */ const CONTAINERS = new Set(['trak', 'mdia', 'minf', 'stbl']) -/** Types a file can plausibly start with. Anything else is not an ISO media file. */ -const LEADING_TYPES = new Set(['ftyp', 'moov', 'mdat', 'free', 'skip', 'wide', 'uuid', 'pnot', 'styp', 'sidx']) - /** Larger than any real index; a `moov` past this is a malformed file, and is left alone. */ const MAX_MOOV_SIZE = 256 * 1024 * 1024 @@ -26,12 +24,6 @@ const READ_CHUNK = 1024 * 1024 * @typedef {{ start: number, end: number } | { bytes: Uint8Array }} Piece * Either a range of the source file — absolute positions, end exclusive — or literal bytes. * - * @typedef {object} Atom - * @property {string} type - * @property {number} offset relative to the start of the media file - * @property {number} size including the header - * @property {number} headerSize 8, or 16 with a 64-bit size - * * @typedef {object} FaststartPlan * @property {'moved' | 'already' | 'skipped'} status * @property {string} detail why, in words @@ -54,7 +46,14 @@ export async function planFaststart(file, base, size) { /** @param {string} detail */ const skipped = (detail) => ({ status: /** @type {const} */ ('skipped'), detail, pieces: identity, size }) - const atoms = await readTopLevelAtoms(file, base, size) + // The media file's own bytes, never past its end: it may be a range of a larger file. + const read = async (/** @type {number} */ offset, /** @type {number} */ length) => { + const buffer = Buffer.alloc(Math.max(0, Math.min(length, size - offset))) + await readExact(file, buffer, base + offset) + return buffer + } + // As far as a fragment, which says the file streams as it is, however many follow. + const atoms = await topLevelAtoms(read, size, 'moof') if (!atoms) return skipped('not an ISO media file') if (atoms.some((atom) => atom.type === 'moof')) return skipped('fragmented mp4, already streamable') @@ -118,46 +117,6 @@ export async function* readPieces(file, pieces) { } } -/** - * The top-level atoms of the file, or `null` where its structure is not one this understands. - * @param {import('node:fs/promises').FileHandle} file - * @param {number} base - * @param {number} size - * @returns {Promise} - */ -async function readTopLevelAtoms(file, base, size) { - /** @type {Atom[]} */ - const atoms = [] - const header = Buffer.alloc(16) - let position = 0 - - while (position < size) { - if (size - position < 8) return null - await readExact(file, header.subarray(0, 8), base + position) - let atomSize = header.readUInt32BE(0) - const type = header.toString('latin1', 4, 8) - let headerSize = 8 - - if (atomSize === 1) { - if (size - position < 16) return null - await readExact(file, header.subarray(8, 16), base + position + 8) - atomSize = Number(header.readBigUInt64BE(8)) - headerSize = 16 - } else if (atomSize === 0) { - atomSize = size - position - } - - if (atomSize < headerSize || position + atomSize > size) return null - if (!/^[\x20-\x7e]{4}$/.test(type)) return null - if (atoms.length === 0 && !LEADING_TYPES.has(type)) return null - - atoms.push({ type, offset: position, size: atomSize, headerSize }) - position += atomSize - } - - return atoms -} - /** * Walks the atoms between `start` and `end` inside `moov`, descending into the containers that * lead to sample tables, and adds `shift(offset)` to every chunk offset it finds. Any other diff --git a/packages/normalize/lib/mp4.mjs b/packages/normalize/lib/mp4.mjs new file mode 100644 index 00000000..bb09ce5d --- /dev/null +++ b/packages/normalize/lib/mp4.mjs @@ -0,0 +1,168 @@ +/** + * An ISO media file, an mp4's or an m4a's, read by its atoms: where they are, and what its index + * (`moov`) says of it. Through a `read(offset, length)` that gives fewer bytes at the file's end, + * so it runs wherever the bytes are, a file in Node or a `Blob` on a page. Only atoms' headers and + * a few small atoms are read: never the media, nor the index's tables. + */ + +/** + * @typedef {(offset: number, length: number) => Promise} Read the file's bytes from an + * offset, fewer at its end + * + * @typedef {object} Atom + * @property {string} type + * @property {number} offset relative to the start of the media file + * @property {number} size including the header + * @property {number} headerSize 8, or 16 with a 64-bit size + * + * @typedef {object} Mp4Info + * @property {number | undefined} duration in seconds, where the index says + * @property {{ kind: 'video' | 'audio' | 'other', codec: string }[]} tracks each track's kind, by + * its handler, and the format of its first sample description: `avc1`, `hvc1`, `mp4a`… + */ + +/** Types a file can plausibly start with. Anything else is not an ISO media file. */ +const LEADING_TYPES = new Set(['ftyp', 'moov', 'mdat', 'free', 'skip', 'wide', 'uuid', 'pnot', 'styp', 'sidx']) + +/** What a track holds, by its handler's type. */ +const KINDS = /** @type {Record} */ ({ vide: 'video', soun: 'audio' }) + +/** + * More atoms than a real file has at one level, a fragmented one included as far as its index: + * a file of more is not one this reads, and is not walked to its end, so that one made of tiny + * atoms costs neither the memory nor the time. + */ +const MAX_ATOMS = 10_000 + +/** @param {Uint8Array} bytes */ +const ascii = (bytes) => String.fromCharCode(...bytes) + +/** + * The atoms between `start` and `end`, each by its header, as far as the first of type `until` + * where one is given; `null` where one is not whole there or is no atom, or where there are more + * than `MAX_ATOMS`. + * @param {Read} read + * @param {number} start + * @param {number} end + * @param {{ file?: boolean, until?: string }} [options] `file`, the file's own atoms, the first of + * which has a type a file starts with; `until`, the type to stop at + * @returns {Promise} + */ +async function atomsIn(read, start, end, { file = false, until } = {}) { + /** @type {Atom[]} */ + const atoms = [] + let position = start + while (position < end) { + if (end - position < 8 || atoms.length === MAX_ATOMS) return null + const header = await read(position, 8) + if (header.length < 8) return null + let size = new DataView(header.buffer, header.byteOffset, 8).getUint32(0) + const type = ascii(header.subarray(4, 8)) + let headerSize = 8 + if (size === 1) { + // A 64-bit size, read only where an atom has one: the eight bytes after a header are its data. + const wide = end - position < 16 ? new Uint8Array(0) : await read(position + 8, 8) + if (wide.length < 8) return null + size = Number(new DataView(wide.buffer, wide.byteOffset, 8).getBigUint64(0)) + headerSize = 16 + } else if (size === 0) { + size = end - position + } + if (size < headerSize || position + size > end) return null + if (!/^[\x20-\x7e]{4}$/.test(type)) return null + if (file && atoms.length === 0 && !LEADING_TYPES.has(type)) return null + atoms.push({ type, offset: position, size, headerSize }) + if (type === until) break + position += size + } + return atoms +} + +/** + * The top-level atoms of the file, as far as the first of type `until` where one is given, or + * `null` where its structure is not one this understands. + * @internal the walk the faststart shares + * @param {Read} read + * @param {number} size the file's length + * @param {string} [until] + * @returns {Promise} + */ +export function topLevelAtoms(read, size, until) { + return atomsIn(read, 0, size, { file: true, until }) +} + +/** + * Text in an atom's body, from `at`: never past the atom, and none where it is too short. + * @param {Read} read + * @param {Atom | undefined} atom + * @param {number} at + * @param {number} length + */ +async function textIn(read, atom, at, length) { + return atom && atom.size - atom.headerSize >= at + length ? ascii(await read(atom.offset + atom.headerSize + at, length)) : '' +} + +/** @param {Read} read @param {Atom} atom */ +const childrenOf = (read, atom) => atomsIn(read, atom.offset + atom.headerSize, atom.offset + atom.size) + +/** + * What an mp4's index says of it: how long it is, and its tracks with their codecs. `null` where + * the file is no ISO media file or has no index this can read. + * @param {Read} read + * @param {number} size the file's length + * @returns {Promise} + */ +export async function mp4Info(read, size) { + // As far as the index, which a fragmented file has first, and any other first or last. + const moov = (await topLevelAtoms(read, size, 'moov'))?.find((atom) => atom.type === 'moov') + const index = moov && (await childrenOf(read, moov)) + // An index says what the movie is in its header: without one, it is none. + if (!index?.some((atom) => atom.type === 'mvhd')) return null + /** @type {Mp4Info} */ + const info = { duration: undefined, tracks: [] } + for (const atom of index) { + if (atom.type === 'mvhd') info.duration = await durationOf(read, atom) + else if (atom.type === 'trak') { + const track = await trackOf(read, atom) + if (track) info.tracks.push(track) + } + } + return info +} + +/** + * The movie header's duration in seconds, or `undefined` where it gives none: 0 or all ones, as a + * fragmented file's may. + * @param {Read} read + * @param {Atom} mvhd + */ +async function durationOf(read, mvhd) { + const body = await read(mvhd.offset + mvhd.headerSize, Math.min(32, mvhd.size - mvhd.headerSize)) + const wide = body[0] === 1 + if (body.length < (wide ? 32 : 20)) return undefined + const view = new DataView(body.buffer, body.byteOffset, body.byteLength) + const timescale = view.getUint32(wide ? 20 : 12) + const duration = wide ? view.getBigUint64(24) : BigInt(view.getUint32(16)) + if (!timescale || !duration || duration === (wide ? 0xffffffffffffffffn : 0xffffffffn)) return undefined + return Number(duration) / timescale +} + +/** + * A track's kind, from `mdia` › `hdlr`, and its codec, the first sample description's format in + * `mdia` › `minf` › `stbl` › `stsd`; `null` where it has no `mdia` this can read. + * @param {Read} read + * @param {Atom} trak + */ +async function trackOf(read, trak) { + const mdia = (await childrenOf(read, trak))?.find((atom) => atom.type === 'mdia') + const media = mdia && (await childrenOf(read, mdia)) + if (!media) return null + const hdlr = media.find((atom) => atom.type === 'hdlr') + const minf = media.find((atom) => atom.type === 'minf') + const stbl = minf && (await childrenOf(read, minf))?.find((atom) => atom.type === 'stbl') + const stsd = stbl && (await childrenOf(read, stbl))?.find((atom) => atom.type === 'stsd') + // After the version and flags: `hdlr` has a word before its handler, `stsd` its count and the entry's size. + const handler = await textIn(read, hdlr, 8, 4) + const codec = await textIn(read, stsd, 12, 4) + return { kind: KINDS[handler] ?? 'other', codec } +} diff --git a/packages/normalize/package.json b/packages/normalize/package.json index 16d09766..2bcad970 100644 --- a/packages/normalize/package.json +++ b/packages/normalize/package.json @@ -29,6 +29,10 @@ "node": "./lib/crc32.mjs", "default": "./lib/crc32-table.mjs" }, + "./mp4": { + "types": "./types/mp4.d.mts", + "default": "./lib/mp4.mjs" + }, "./package.json": "./package.json" }, "files": [ diff --git a/packages/normalize/tests/helpers/mp4.ts b/packages/normalize/tests/helpers/mp4.ts index 75ccd3a4..1980bd70 100644 --- a/packages/normalize/tests/helpers/mp4.ts +++ b/packages/normalize/tests/helpers/mp4.ts @@ -128,3 +128,29 @@ export function chunkOffsets(bytes: Uint8Array, table: FoundAtom): number[] { wide ? Number(view.getBigUint64(body + 8 + index * 8)) : view.getUint32(body + 8 + index * 4) ) } + +/** + * An mp4 whose index says what a reader looks for: the movie's length in `mvhd` (version 1 with + * `wide`), and for each track its handler in `hdlr` and its codec in `stsd`. Before its media with + * `faststart`, after it otherwise; `mdatSize` bytes of media nobody should read. + */ +export function describedMp4(options: { + duration?: number, timescale?: number, wide?: boolean, faststart?: boolean, mdatSize?: number, + tracks?: { handler: string, codec: string }[] +} = {}): Uint8Array { + const { duration = 30_000, timescale = 1000, wide = false, faststart = true, mdatSize = 4096 } = options + const tracks = options.tracks ?? [{ handler: 'vide', codec: 'avc1' }, { handler: 'soun', codec: 'mp4a' }] + const mvhd = wide + ? atom('mvhd', u32s(0x01000000), u64s(0, 0), u32s(timescale), u64s(duration), new Uint8Array(80)) + : atom('mvhd', u32s(0, 0, 0, timescale, duration), new Uint8Array(80)) + const trak = ({ handler, codec }: { handler: string, codec: string }) => atom('trak', + atom('tkhd', new Uint8Array(84)), + atom('mdia', + atom('mdhd', new Uint8Array(24)), + atom('hdlr', u32s(0, 0), encoder.encode(handler), new Uint8Array(13)), + atom('minf', atom('stbl', atom('stsd', u32s(0, 1, 16), encoder.encode(codec), new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1])), atom('stts', u32s(0, 0)), stco([]))))) + const ftyp = atom('ftyp', encoder.encode('isom'), u32s(0x200), encoder.encode('isomavc1')) + const moov = atom('moov', mvhd, ...tracks.map(trak)) + const mdat = atom('mdat', new Uint8Array(mdatSize).fill(7)) + return concat(faststart ? [ftyp, moov, mdat] : [ftyp, mdat, moov]) +} diff --git a/packages/normalize/tests/mp4.test.ts b/packages/normalize/tests/mp4.test.ts new file mode 100644 index 00000000..7f32abce --- /dev/null +++ b/packages/normalize/tests/mp4.test.ts @@ -0,0 +1,85 @@ +import { describe, expect, it } from 'vitest' +import { mp4Info, topLevelAtoms } from '../lib/mp4.mjs' +import { atom, concat, describedMp4, sampleMp4, u32s } from './helpers/mp4' + +/** A `read` over bytes, as a file's or a `Blob`'s: fewer at the end. It counts what it gave. */ +function reader(bytes: Uint8Array) { + const read = Object.assign(async (offset: number, length: number) => { + const piece = bytes.subarray(offset, offset + length) + read.given += piece.length + return piece + }, { given: 0 }) + return read +} + +const infoOf = (bytes: Uint8Array) => mp4Info(reader(bytes), bytes.length) + +describe('topLevelAtoms', () => { + it('lists the file\'s atoms by their headers', async () => { + const { bytes } = sampleMp4() + expect((await topLevelAtoms(reader(bytes), bytes.length))?.map((atom) => atom.type)).toEqual(['ftyp', 'free', 'mdat', 'moov']) + }) + + it('is null for a file that is no ISO media file', async () => { + const page = new TextEncoder().encode('clip.mp4') + const webm = new Uint8Array([0x1a, 0x45, 0xdf, 0xa3, 0x9f, 0x42, 0x86, 0x81, 0x01, 0x42, 0xf7, 0x81]) + expect(await topLevelAtoms(reader(page), page.length)).toBeNull() + expect(await topLevelAtoms(reader(webm), webm.length)).toBeNull() + }) +}) + +describe('mp4Info', () => { + it('reads the length and each track\'s kind and codec, the index before its media or after', async () => { + const expected = { duration: 30, tracks: [{ kind: 'video', codec: 'avc1' }, { kind: 'audio', codec: 'mp4a' }] } + expect(await infoOf(describedMp4())).toEqual(expected) + expect(await infoOf(describedMp4({ faststart: false }))).toEqual(expected) + }) + + it('reads a 64-bit header, a silent video and an HEVC one', async () => { + expect(await infoOf(describedMp4({ wide: true, duration: 90_090, timescale: 30_000, tracks: [{ handler: 'vide', codec: 'hvc1' }] }))) + .toEqual({ duration: 3.003, tracks: [{ kind: 'video', codec: 'hvc1' }] }) + }) + + it('gives no length where the index gives none, as a fragmented file\'s may', async () => { + expect((await infoOf(describedMp4({ duration: 0 })))?.duration).toBeUndefined() + }) + + it('reads headers and the few atoms it needs, never the media', async () => { + const bytes = describedMp4({ faststart: false, mdatSize: 1_000_000 }) + // The media starts after `mdat`'s type, and runs for its million bytes: none of it is asked for. + const media = bytes.findIndex((_, i) => String.fromCharCode(...bytes.subarray(i, i + 4)) === 'mdat') + 4 + const read = async (offset: number, length: number) => { + if (offset + length > media && offset < media + 1_000_000) throw new Error(`read ${length} bytes of the media at ${offset}`) + return bytes.subarray(offset, offset + length) + } + expect((await mp4Info(read, bytes.length))?.duration).toBe(30) + }) + + it('reads each field within its atom: a header cut short gives no length rather than a neighbour\'s bytes', async () => { + const ftyp = atom('ftyp', new TextEncoder().encode('isom')) + // An mvhd of 12 bytes, where version 0 needs 20: read past its end, the next atom's header + // would pass for a timescale and a duration. + const cut = concat([ftyp, atom('moov', atom('mvhd', u32s(0, 0, 0)), atom('free', u32s(1000, 30_000, 0, 0)))]) + expect(await infoOf(cut)).toEqual({ duration: undefined, tracks: [] }) + }) + + it('is null for a file with no index, an index with no header, and a file that is no mp4', async () => { + const ftyp = atom('ftyp', new TextEncoder().encode('isom')) + expect(await infoOf(concat([ftyp, atom('mdat', new Uint8Array(100))]))).toBeNull() + expect(await infoOf(concat([ftyp, atom('moov', atom('udta', new Uint8Array(4))), atom('mdat', new Uint8Array(100))]))).toBeNull() + expect(await infoOf(new TextEncoder().encode('ID3\u0004\u0000\u0000\u0000\u0000\u0000\u0000'))).toBeNull() + }) + + it('stops at the index, and gives up on a file of more atoms than a real one has before it', async () => { + // A fragmented file: its index first, then as many fragments as it likes, which are not walked. + const fragmented = concat([describedMp4(), ...Array.from({ length: 20_000 }, () => atom('moof', new Uint8Array(0)))]) + const read = reader(fragmented) + expect((await mp4Info(read, fragmented.length))?.duration).toBe(30) + expect(read.given).toBeLessThan(1_000) + // A file of tiny atoms and no index costs ten thousand headers at most, not one per atom. + const tiny = concat([atom('ftyp', new TextEncoder().encode('isom')), ...Array.from({ length: 20_000 }, () => atom('free', new Uint8Array(0)))]) + const counted = reader(tiny) + expect(await mp4Info(counted, tiny.length)).toBeNull() + expect(counted.given).toBeLessThanOrEqual(10_000 * 8) + }) +}) diff --git a/packages/normalize/tests/package-types.ts b/packages/normalize/tests/package-types.ts index fe51b07e..3e1ea6d0 100644 --- a/packages/normalize/tests/package-types.ts +++ b/packages/normalize/tests/package-types.ts @@ -8,6 +8,10 @@ import { archiveFingerprint, formatBytes, normalizeArchive, type EntryResult, ty import { orderEntries } from '@missing-elements/h5p-normalize' // For the packages of this repository that write zips, h5p-author's page build among them. import { crc32 } from '@missing-elements/h5p-normalize/crc32' +// For h5p-author, which reads a video's index where it builds: in Node, and on a page. +import { mp4Info, type Mp4Info } from '@missing-elements/h5p-normalize/mp4' +// @ts-expect-error the walk the normalizer shares with it, not part of the package's API +import { topLevelAtoms } from '@missing-elements/h5p-normalize/mp4' export async function asACallerWould(): Promise { const seen: EntryResult[] = [] @@ -28,6 +32,12 @@ export async function asACallerWould(): Promise { // @ts-expect-error bytes, not text crc32('h5p.json') - void [moved, saved, orderEntries, checked, trailing] + const info: Mp4Info | null = await mp4Info(async (offset, length) => new Uint8Array(length), 100) + const length: number | undefined = info?.duration + const codecs: string[] = info?.tracks.filter(({ kind }) => kind === 'video').map(({ codec }) => codec) ?? [] + // @ts-expect-error a read, not the bytes + await mp4Info(new Uint8Array(100), 100) + + void [moved, saved, orderEntries, checked, trailing, length, codecs, topLevelAtoms] return `${report.revision} ${revision}` } diff --git a/packages/normalize/tsconfig.types.json b/packages/normalize/tsconfig.types.json index 454d11b7..7a799973 100644 --- a/packages/normalize/tsconfig.types.json +++ b/packages/normalize/tsconfig.types.json @@ -8,5 +8,5 @@ "rootDir": "lib", "outDir": "types" }, - "include": ["lib/normalize.mjs", "lib/crc32.mjs"] + "include": ["lib/normalize.mjs", "lib/crc32.mjs", "lib/mp4.mjs"] }