Skip to content

Commit 297a394

Browse files
committed
fix: skip invisible video extraction windows
1 parent ba55214 commit 297a394

4 files changed

Lines changed: 64 additions & 21 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ describe("resolveVideoExtractionDuration", () => {
9595
expect(resolveVideoExtractionDuration(preroll, metadata(120), 2)).toBe(2);
9696
});
9797

98+
it("returns an empty window for a clip entirely before composition time zero", () => {
99+
expect(resolveVideoExtractionWindow(video({ start: -60, end: -10 }), metadata(120), 2)).toEqual(
100+
{ compositionStart: 0, mediaStart: 60, durationSeconds: 0 },
101+
);
102+
});
103+
98104
it("retains legacy behavior when no timeline end is supplied", () => {
99105
expect(resolveVideoExtractionDuration(video(), metadata(60))).toBe(60);
100106
});
@@ -1072,6 +1078,28 @@ describe.skipIf(!HAS_FFMPEG)("extractAllVideoFrames on a VFR source", () => {
10721078
if (existsSync(FIXTURE_DIR)) rmSync(FIXTURE_DIR, { recursive: true, force: true });
10731079
});
10741080

1081+
it("skips a clip entirely before time zero without reporting an extraction error", async () => {
1082+
const outputDir = join(FIXTURE_DIR, "out-before-timeline");
1083+
mkdirSync(outputDir, { recursive: true });
1084+
const video: VideoElement = {
1085+
id: "before-timeline",
1086+
src: VFR_FIXTURE,
1087+
start: -2,
1088+
end: -1,
1089+
mediaStart: 0,
1090+
loop: false,
1091+
hasAudio: false,
1092+
};
1093+
1094+
const result = await extractAllVideoFrames([video], FIXTURE_DIR, {
1095+
fps: 1,
1096+
outputDir,
1097+
timelineEnd: 2,
1098+
});
1099+
1100+
expect(result).toMatchObject({ success: true, extracted: [], errors: [] });
1101+
});
1102+
10751103
it("detects the synthesized fixture as VFR", async () => {
10761104
const md = await extractVideoMetadata(VFR_FIXTURE);
10771105
expect(md.isVFR).toBe(true);

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,10 @@ export async function extractAllVideoFrames(
13601360
};
13611361
}
13621362

1363-
type PreparedExtractionResult = { work: PreparedExtraction } | { error: VideoExtractionFailure };
1363+
type PreparedExtractionResult =
1364+
| { work: PreparedExtraction }
1365+
| { error: VideoExtractionFailure }
1366+
| { skipped: true };
13641367

13651368
type ExtractionOutcome = { result: ExtractedFrames } | { error: VideoExtractionFailure };
13661369

@@ -1585,7 +1588,7 @@ export async function extractAllVideoFrames(
15851588
const window = resolveVideoExtractionWindow(video, metadata, options.timelineEnd);
15861589
const videoDuration = window.durationSeconds;
15871590
if (videoDuration <= 0) {
1588-
throw new Error(`Video "${video.id}" has no interval inside the render timeline`);
1591+
return { skipped: true };
15891592
}
15901593
video.start = window.compositionStart;
15911594
video.end = window.compositionStart + videoDuration;
@@ -1649,11 +1652,20 @@ export async function extractAllVideoFrames(
16491652
for (const [key, outcome] of groupOutcomes) uniqueOutcomes.set(key, outcome);
16501653
}
16511654

1652-
const results: ExtractionOutcome[] = preparedExtractions.map((prepared) => {
1653-
if ("error" in prepared) return prepared;
1655+
const results: ExtractionOutcome[] = [];
1656+
for (const prepared of preparedExtractions) {
1657+
if ("skipped" in prepared) continue;
1658+
if ("error" in prepared) {
1659+
results.push(prepared);
1660+
continue;
1661+
}
16541662
const outcome = uniqueOutcomes.get(prepared.work.dedupeKey);
1655-
if (!outcome)
1656-
return { error: extractionError(prepared.work.video.id, "missing extraction result") };
1663+
if (!outcome) {
1664+
results.push({
1665+
error: extractionError(prepared.work.video.id, "missing extraction result"),
1666+
});
1667+
continue;
1668+
}
16571669
if ("error" in outcome) {
16581670
// A shared (deduped/superset) failure fans out to every element with the
16591671
// same key; annotate followers with the leader's videoId so N copies of
@@ -1662,17 +1674,18 @@ export async function extractAllVideoFrames(
16621674
const message = isFollower
16631675
? `[shared extraction, leader ${outcome.error.videoId}] ${outcome.error.error}`
16641676
: outcome.error.error;
1665-
return {
1677+
results.push({
16661678
error: {
16671679
videoId: prepared.work.video.id,
16681680
kind: outcome.error.kind,
16691681
retryable: outcome.error.retryable,
16701682
error: message,
16711683
},
1672-
};
1684+
});
1685+
continue;
16731686
}
1674-
return { result: { ...outcome.result, videoId: prepared.work.video.id } };
1675-
});
1687+
results.push({ result: { ...outcome.result, videoId: prepared.work.video.id } });
1688+
}
16761689

16771690
breakdown.extractMs = Date.now() - phase3Start;
16781691

‎packages/producer/src/services/render/stages/captureHdrResources.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,12 @@ describe("estimateHdrExtractionBytes", () => {
112112

113113
describe("resolveHdrExtractionWindow", () => {
114114
it("bounds a two-second composition backed by an unbounded HDR source to 60 raw frames", () => {
115-
const { durationSeconds } = resolveHdrExtractionWindow(
115+
const window = resolveHdrExtractionWindow(
116116
{ id: "long-hdr", start: 0, end: Number.POSITIVE_INFINITY, mediaStart: 0 },
117117
2,
118118
);
119+
if (!window) throw new Error("expected a visible HDR extraction window");
120+
const { durationSeconds } = window;
119121
expect(durationSeconds).toBe(2);
120122
expect(estimateHdrExtractionBytes([{ durationSeconds, width: 3840, height: 2160 }], 30)).toBe(
121123
60 * 3840 * 2160 * 6,
@@ -136,10 +138,10 @@ describe("resolveHdrExtractionWindow", () => {
136138
).toEqual({ compositionStart: 0, mediaStart: 60, durationSeconds: 2 });
137139
});
138140

139-
it("rejects HDR media with no interval inside the composition", () => {
140-
expect(() =>
141+
it("skips HDR media with no interval inside the composition", () => {
142+
expect(
141143
resolveHdrExtractionWindow({ id: "hdr", start: 3, end: Infinity, mediaStart: 0 }, 2),
142-
).toThrow('HDR video "hdr" has no finite interval inside the 2s composition');
144+
).toBeNull();
143145
});
144146
});
145147

‎packages/producer/src/services/render/stages/captureHdrResources.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ export type HdrExtractionWindow = TimelineExtractionWindow;
267267
export function resolveHdrExtractionWindow(
268268
video: { id: string; start: number; end: number; mediaStart: number },
269269
compositionDuration: number,
270-
): HdrExtractionWindow {
270+
): HdrExtractionWindow | null {
271271
if (!Number.isFinite(compositionDuration) || compositionDuration <= 0) {
272272
throw new Error(
273273
`Cannot extract HDR video "${video.id}" with invalid composition duration ${String(compositionDuration)}`,
@@ -280,16 +280,15 @@ export function resolveHdrExtractionWindow(
280280
requestedEnd - video.start,
281281
compositionDuration,
282282
);
283-
if (
284-
!Number.isFinite(window.durationSeconds) ||
285-
window.durationSeconds <= 0 ||
286-
!Number.isFinite(window.mediaStart) ||
287-
window.mediaStart < 0
288-
) {
283+
if (!Number.isFinite(window.durationSeconds)) {
289284
throw new Error(
290285
`HDR video "${video.id}" has no finite interval inside the ${compositionDuration}s composition`,
291286
);
292287
}
288+
if (window.durationSeconds <= 0) return null;
289+
if (!Number.isFinite(window.mediaStart) || window.mediaStart < 0) {
290+
throw new Error(`HDR video "${video.id}" has invalid mediaStart ${String(window.mediaStart)}`);
291+
}
293292
return window;
294293
}
295294

@@ -414,6 +413,7 @@ export async function extractHdrVideoFrames(args: {
414413
if (!video) continue;
415414
const dims = prep.hdrExtractionDims.get(videoId) ?? { width, height };
416415
const window = resolveHdrExtractionWindow(video, composition.duration);
416+
if (!window) continue;
417417
extractionWindows.set(videoId, window);
418418
prep.hdrVideoStartTimes.set(videoId, window.compositionStart);
419419
plannedVideos.push({

0 commit comments

Comments
 (0)