Files
hyperframes/packages/engine/src/services/extractedFrameIndex.ts
T
Miguel Ángel 69738b7ce4 fix(engine): render into a folder whose name holds a percent sign (#6116)
* fix(engine): render into a folder whose name holds a percent sign

ffmpeg reads an image sequence path as a printf template, so a folder like
"12% Up" was parsed as a format code and every video render failed. The
folder part of each frame-sequence path now goes through one helper that
doubles the percent sign.

* fix(engine): drop the helper doc line the comment ratchet rejects
2026-10-23 07:33:25 +00:00

62 lines
2.0 KiB
TypeScript

import { readdirSync } from "node:fs";
import { join } from "node:path";
export type ExtractedFrameFormat = "jpg" | "png";
export const FRAME_FILENAME_PREFIX = "frame_";
export function frameSequencePath(dir: string, framePattern: string): string {
return join(dir.replaceAll("%", "%%"), framePattern);
}
export class ExtractedFrameSequenceError extends Error {
constructor(message: string) {
super(message);
this.name = "ExtractedFrameSequenceError";
}
}
export function extractedFrameIndex(file: string, format: ExtractedFrameFormat): number {
const match = new RegExp(`^${FRAME_FILENAME_PREFIX}(\\d+)\\.${format}$`).exec(file);
if (!match) {
throw new ExtractedFrameSequenceError(`Invalid extracted frame filename: ${file}`);
}
const ordinal = Number.parseInt(match[1]!, 10);
if (!Number.isSafeInteger(ordinal) || ordinal < 1) {
throw new ExtractedFrameSequenceError(`Invalid extracted frame ordinal: ${file}`);
}
return ordinal - 1;
}
export function framePathsFromDirectory(
outputDir: string,
format: ExtractedFrameFormat,
allowGaps = false,
): Map<number, string> {
const suffix = `.${format}`;
const indexed = new Map<number, string>();
for (const file of readdirSync(outputDir)) {
if (!file.startsWith(FRAME_FILENAME_PREFIX) || !file.endsWith(suffix)) continue;
const index = extractedFrameIndex(file, format);
if (indexed.has(index)) {
throw new ExtractedFrameSequenceError(
`Duplicate extracted frame index ${index}: ${indexed.get(index)} and ${file}`,
);
}
indexed.set(index, join(outputDir, file));
}
if (allowGaps) return new Map([...indexed].sort(([a], [b]) => a - b));
const ordered = new Map<number, string>();
for (let index = 0; index < indexed.size; index += 1) {
const path = indexed.get(index);
if (!path) {
throw new ExtractedFrameSequenceError(
`Missing extracted frame index ${index} in ${outputDir}`,
);
}
ordered.set(index, path);
}
return ordered;
}