Skip to content

Write HTML. Render video. Built for agents.

fix(cli): honor authored durations when listing compositions (#5265)

Abdullah M. Nazif1094b8ef0e76
diff --git a/packages/cli/src/commands/compositions.test.ts b/packages/cli/src/commands/compositions.test.ts
index f340b94bbc..07070f9e3b 100644
--- a/packages/cli/src/commands/compositions.test.ts
+++ b/packages/cli/src/commands/compositions.test.ts
@@ -10,6 +10,29 @@ describe("parseCompositions", () => {
ensureDOMParser();
});
+ it.each([
+ { declared: "8", child: "2", expected: 8 },
+ { declared: "2.5", child: "7", expected: 2.5 },
+ { declared: "3", child: null, expected: 3 },
+ ])("honors the declared inline duration $declared", ({ declared, child, expected }) => {
+ const html = `<div data-composition-id="host" data-duration="${declared}">
+ ${child === null ? "" : `<div class="clip" data-start="0" data-duration="${child}"></div>`}
+ </div>`;
+
+ expect(parseCompositions(html, ".")[0]?.duration).toBe(expected);
+ });
+
+ it.each([null, "0", "-1", "Infinity", "invalid", "4junk"])(
+ "derives the inline duration when %s is not a valid declared duration",
+ (declared) => {
+ const html = `<div data-composition-id="host" ${declared === null ? "" : `data-duration="${declared}"`}>
+ <div class="clip" data-start="1" data-duration="2"></div>
+ </div>`;
+
+ expect(parseCompositions(html, ".")[0]?.duration).toBe(3);
+ },
+ );
+
it("resolves relative sub-composition starts when computing host duration", () => {
const baseDir = mkdtempSync(join(tmpdir(), "hyperframes-compositions-"));
@@ -47,6 +70,29 @@ describe("parseSubComposition", () => {
ensureDOMParser();
});
+ it.each([false, true])(
+ "honors an external composition trim with a template wrapper: %s",
+ (wrapped) => {
+ const content = `<div data-composition-id="scene" data-duration="2.5">
+ <div class="clip" data-start="0" data-duration="7"></div>
+ </div>`;
+ const html = wrapped ? `<template>${content}</template>` : content;
+
+ expect(parseSubComposition(html, "fallback", 1920, 1080).duration).toBe(2.5);
+ },
+ );
+
+ it.each(["0", "-1", "Infinity", "invalid", "4junk"])(
+ "derives the external duration when %s is not a valid declared duration",
+ (declared) => {
+ const html = `<template><div data-composition-id="scene" data-duration="${declared}">
+ <div class="clip" data-start="1" data-duration="2"></div>
+ </div></template>`;
+
+ expect(parseSubComposition(html, "fallback", 1920, 1080).duration).toBe(3);
+ },
+ );
+
it("reads template-wrapped sub-composition contents", () => {
const html = `
<template id="foo-template">
diff --git a/packages/cli/src/commands/compositions.ts b/packages/cli/src/commands/compositions.ts
index a15cbdd554..4e2242820a 100644
--- a/packages/cli/src/commands/compositions.ts
+++ b/packages/cli/src/commands/compositions.ts
@@ -2,6 +2,8 @@ import { defineCommand } from "citty";
import type { Example } from "./_examples.js";
import { readFileSync } from "node:fs";
import { readProjectFile } from "@hyperframes/parsers/asset-resolution";
+import { resolveCompositionDuration } from "@hyperframes/parsers/composition-duration";
+import { readDataDurationSeconds } from "@hyperframes/parsers/media-duration";
import { resolve, dirname } from "node:path";
export const examples: Example[] = [
@@ -74,7 +76,7 @@ export function parseCompositions(html: string, baseDir: string): CompositionInf
}
const timedChildren = div.querySelectorAll("[data-start]");
- let maxEnd = 0;
+ const clipEndsSeconds: number[] = [];
let elementCount = 0;
timedChildren.forEach((el) => {
@@ -92,14 +94,18 @@ export function parseCompositions(html: string, baseDir: string): CompositionInf
end = start + 5;
}
- if (end > maxEnd) {
- maxEnd = end;
- }
+ clipEndsSeconds.push(end);
});
+ const duration =
+ resolveCompositionDuration({
+ authoredDurationSeconds: readDataDurationSeconds((name) => div.getAttribute(name)),
+ clipEndsSeconds,
+ }).seconds ?? 0;
+
compositions.push({
id,
- duration: maxEnd,
+ duration,
width,
height,
elementCount,
@@ -135,14 +141,7 @@ export function parseSubComposition(
elementCount = countRenderableDescendants(compDiv);
}
- // Parse duration from the composition's own data-duration attribute
- let duration = 0;
- const durationAttr = compDiv?.getAttribute("data-duration");
- if (durationAttr && !durationAttr.startsWith("__")) {
- duration = parseFloat(durationAttr) || 0;
- }
-
- // Also check timed children for max end time
+ const clipEndsSeconds: number[] = [];
if (compDiv) {
const timedEls = compDiv.querySelectorAll("[data-start]");
const startCache = new Map<Element, number>();
@@ -161,11 +160,14 @@ export function parseSubComposition(
} else {
end = start + 5;
}
- if (end > duration) {
- duration = end;
- }
+ clipEndsSeconds.push(end);
});
}
+ let duration =
+ resolveCompositionDuration({
+ authoredDurationSeconds: readDataDurationSeconds((name) => compDiv?.getAttribute(name)),
+ clipEndsSeconds,
+ }).seconds ?? 0;
if (duration <= 0) {
duration = estimateDurationFromScripts(searchRoot);
}