Skip to content

Commit 480807e

Browse files
fix(core): a split sub-composition plays its videos from its in-point in preview and timeline (#5413)
* fix(core): preview plays a split sub-composition's videos from its in-point * fix(cli): timeline shows a split sub-composition's clips from its in-point * fix(core): split halves cut nested media and keep each clip's loop, ramp and fades * fix(core): a scene's media plays past a shorter root animation again * fix(core): only a host with an in-point shifts or cuts what it holds * test(core): a split half with no id stops at its own end * fix(core): the playhead and Studio's clip list skip what an in-point cuts * fix(core): keep clips under a cut-away nested host nested in studio * refactor(core): name why a cut-away nested host stays listed
1 parent 64c47cf commit 480807e

16 files changed

Lines changed: 1164 additions & 147 deletions
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import { join } from "node:path";
4+
import { beforeAll, describe, expect, it, onTestFinished } from "vitest";
5+
import { ensureDOMParser } from "../utils/dom.js";
6+
import { describeProject } from "./describeProject.js";
7+
8+
// What Studio's split writes when a 12 s scene clip is cut at 5 s and then at 9 s.
9+
const SPLIT_INDEX = `<!DOCTYPE html><html><head></head><body><div data-composition-id="main" data-duration="12">
10+
<div data-playback-start="0" id="scene" class="clip" data-composition-id="scene" data-composition-src="compositions/scene.html" data-start="0" data-duration="5" data-track-index="0"></div><div data-playback-start="5" id="scene-split" class="clip" data-composition-id="scene-split" data-composition-src="compositions/scene.html" data-start="5" data-duration="4" data-track-index="0"></div><div data-playback-start="9" id="scene-split-split" class="clip" data-composition-id="scene-split-split" data-composition-src="compositions/scene.html" data-start="9" data-duration="3" data-track-index="0"></div>
11+
</div></body></html>`;
12+
const SCENE = `<template id="scene-template"><div data-composition-id="scene" data-duration="12">
13+
<video id="v1" class="clip" src="../sample.mp4" muted data-start="0" data-duration="4" data-media-start="1" data-track-index="0"></video>
14+
<video id="v2" class="clip" src="../sample.mp4" muted data-start="4" data-duration="4" data-media-start="5" data-track-index="0"></video>
15+
<video id="v3" class="clip" src="../sample.mp4" muted data-start="8" data-duration="4" data-media-start="2" data-track-index="0"></video>
16+
</div></template>`;
17+
18+
beforeAll(ensureDOMParser);
19+
20+
describe("a sub-composition's in-point", () => {
21+
it("shifts each later split half's videos by its in-point and cuts them to that half", async () => {
22+
const dir = mkdtempSync(join(tmpdir(), "hf-timeline-inpoint-"));
23+
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
24+
mkdirSync(join(dir, "compositions"));
25+
writeFileSync(join(dir, "index.html"), SPLIT_INDEX);
26+
writeFileSync(join(dir, "compositions", "scene.html"), SCENE);
27+
28+
const timeline = await describeProject(join(dir, "index.html"));
29+
const videos = timeline.tracks.find((t) => t.kind === "video")!.rows;
30+
31+
// The first half's in-point is 0, so it shows its children's authored extent, as before.
32+
expect(videos.map((r) => [r.host, r.id, r.absStart, r.absEnd])).toEqual([
33+
["scene", "v1", 0, 4],
34+
["scene", "v2", 4, 8],
35+
["scene-split", "v2", 5, 8],
36+
["scene", "v3", 8, 12],
37+
["scene-split", "v3", 8, 9],
38+
["scene-split-split", "v3", 9, 12],
39+
]);
40+
});
41+
42+
it("cuts nothing for a host whose in-point is 0, as Studio stamps on every inserted scene", async () => {
43+
const dir = mkdtempSync(join(tmpdir(), "hf-timeline-inpoint0-"));
44+
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
45+
mkdirSync(join(dir, "compositions"));
46+
writeFileSync(
47+
join(dir, "index.html"),
48+
`<div data-composition-id="main" data-duration="12"><div id="scene" data-composition-src="compositions/scene.html" data-start="1" data-duration="3" data-playback-start="0"></div></div>`,
49+
);
50+
writeFileSync(join(dir, "compositions", "scene.html"), SCENE);
51+
52+
const timeline = await describeProject(join(dir, "index.html"));
53+
const videos = timeline.tracks.find((t) => t.kind === "video")!.rows;
54+
55+
expect(videos.map((r) => [r.id, r.absStart, r.absEnd])).toEqual([
56+
["v1", 1, 5],
57+
["v2", 5, 9],
58+
["v3", 9, 13],
59+
]);
60+
});
61+
});

‎packages/cli/src/timeline/describeProject.ts‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ import {
2424
type StructureNode,
2525
type TrackKind,
2626
} from "@hyperframes/parsers";
27-
import { resolveMediaStartSeconds } from "@hyperframes/core/media-timing";
27+
import {
28+
compositionOriginSeconds,
29+
cutToHostSlots,
30+
hostInPointSeconds,
31+
resolveMediaStartSeconds,
32+
type HostSlot,
33+
} from "@hyperframes/core/media-timing";
2834
import {
2935
extractAudioMetadata,
3036
extractMediaMetadata,
@@ -47,7 +53,8 @@ export interface TimelineRow extends ClipFact {
4753
pendingReason: string | null;
4854
/** Why `data-automation` / `data-fx-chain` could not be read; `null` when fine or absent. */
4955
laneError: string | null;
50-
/** Start and end on the main timeline, in seconds. `start`/`end` are local to the owning file's composition. */
56+
/** Main-timeline window in seconds: the played part under an in-point host, while `duration` stays
57+
* the authored length. `start`/`end` are local to the owning file's composition. */
5158
absStart: number;
5259
absEnd: number;
5360
/** Project-relative path of the file that declares this clip. */
@@ -133,7 +140,7 @@ interface DocScope {
133140
startCache: Map<Element, number>;
134141
/** Sub-composition files must stay inside the project. */
135142
projectDir: string;
136-
/** Main-timeline start of this document's root: 0 for index.html, the host's start for a sub-composition. */
143+
/** Main-timeline time of this document's local t=0: 0 for index.html, see `compositionOriginSeconds`. */
137144
origin: number;
138145
/** Project-relative path of this document, with `/` separators. */
139146
file: string;
@@ -277,7 +284,12 @@ async function resolveRowTiming(scope: DocScope, node: DomNode, depth: number):
277284
const authored = resolveReferencedDuration(scope.doc, el, scope.startCache, new Set());
278285
const host = el.getAttribute("data-composition-src");
279286
const absStart = mainTimelineStart(scope, el, start);
280-
const children = host && depth === 0 ? await readSubComposition(host, scope, absStart) : [];
287+
const slots: HostSlot[] =
288+
hostInPointSeconds(el) > 0 ? [{ start: absStart, end: absStart + (authored ?? Infinity) }] : [];
289+
const children =
290+
host && depth === 0
291+
? await readSubComposition(host, scope, compositionOriginSeconds(absStart, el), slots)
292+
: [];
281293
const kind = el.tagName.toLowerCase();
282294
const duration = MEDIA_TAG.test(kind)
283295
? await resolveMediaRowDuration(scope, el, kind as MediaTag, authored)
@@ -334,6 +346,7 @@ async function readSubComposition(
334346
src: string,
335347
parent: DocScope,
336348
origin: number,
349+
slots: readonly HostSlot[],
337350
): Promise<ClipDraft[]> {
338351
const authored = resolve(parent.dir, src);
339352
const file = realFileInside(parent.projectDir, authored);
@@ -359,7 +372,13 @@ async function readSubComposition(
359372
const rows = await Promise.all(
360373
topLevelElements(toNode(root)).map((node) => describeRow(scope, node, 1)),
361374
);
362-
return rows.sort(byStart);
375+
return rows.flatMap((row) => cutRow(row, slots)).sort(byStart);
376+
}
377+
378+
function cutRow(row: ClipDraft, slots: readonly HostSlot[]): ClipDraft[] {
379+
const kept = cutToHostSlots({ start: row.absStart, end: row.absEnd }, slots);
380+
if (kept.end === kept.start && row.absEnd > row.absStart) return [];
381+
return [{ ...row, absStart: roundMs(kept.start), absEnd: roundMs(kept.end) }];
363382
}
364383

365384
/** The file's real path when it is a regular file inside the project (symlinks resolved), else null. */

‎packages/core/src/mediaTiming.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { readMediaOffsetSeconds } from "@hyperframes/parsers/media-duration";
2+
13
export const MEDIA_START_BASIS_ATTR = "data-hf-media-start-basis";
24

35
export type MediaStartBasis = "local" | "global";
@@ -20,8 +22,8 @@ export function resolveAbsoluteMediaStartSeconds(input: {
2022
}
2123

2224
/** The one rule for a media element's root-timeline start; the runtime and the CLI both call it.
23-
* With no literal start, an auto-injected start, or a host at t<=0 there is nothing for the basis
24-
* to disambiguate, so the ordinary start resolution applies. */
25+
* `hostStart` is the composition's origin. With no literal start, an auto-injected start, or an
26+
* origin at 0 there is nothing for the basis to disambiguate, so ordinary resolution applies. */
2527
export function resolveMediaStartSeconds(
2628
input: MediaStartInput & { ordinaryStart: () => number },
2729
): number {
@@ -48,5 +50,34 @@ export interface MediaStartInput {
4850
function hasLiteralNestedStart(
4951
input: MediaStartInput,
5052
): input is MediaStartInput & { authoredStart: number } {
51-
return !input.hasAutoStart && input.authoredStart != null && input.hostStart > 0;
53+
return !input.hasAutoStart && input.authoredStart != null && input.hostStart !== 0;
54+
}
55+
56+
/** A composition host's in-point; only a host with one shifts or cuts what it holds. */
57+
export function hostInPointSeconds(host: Pick<Element, "getAttribute">): number {
58+
return readMediaOffsetSeconds((name) => host.getAttribute(name));
59+
}
60+
61+
/** Main-timeline time of a sub-composition's local t=0: its host's start minus its in-point. */
62+
export function compositionOriginSeconds(
63+
hostStart: number,
64+
host: Pick<Element, "getAttribute">,
65+
): number {
66+
return hostStart - hostInPointSeconds(host);
67+
}
68+
69+
export interface HostSlot {
70+
start: number;
71+
end: number;
72+
}
73+
74+
/** Each in-point host cuts the span to its own window. */
75+
export function cutToHostSlots(
76+
span: { start: number; end: number },
77+
hosts: readonly HostSlot[],
78+
): { start: number; end: number } {
79+
return hosts.reduce((kept, host) => {
80+
const start = Math.max(kept.start, host.start);
81+
return { start, end: Math.max(start, Math.min(kept.end, host.end)) };
82+
}, span);
5283
}

‎packages/core/src/runtime/audioClockSource.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ afterEach(() => {
141141
});
142142

143143
describe("the audio the playhead follows", () => {
144+
it("does not hold the playhead on a clip an in-point host has not started yet", async () => {
145+
mount(
146+
`<div id="half" class="clip" data-composition-id="half" data-composition-file="compositions/scene.html"` +
147+
` data-start="5" data-duration="5" data-playback-start="5">` +
148+
`<div><audio id="a" data-start="2" data-duration="8" src="/assets/a.mp3"></audio></div></div>`,
149+
);
150+
const audio = document.getElementById("a") as HTMLAudioElement;
151+
Object.defineProperty(audio, "readyState", { value: 0, writable: true, configurable: true });
152+
initSandboxRuntimeModular();
153+
await flush();
154+
window.__player?.play();
155+
await flush();
156+
stepFrames(240);
157+
expect(window.__player!.getTime()).toBeGreaterThan(3.5);
158+
});
159+
144160
it("keeps following a playing voice when a clip earlier in the page starts late", async () => {
145161
mount(
146162
`<audio id="sfx" data-start="1" data-duration="2" src="/assets/sfx.mp3"></audio>` +

0 commit comments

Comments
 (0)