Files
hyperframes/packages/cli/src/utils/mediaUsedSeconds.ts
T
Miguel Ángel c6f87f59b0 fix(cli): check copies only the stretch of a video it plays and stops after 2 minutes of copies (#7182)
* fix(cli): check stops after 2 minutes of video copies and says how to finish

* fix(cli): check copies only the stretch of a video the composition plays

* docs(cli): check copies only the played stretch of a long video

* docs(cli): shorten two comments on the copy window

* fix(cli): finding crops and frames get the same copy windows as the check

* fix(cli): drop a comment the ratchet counts

* refactor(studio-server): keep runFfmpeg under the complexity gate
2026-11-04 21:16:31 +00:00

53 lines
1.9 KiB
TypeScript

import { parseHTML } from "linkedom";
const number = (value: string | null | undefined): number | undefined => {
const parsed = Number(value);
return value !== null && value !== undefined && value.trim() !== "" && Number.isFinite(parsed)
? parsed
: undefined;
};
/** `clip.mov?x#y`, `./clip.mov` and `/clip.mov` all name the file `/clip.mov`. */
function pathnameOf(src: string): string {
const bare = src
.replace(/[?#].*$/, "")
.replace(/^\.\//, "")
.replace(/^\/+/, "");
try {
return `/${decodeURIComponent(bare)}`;
} catch {
return `/${bare}`;
}
}
function plannedDuration(video: Element): number | undefined {
for (let node: Element | null = video; node; node = node.parentElement) {
const duration = number(node.getAttribute("data-duration"));
if (duration !== undefined) return duration;
}
return undefined;
}
/** Seconds from the start of each file the composition plays, by pathname; a file with any unknown use is left out. */
export function mediaUsedSeconds(html: string): Map<string, number> {
const { document } = parseHTML(html);
const used = new Map<string, number>();
const whole = new Set<string>();
for (const element of document.querySelectorAll("[src]")) {
const pathname = pathnameOf(element.getAttribute("src") ?? "");
const video = element.tagName === "VIDEO" ? element : null;
const duration = video ? plannedDuration(video) : undefined;
if (!video || duration === undefined || video.hasAttribute("loop")) {
whole.add(pathname);
continue;
}
const rate = Math.max(1, number(video.getAttribute("data-playback-rate")) ?? 1);
const offset =
(number(video.getAttribute("data-media-start")) ?? 0) +
(number(video.getAttribute("data-playback-start")) ?? 0);
used.set(pathname, Math.max(used.get(pathname) ?? 0, offset + duration * rate));
}
for (const pathname of whole) used.delete(pathname);
return used;
}