🎥 Make videos programmatically with React
query optimizer, but has a bottleneck
Jonny Burgerdb4f734351d8
diff --git a/packages/lambda/src/chunk-optimization/get-frame-ranges-from-profile.ts b/packages/lambda/src/chunk-optimization/get-frame-ranges-from-profile.tsnew file mode 100644index 0000000000..2e5886e14a--- /dev/null+++ b/packages/lambda/src/chunk-optimization/get-frame-ranges-from-profile.ts@@ -0,0 +1,11 @@+import {ChunkTimingData} from './types';++export const getFrameRangesFromProfile = (+ profile: ChunkTimingData[]+): [number, number][] => {+ return profile.map((p) => p.frameRange);+};++export const sortProfileByFrameRanges = (profile: ChunkTimingData[]) => {+ return profile.slice().sort((a, b) => a.frameRange[0] - b.frameRange[0]);+};diff --git a/packages/lambda/src/chunk-optimization/optimize-profile.ts b/packages/lambda/src/chunk-optimization/optimize-profile.tsindex 91779b1fbe..bec3bde2db 100644--- a/packages/lambda/src/chunk-optimization/optimize-profile.ts+++ b/packages/lambda/src/chunk-optimization/optimize-profile.ts@@ -1,5 +1,82 @@+import {getFrameRangesFromProfile} from './get-frame-ranges-from-profile';+import {getTimingEndTimestamps} from './get-profile-duration';+import {simulateFrameRanges} from './simulate-frame-ranges';import {ChunkTimingData} from './types';+export const assignFrameToOther = ({+ frameRanges,+ fromChunk,+ toChunk,+}: {+ frameRanges: [number, number][];+ fromChunk: number;+ toChunk: number;+}): [number, number][] => {+ if (fromChunk < toChunk) {+ return frameRanges.map((frameRange, i) => {+ if (i === fromChunk) {+ return [frameRange[0], frameRange[1] - 1];+ }++ if (i === toChunk) {+ return [frameRange[0] - 1, frameRange[1]];+ }++ if (i > fromChunk && i < toChunk) {+ return [frameRange[0] - 1, frameRange[1] - 1];+ }++ return frameRange;+ });+ }++ return frameRanges.map((frameRange, i) => {+ if (i === fromChunk) {+ return [frameRange[0] + 1, frameRange[1]];+ }++ if (i === toChunk) {+ return [frameRange[0], frameRange[1] + 1];+ }++ if (i > toChunk && i < fromChunk) {+ return [frameRange[0] + 1, frameRange[1] + 1];+ }++ return frameRange;+ });+};+export const optimizeProfile = (profile: ChunkTimingData[]) => {- return profile;+ const sortedByDuration = profile.slice().sort((a, b) => {+ const aTimestamps = getTimingEndTimestamps(a);+ const bTimestamps = getTimingEndTimestamps(b);++ const aDuration = Math.max(...aTimestamps) - Math.min(...aTimestamps);+ const bDuration = Math.max(...bTimestamps) - Math.min(...bTimestamps);++ return aDuration - bDuration;+ });+ const indexOfFastest = profile.indexOf(sortedByDuration[0]);+ if (indexOfFastest === -1) {+ throw new Error('something went wrong');+ }++ const indexOfSlowest = profile.indexOf(+ sortedByDuration[sortedByDuration.length - 1]+ );+ if (indexOfSlowest === -1) {+ throw new Error('something went wrong');+ }++ const frameRanges = getFrameRangesFromProfile(profile);+ const newFrameRanges = assignFrameToOther({+ frameRanges,+ fromChunk: indexOfSlowest,+ toChunk: indexOfFastest,+ });+ return simulateFrameRanges({+ profile,+ newFrameRanges,+ });};diff --git a/packages/lambda/src/test/optimize-profile.test.ts b/packages/lambda/src/test/optimize-profile.test.tsindex aaa6900597..f96e47a5a8 100644--- a/packages/lambda/src/test/optimize-profile.test.ts+++ b/packages/lambda/src/test/optimize-profile.test.ts@@ -1,4 +1,12 @@+import {+ getFrameRangesFromProfile,+ sortProfileByFrameRanges,+} from '../chunk-optimization/get-frame-ranges-from-profile';import {getProfileDuration} from '../chunk-optimization/get-profile-duration';+import {+ assignFrameToOther,+ optimizeProfile,+} from '../chunk-optimization/optimize-profile';import {getSimulatedTimingForFrameRange,getTimingForFrame,@@ -49,7 +57,7 @@ test('Get simulated profile for frame range', () => {test('Parser should not lose precision, same duration after parsing and reconstruction', () => {const reconstructed = simulateFrameRanges({profile: demoProfiles,- newFrameRanges: demoProfiles.map((p) => p.frameRange),+ newFrameRanges: getFrameRangesFromProfile(demoProfiles),});expect(getProfileDuration(reconstructed)).toEqual(24440);@@ -57,3 +65,41 @@ test('Parser should not lose precision, same duration after parsing and reconstrgetProfileDuration(demoProfiles));});++describe('Move frame from 1 frame range to another', () => {+ const frameRanges: [number, number][] = [+ [0, 19],+ [20, 39],+ [40, 59],+ ];+ test('Case from < to', () => {+ expect(assignFrameToOther({frameRanges, fromChunk: 0, toChunk: 2})).toEqual(+ [+ [0, 18],+ [19, 38],+ [39, 59],+ ]+ );+ });+ test('Case from > to', () => {+ expect(assignFrameToOther({frameRanges, fromChunk: 2, toChunk: 0})).toEqual(+ [+ [0, 20],+ [21, 40],+ [41, 59],+ ]+ );+ });+});++test('Optimize profile', () => {+ const sortedProfile = sortProfileByFrameRanges(demoProfiles);+ let optimized = sortedProfile;+ for (let i = 0; i < 10; i++) {+ optimized = optimizeProfile(optimized);+ }++ const newDuration = getProfileDuration(optimized);+ console.log(newDuration);+ expect(newDuration).toBeLessThan(20000);+});