🎥 Make videos programmatically with React
`@remotion/media`: Keep at least 1 keyframe bank always
JonnyBurgerf9bd166cce48
diff --git a/packages/media/src/video-extraction/keyframe-bank.ts b/packages/media/src/video-extraction/keyframe-bank.tsindex fd3c871deb..e1f0a7a0d6 100644--- a/packages/media/src/video-extraction/keyframe-bank.ts+++ b/packages/media/src/video-extraction/keyframe-bank.ts@@ -6,7 +6,7 @@ export type KeyframeBank = {startTimestampInSeconds: number;endTimestampInSeconds: number;getFrameFromTimestamp: (timestamp: number) => Promise<VideoSample | null>;- prepareForDeletion: (logLevel: LogLevel) => void;+ prepareForDeletion: (logLevel: LogLevel) => {framesDeleted: number};deleteFramesBeforeTimestamp: ({logLevel,src,@@ -155,6 +155,8 @@ export const makeKeyframeBank = ({return null;});+ let framesDeleted = 0;+for (const frameTimestamp of frameTimestamps) {if (!frames[frameTimestamp]) {continue;@@ -163,9 +165,11 @@ export const makeKeyframeBank = ({allocationSize -= frames[frameTimestamp].allocationSize();frames[frameTimestamp].close();delete frames[frameTimestamp];+ framesDeleted++;}frameTimestamps.length = 0;+ return {framesDeleted};};const deleteFramesBeforeTimestamp = ({@@ -228,10 +232,7 @@ export const makeKeyframeBank = ({queue = queue.then(() => getFrameFromTimestamp(timestamp));return queue as Promise<VideoSample | null>;},- prepareForDeletion: (logLevel: LogLevel) => {- queue = queue.then(() => prepareForDeletion(logLevel));- return queue as Promise<void>;- },+ prepareForDeletion,hasTimestampInSecond,addFrame,deleteFramesBeforeTimestamp,diff --git a/packages/media/src/video-extraction/keyframe-manager.ts b/packages/media/src/video-extraction/keyframe-manager.tsindex 38082af78c..078fd4cfc1 100644--- a/packages/media/src/video-extraction/keyframe-manager.ts+++ b/packages/media/src/video-extraction/keyframe-manager.ts@@ -77,6 +77,8 @@ export const makeKeyframeManager = () => {let mostInThePast = null;let mostInThePastBank = null;+ let numberOfBanks = 0;+for (const src in sources) {for (const b in sources[src]) {const bank = await sources[src][b];@@ -86,6 +88,8 @@ export const makeKeyframeManager = () => {mostInThePast = lastUsed;mostInThePastBank = {src, bank};}++ numberOfBanks++;}}@@ -93,31 +97,54 @@ export const makeKeyframeManager = () => {throw new Error('No keyframe bank found');}- return mostInThePastBank;+ return {mostInThePastBank, numberOfBanks};};- const deleteOldestKeyframeBank = async (logLevel: LogLevel) => {- const {bank: mostInThePastBank, src: mostInThePastSrc} =- await getTheKeyframeBankMostInThePast();+ const deleteOldestKeyframeBank = async (+ logLevel: LogLevel,+ ): Promise<{finish: boolean}> => {+ const {+ mostInThePastBank: {bank: mostInThePastBank, src: mostInThePastSrc},+ numberOfBanks,+ } = await getTheKeyframeBankMostInThePast();++ if (numberOfBanks < 2) {+ return {finish: true};+ }if (mostInThePastBank) {- await mostInThePastBank.prepareForDeletion(logLevel);+ const {framesDeleted} = mostInThePastBank.prepareForDeletion(logLevel);delete sources[mostInThePastSrc][mostInThePastBank.startTimestampInSeconds];Internals.Log.verbose({logLevel, tag: '@remotion/media'},- `Deleted frames for src ${mostInThePastSrc} from ${mostInThePastBank.startTimestampInSeconds}sec to ${mostInThePastBank.endTimestampInSeconds}sec to free up memory.`,+ `Deleted ${framesDeleted} frames for src ${mostInThePastSrc} from ${mostInThePastBank.startTimestampInSeconds}sec to ${mostInThePastBank.endTimestampInSeconds}sec to free up memory.`,);}++ return {finish: false};};const ensureToStayUnderMaxCacheSize = async (logLevel: LogLevel) => {let cacheStats = await getTotalCacheStats();const maxCacheSize = getMaxVideoCacheSize(logLevel);+while (cacheStats.totalSize > maxCacheSize) {- await deleteOldestKeyframeBank(logLevel);+ const {finish} = await deleteOldestKeyframeBank(logLevel);+ if (finish) {+ break;+ }++ Internals.Log.verbose(+ {logLevel, tag: '@remotion/media'},+ 'Deleted oldest keyframe bank to stay under max cache size',+ (cacheStats.totalSize / 1024 / 1024).toFixed(1),+ 'out of',+ (maxCacheSize / 1024 / 1024).toFixed(1),+ );+cacheStats = await getTotalCacheStats();}};@@ -144,7 +171,7 @@ export const makeKeyframeManager = () => {const {endTimestampInSeconds, startTimestampInSeconds} = bank;if (endTimestampInSeconds < threshold) {- await bank.prepareForDeletion(logLevel);+ bank.prepareForDeletion(logLevel);Internals.Log.verbose({logLevel, tag: '@remotion/media'},`[Video] Cleared frames for src ${src} from ${startTimestampInSeconds}sec to ${endTimestampInSeconds}sec`,