Skip to content

Commit 127eb19

Browse files
authored
fix(producer): correct short VFR frame coverage (#2936)
* fix(producer): correct short VFR frame coverage * fix(engine): keep VFR extraction seek-local * fix(engine): match ffmpeg decimal frame boundaries * fix(engine): preserve exact extraction frame rates * fix(engine): key frame cache by exact rate
1 parent 9792c32 commit 127eb19

9 files changed

Lines changed: 543 additions & 42 deletions

File tree

‎packages/engine/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export {
185185
resolveFinalFrameExtractionWindow,
186186
resolveVideoExtractionDuration,
187187
resolvePlayableVideoDuration,
188+
extractionFrameCountForDuration,
188189
resolveProjectRelativeSrc,
189190
getFrameAtTime,
190191
createFrameLookupTable,

‎packages/engine/src/services/extractionCache.test.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const keyFor = (videoPath: string, overrides: Partial<CacheKeyInput> = {}): Cach
3737
size: stat.size,
3838
mediaStart: 0,
3939
duration: 3,
40-
fps: 30,
40+
fps: "30",
4141
format: "jpg",
4242
...overrides,
4343
};
@@ -63,8 +63,8 @@ function seedPartialDir(entry: { dir: string; keyHash: string }, frameContent: s
6363
}
6464

6565
describe("extractionCache constants", () => {
66-
it("exposes the v2 schema prefix", () => {
67-
expect(SCHEMA_PREFIX).toBe("hfcache-v3-");
66+
it("exposes the v4 schema prefix", () => {
67+
expect(SCHEMA_PREFIX).toBe("hfcache-v4-");
6868
});
6969

7070
it("exposes the frame filename prefix shared with the extractor", () => {
@@ -123,10 +123,16 @@ describe("computeCacheKey", () => {
123123

124124
it("changes when fps changes (different frame count invalidates key)", () => {
125125
const a = computeCacheKey(base(sourceFile));
126-
const b = computeCacheKey({ ...base(sourceFile), fps: 60 });
126+
const b = computeCacheKey({ ...base(sourceFile), fps: "60" });
127127
expect(a).not.toBe(b);
128128
});
129129

130+
it("keeps exact rational rates distinct from their JavaScript decimal", () => {
131+
const rational = computeCacheKey({ ...base(sourceFile), fps: "30000/1001" });
132+
const decimal = computeCacheKey({ ...base(sourceFile), fps: String(30000 / 1001) });
133+
expect(rational).not.toBe(decimal);
134+
});
135+
130136
it("changes when format changes", () => {
131137
const a = computeCacheKey(base(sourceFile));
132138
const b = computeCacheKey({ ...base(sourceFile), format: "png" });
@@ -200,6 +206,18 @@ describe("lookupCacheEntry / markCacheEntryComplete", () => {
200206

201207
const base = (videoPath: string): CacheKeyInput => keyFor(videoPath);
202208

209+
it("does not reuse a complete entry from the v3 numeric-fps namespace", () => {
210+
const input = base(sourceFile);
211+
const keyHash = computeCacheKey(input);
212+
const staleV3Dir = join(tmpRoot, `hfcache-v3-${keyHash.slice(0, 16)}`);
213+
mkdirSync(staleV3Dir, { recursive: true });
214+
writeFileSync(join(staleV3Dir, COMPLETE_SENTINEL), "", "utf-8");
215+
216+
const lookup = lookupCacheEntry(tmpRoot, input);
217+
expect(lookup.hit).toBe(false);
218+
expect(lookup.entry.dir).toBe(join(tmpRoot, `${SCHEMA_PREFIX}${keyHash.slice(0, 16)}`));
219+
});
220+
203221
it("misses on an empty cache root", () => {
204222
const lookup = lookupCacheEntry(tmpRoot, base(sourceFile));
205223
expect(lookup.hit).toBe(false);

‎packages/engine/src/services/extractionCache.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ export const GC_MARKER = ".hf-last-gc";
6262
* VFR-to-CFR re-encode, changing frame contents for VFR sources under
6363
* identical key tuples. Without the bump, warm v2 entries (two-pass frames)
6464
* would keep being served across the deploy boundary.
65+
* v3 -> v4: the target fps identity is the exact FFmpeg argument instead of
66+
* a JavaScript number. This invalidates entries created after rational NTSC
67+
* rates had already been rounded to a decimal.
6568
*/
66-
export const SCHEMA_PREFIX = "hfcache-v3-";
69+
export const SCHEMA_PREFIX = "hfcache-v4-";
6770

6871
/** Truncated hex chars of SHA-256 used for the entry directory name. */
6972
const KEY_HEX_CHARS = 16;
@@ -84,8 +87,8 @@ export interface CacheKeyInput {
8487
* so callers that pass an unresolved "natural duration" still produce a
8588
* stable key across invocations. */
8689
duration: number;
87-
/** Target output frames-per-second. */
88-
fps: number;
90+
/** Exact target output frame-rate argument (for example `30000/1001`). */
91+
fps: string;
8992
/** Output image format. */
9093
format: CacheFrameFormat;
9194
/** Optional source transform applied during extraction. */
@@ -136,7 +139,7 @@ function canonicalKeyBlob(input: CacheKeyInput): string {
136139
s: number;
137140
ms: number;
138141
d: number;
139-
f: number;
142+
f: string;
140143
fmt: CacheFrameFormat;
141144
t?: string;
142145
} = {

0 commit comments

Comments
 (0)