Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: 10MB LRU cache for timeline video frames (#6960)

Jonny Burgerad95326a41d5
diff --git a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
index 384a67776b..40668fa4ad 100644
--- a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
+++ b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx
@@ -3,9 +3,8 @@ import {useVideoConfig} from 'remotion';
import {extractFrames} from '../../helpers/extract-frames';
import type {FrameDatabaseKey} from '../../helpers/frame-database';
import {
+ addFrameToCache,
aspectRatioCache,
- clearFramesForSrc,
- clearOldFrames,
frameDatabase,
getAspectRatioFromCache,
getFrameDatabaseKeyPrefix,
@@ -305,12 +304,6 @@ export const TimelineVideoInfo: React.FC<{
const [error, setError] = useState<Error | null>(null);
const aspectRatio = useRef<number | null>(getAspectRatioFromCache(src));
- useEffect(() => {
- return () => {
- clearFramesForSrc(src);
- };
- }, [src]);
-
// for rendering frames
useEffect(() => {
if (error) {
@@ -368,13 +361,10 @@ export const TimelineVideoInfo: React.FC<{
if (unfilled.length === 0) {
return () => {
current.removeChild(canvas);
- clearOldFrames();
};
}
}
- clearOldFrames();
-
extractFrames({
timestampsInSeconds: ({
track,
@@ -417,15 +407,7 @@ export const TimelineVideoInfo: React.FC<{
const databaseKey = makeFrameDatabaseKey(src, transformed.timestamp);
- const existingFrame = frameDatabase.get(databaseKey);
- if (existingFrame) {
- existingFrame.frame.close();
- }
-
- frameDatabase.set(databaseKey, {
- frame: transformed,
- lastUsed: Date.now(),
- });
+ addFrameToCache(databaseKey, transformed);
if (aspectRatio.current === null) {
throw new Error('Aspect ratio is not set');
}
@@ -473,9 +455,6 @@ export const TimelineVideoInfo: React.FC<{
})
.catch((e: unknown) => {
setError(e as Error);
- })
- .finally(() => {
- clearOldFrames();
});
return () => {
diff --git a/packages/studio/src/helpers/frame-database.ts b/packages/studio/src/helpers/frame-database.ts
index fd21659705..ec861d7f03 100644
--- a/packages/studio/src/helpers/frame-database.ts
+++ b/packages/studio/src/helpers/frame-database.ts
@@ -16,8 +16,13 @@ export const getFrameDatabaseKeyPrefix = (src: string): string => {
type VideoFrameAndLastUsed = {
frame: VideoFrame;
lastUsed: number;
+ size: number;
};
+const MAX_CACHE_SIZE_BYTES = 10 * 1024 * 1024; // 10 MB
+
+let totalCacheSize = 0;
+
export const frameDatabase: Map<FrameDatabaseKey, VideoFrameAndLastUsed> =
new Map();
export const aspectRatioCache: Map<string, number> = new Map();
@@ -36,41 +41,46 @@ 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;
+const evictLRU = () => {
+ while (totalCacheSize > MAX_CACHE_SIZE_BYTES && frameDatabase.size > 0) {
+ let oldestKey: FrameDatabaseKey | undefined;
+ let oldestTime = Infinity;
-export const clearOldFrames = () => {
- if (frameDatabase.size <= MAX_FRAMES_IN_CACHE) {
- return;
- }
+ for (const [key, candidate] of frameDatabase) {
+ if (candidate.lastUsed < oldestTime) {
+ oldestTime = candidate.lastUsed;
+ oldestKey = key;
+ }
+ }
- const framesToRemove = Array.from(frameDatabase.entries()).sort(
- (a, b) => a[1].lastUsed - b[1].lastUsed,
- );
+ if (!oldestKey) {
+ break;
+ }
- for (const [key, frame] of framesToRemove.slice(
- 0,
- framesToRemove.length - MAX_FRAMES_IN_CACHE,
- )) {
- frame.frame.close();
- frameDatabase.delete(key);
+ const entry = frameDatabase.get(oldestKey);
+ if (entry) {
+ totalCacheSize -= entry.size;
+ entry.frame.close();
+ frameDatabase.delete(oldestKey);
+ }
}
};
-export const clearFramesForSrc = (src: string) => {
- const keysToRemove: FrameDatabaseKey[] = [];
- const prefix = getFrameDatabaseKeyPrefix(src);
-
- for (const [key, frame] of frameDatabase.entries()) {
- if (key.startsWith(prefix)) {
- frame.frame.close();
- keysToRemove.push(key);
- }
+export const addFrameToCache = (key: FrameDatabaseKey, frame: VideoFrame) => {
+ const existing = frameDatabase.get(key);
+ if (existing) {
+ totalCacheSize -= existing.size;
+ existing.frame.close();
}
- for (const key of keysToRemove) {
- frameDatabase.delete(key);
- }
+ const size = frame.allocationSize();
+ totalCacheSize += size;
+
+ frameDatabase.set(key, {
+ frame,
+ lastUsed: Date.now(),
+ size,
+ });
+
+ evictLRU();
};