diff --git a/packages/producer/src/services/render/videoShownBoxes.test.ts b/packages/producer/src/services/render/videoShownBoxes.test.ts index 6901f68909..c4cfa538d4 100644 --- a/packages/producer/src/services/render/videoShownBoxes.test.ts +++ b/packages/producer/src/services/render/videoShownBoxes.test.ts @@ -70,6 +70,11 @@ describe("shownBoxTimes", () => { expect(shownBoxTimes([video("a", 2 ** 53, 2 ** 54)], fps, 10)).toEqual([]); expect(shownBoxTimes([video("a", Number.NaN, 1)], fps, 10)).toEqual([]); }); + + it("puts every video's strides on one grid, so many offset videos add only their own first and last frames", () => { + const offset = Array.from({ length: 1000 }, (_, i) => video(`v${i}`, i / 1000, 10)); + expect(shownBoxTimes(offset, fps, 10).length).toBeLessThanOrEqual(10 / 0.25 + 2 * 1000); + }); }); describe("measureLocalShownBoxes", () => { diff --git a/packages/producer/src/services/render/videoShownBoxes.ts b/packages/producer/src/services/render/videoShownBoxes.ts index 84f0cac8cd..22ea557af6 100644 --- a/packages/producer/src/services/render/videoShownBoxes.ts +++ b/packages/producer/src/services/render/videoShownBoxes.ts @@ -35,7 +35,7 @@ export async function oversizedVideos( return videos.filter((_, index) => sized[index]); } -/** Each video's first and last frame before `duration` and every stride between, in seconds, ascending. */ +/** Each video's first and last frame before `duration`, and the one stride grid all videos share between. */ export function shownBoxTimes( videos: readonly VideoElement[], fps: { num: number; den: number }, @@ -47,8 +47,13 @@ export function shownBoxTimes( const end = Math.min(authoredEnd, duration); if (!Number.isFinite(start) || !Number.isFinite(end) || start > end) continue; const last = Math.max(start, end - frame); - for (let step = 0; start + step * SHOWN_BOX_STRIDE_S < last; step += 1) { - times.add(start + step * SHOWN_BOX_STRIDE_S); + times.add(start); + for ( + let step = Math.floor(start / SHOWN_BOX_STRIDE_S) + 1; + step * SHOWN_BOX_STRIDE_S < last; + step += 1 + ) { + times.add(step * SHOWN_BOX_STRIDE_S); } times.add(last); }