Skip to content

🎥 Make videos programmatically with React

use a LRU cache

Jonny Burgera1915a5b30ab
diff --git a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
index 0dfa0d2f50..666fb14065 100644
--- a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
+++ b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
@@ -5,6 +5,7 @@ import {useVideoConfig} from 'remotion';
import type {FrameDatabaseKey} from '../../helpers/frame-database';
import {
aspectRatioCache,
+ clearOldFrames,
frameDatabase,
getAspectRatioFromCache,
getTimestampFromFrameDatabaseKey,
@@ -189,14 +190,16 @@ const fillWithCachedFrames = ({
if (
alreadyFilled &&
Math.abs(alreadyFilled - timestamp) <=
- Math.abs(frame.timestamp - timestamp)
+ Math.abs(frame.frame.timestamp - timestamp)
) {
continue;
}
+ frame.lastUsed = Date.now();
+
drawSlot({
ctx,
- frame,
+ frame: frame.frame,
filledSlots,
visualizationWidth,
timestamp,
@@ -304,6 +307,8 @@ export const TimelineVideoInfo: React.FC<{
});
}
+ clearOldFrames();
+
current.appendChild(canvas);
const controller = new AbortController();
@@ -348,10 +353,13 @@ export const TimelineVideoInfo: React.FC<{
const existingFrame = frameDatabase.get(databaseKey);
if (existingFrame) {
- existingFrame.close();
+ existingFrame.frame.close();
}
- frameDatabase.set(databaseKey, transformed);
+ frameDatabase.set(databaseKey, {
+ frame: transformed,
+ lastUsed: Date.now(),
+ });
if (aspectRatio.current === null) {
throw new Error('Aspect ratio is not set');
}
@@ -390,6 +398,9 @@ export const TimelineVideoInfo: React.FC<{
}
setError(e);
+ })
+ .finally(() => {
+ clearOldFrames();
});
return () => {
diff --git a/packages/studio/src/helpers/frame-database.ts b/packages/studio/src/helpers/frame-database.ts
index efe2b149a8..d0c08c3941 100644
--- a/packages/studio/src/helpers/frame-database.ts
+++ b/packages/studio/src/helpers/frame-database.ts
@@ -7,7 +7,13 @@ export const makeFrameDatabaseKey = (
timestamp: number,
): FrameDatabaseKey => `${src}|${timestamp}` as FrameDatabaseKey;
-export const frameDatabase: Map<FrameDatabaseKey, VideoFrame> = new Map();
+type VideoFrameAndLastUsed = {
+ frame: VideoFrame;
+ lastUsed: number;
+};
+
+export const frameDatabase: Map<FrameDatabaseKey, VideoFrameAndLastUsed> =
+ new Map();
export const aspectRatioCache: Map<string, number> = new Map();
export const getTimestampFromFrameDatabaseKey = (key: FrameDatabaseKey) => {
@@ -23,3 +29,26 @@ export const getAspectRatioFromCache = (src: string) => {
return null;
};
+
+// a 16:9 thumbnail is 43x23px wide - 43 * 23 * 4 = 4052 bytes
+// Our allowance is a 50MB frame cache, so we can store 12340 thumbnails
+
+const MAX_FRAMES_IN_CACHE = 12340;
+
+export const clearOldFrames = () => {
+ if (frameDatabase.size <= MAX_FRAMES_IN_CACHE) {
+ return;
+ }
+
+ const framesToRemove = Array.from(frameDatabase.entries()).sort(
+ (a, b) => a[1].lastUsed - b[1].lastUsed,
+ );
+
+ for (const [key, frame] of framesToRemove.slice(
+ 0,
+ framesToRemove.length - MAX_FRAMES_IN_CACHE,
+ )) {
+ frame.frame.close();
+ frameDatabase.delete(key);
+ }
+};