Skip to content

🎥 Make videos programmatically with React

chunk optimization progress

Jonny Burger393a95ced07b
diff --git a/packages/lambda/src/chunk-optimization/simulate-frame-ranges.ts b/packages/lambda/src/chunk-optimization/simulate-frame-ranges.ts
new file mode 100644
index 0000000000..9c684bbd79
--- /dev/null
+++ b/packages/lambda/src/chunk-optimization/simulate-frame-ranges.ts
@@ -0,0 +1,59 @@
+import {ChunkTimingData} from './types';
+
+export const getTimingForFrame = (
+ profile: ChunkTimingData[],
+ frame: number
+) => {
+ for (const timingInfo of profile) {
+ let lastTime = timingInfo.startDate;
+ for (const [fr, timing] of Object.entries(timingInfo.timings)) {
+ const actualFrame = timingInfo.frameRange[0] + Number(fr) - 1;
+ const absolute = timing + timingInfo.startDate;
+ if (actualFrame === frame) {
+ return absolute - lastTime;
+ }
+
+ lastTime = absolute;
+ }
+ }
+
+ throw new Error(`Frame ${frame} was not rendered`);
+};
+
+export const getSimulatedTimingForFrameRange = (
+ profile: ChunkTimingData[],
+ frameRange: [number, number]
+): ChunkTimingData['timings'] => {
+ const timings: ChunkTimingData['timings'] = {};
+
+ let totalDuration = 0;
+ let index = 1;
+ for (let i = frameRange[0]; i < frameRange[1]; i++) {
+ timings[index] = getTimingForFrame(profile, i) + totalDuration;
+ totalDuration += getTimingForFrame(profile, i);
+ index++;
+ }
+
+ return timings;
+};
+
+export const simulateFrameRanges = ({
+ profile,
+ newFrameRanges,
+}: {
+ profile: ChunkTimingData[];
+ newFrameRanges: [number, number][];
+}): ChunkTimingData[] => {
+ if (profile.length !== newFrameRanges.length) {
+ throw new Error('Expected previous and new frame ranges to be equal');
+ }
+
+ return newFrameRanges.map((range, i) => {
+ return {
+ timings: getSimulatedTimingForFrameRange(profile, range),
+ chunk: i,
+ frameRange: range,
+ startDate: profile[i].startDate,
+ };
+ });
+};
diff --git a/packages/lambda/src/test/optimize-profile.test.ts b/packages/lambda/src/test/optimize-profile.test.ts
index 13dcd5bfed..e822f4f5f5 100644
--- a/packages/lambda/src/test/optimize-profile.test.ts
+++ b/packages/lambda/src/test/optimize-profile.test.ts
@@ -1,6 +1,45 @@
import {getProfileDuration} from '../chunk-optimization/get-profile-duration';
+import {
+ getSimulatedTimingForFrameRange,
+ getTimingForFrame,
+} from '../chunk-optimization/simulate-frame-ranges';
import {demoProfiles} from './demo-profile';
test('Should measure demo profile correctly', () => {
expect(getProfileDuration(demoProfiles)).toEqual(24440);
});
+
+test('Should get correct duration for a frame', () => {
+ expect(getTimingForFrame(demoProfiles, 19)).toEqual(
+ demoProfiles[0].timings[20] - demoProfiles[0].timings[19]
+ );
+ expect(getTimingForFrame(demoProfiles, 19)).toEqual(103);
+ expect(getTimingForFrame(demoProfiles, 20)).toEqual(
+ demoProfiles[1].timings[1]
+ );
+ expect(getTimingForFrame(demoProfiles, 20)).toEqual(1121);
+});
+
+test('Get simulated profile for frame range', () => {
+ expect(getSimulatedTimingForFrameRange(demoProfiles, [10, 29])).toEqual({
+ '1': 82,
+ '2': 183,
+ '3': 266,
+ '4': 349,
+ '5': 432,
+ '6': 515,
+ '7': 599,
+ '8': 682,
+ '9': 765,
+ '10': 868,
+ '11': 1989,
+ '12': 2087,
+ '13': 2220,
+ '14': 2322,
+ '15': 2437,
+ '16': 2536,
+ '17': 2619,
+ '18': 2735,
+ '19': 2819,
+ });
+});