Skip to content

🎥 Make videos programmatically with React

take half of memory by default

Jonny Burger7ec5d59a3a59
diff --git a/packages/media/src/audio-extraction/audio-manager.ts b/packages/media/src/audio-extraction/audio-manager.ts
index 20c896e684..7d6a8e19dd 100644
--- a/packages/media/src/audio-extraction/audio-manager.ts
+++ b/packages/media/src/audio-extraction/audio-manager.ts
@@ -1,5 +1,5 @@
import type {AudioSampleSink} from 'mediabunny';
-import {getTotalCacheStats, MAX_CACHE_SIZE} from '../caches';
+import {getMaxVideoCacheSize, getTotalCacheStats} from '../caches';
import type {LogLevel} from '../log';
import type {RememberActualMatroskaTimestamps} from '../video-extraction/remember-actual-matroska-timestamps';
import type {AudioSampleIterator} from './audio-iterator';
@@ -62,14 +62,17 @@ export const makeAudioManager = () => {
audioSampleSink,
isMatroska,
actualMatroskaTimestamps,
+ logLevel,
}: {
src: string;
timeInSeconds: number;
audioSampleSink: AudioSampleSink;
isMatroska: boolean;
actualMatroskaTimestamps: RememberActualMatroskaTimestamps;
+ logLevel: LogLevel;
}) => {
- while ((await getTotalCacheStats()).totalSize > MAX_CACHE_SIZE) {
+ const maxCacheSize = getMaxVideoCacheSize(logLevel);
+ while ((await getTotalCacheStats()).totalSize > maxCacheSize) {
await deleteOldestIterator();
}
diff --git a/packages/media/src/audio-extraction/extract-audio.ts b/packages/media/src/audio-extraction/extract-audio.ts
index 7974df4f2c..55f88848b1 100644
--- a/packages/media/src/audio-extraction/extract-audio.ts
+++ b/packages/media/src/audio-extraction/extract-audio.ts
@@ -46,6 +46,7 @@ export const extractAudio = async ({
audioSampleSink: audio.sampleSink,
isMatroska,
actualMatroskaTimestamps,
+ logLevel,
});
const samples = await sampleIterator.getSamples(
diff --git a/packages/media/src/caches.ts b/packages/media/src/caches.ts
index 46fda30722..349e0cc7ae 100644
--- a/packages/media/src/caches.ts
+++ b/packages/media/src/caches.ts
@@ -1,7 +1,8 @@
+import {cancelRender, type LogLevel} from 'remotion';
import {makeAudioManager} from './audio-extraction/audio-manager';
+import {Log} from './log';
import {makeKeyframeManager} from './video-extraction/keyframe-manager';
-export const MAX_CACHE_SIZE = 1000 * 1000 * 1000; // 1GB
// TODO: make it dependent on the fps and concurrency
export const SAFE_BACK_WINDOW_IN_SECONDS = 1;
@@ -18,3 +19,73 @@ export const getTotalCacheStats = async () => {
keyframeManagerCacheStats.totalSize + audioManagerCacheStats.totalSize,
};
};
+
+const getUncachedMaxCacheSize = (logLevel: LogLevel) => {
+ if (
+ window.remotion_videoCacheSizeInBytes !== undefined &&
+ window.remotion_videoCacheSizeInBytes !== null
+ ) {
+ if (window.remotion_videoCacheSizeInBytes < 240 * 1024 * 1024) {
+ cancelRender(
+ new Error(
+ `The minimum value for the "videoCacheSizeInBytes" prop is 240MB (${240 * 1024 * 1024}), got: ${window.remotion_videoCacheSizeInBytes}`,
+ ),
+ );
+ }
+
+ if (window.remotion_videoCacheSizeInBytes > 20_000 * 1024 * 1024) {
+ cancelRender(
+ new Error(
+ `The maximum value for the "videoCacheSizeInBytes" prop is 20GB (${20000 * 1024 * 1024}), got: ${window.remotion_videoCacheSizeInBytes}`,
+ ),
+ );
+ }
+
+ Log.verbose(
+ logLevel,
+ `Using @remotion/media cache size set using "videoCacheSizeInBytes": ${(window.remotion_videoCacheSizeInBytes / 1024 / 1024).toFixed(1)} MB`,
+ );
+ return window.remotion_videoCacheSizeInBytes;
+ }
+
+ if (
+ window.remotion_initialMemoryAvailable !== undefined &&
+ window.remotion_initialMemoryAvailable !== null
+ ) {
+ const value = window.remotion_initialMemoryAvailable / 2;
+ if (value < 240 * 1024 * 1024) {
+ Log.verbose(
+ logLevel,
+ `Using @remotion/media cache size set based on minimum value of 240MB (which is more than half of the available system memory!)`,
+ );
+ return 240 * 1024 * 1024;
+ }
+
+ if (value > 20_000 * 1024 * 1024) {
+ Log.verbose(
+ logLevel,
+ `Using @remotion/media cache size set based on maximum value of 20GB (which is less than half of the available system memory)`,
+ );
+ return 20_000 * 1024 * 1024;
+ }
+
+ Log.verbose(
+ logLevel,
+ `Using @remotion/media cache size set based on available memory (50% of available memory): ${(window.remotion_initialMemoryAvailable / 1024 / 1024).toFixed(1)} MB`,
+ );
+ return value;
+ }
+
+ return 1000 * 1000 * 1000; // 1GB
+};
+
+let cachedMaxCacheSize: number | null = null;
+
+export const getMaxVideoCacheSize = (logLevel: LogLevel) => {
+ if (cachedMaxCacheSize !== null) {
+ return cachedMaxCacheSize;
+ }
+
+ cachedMaxCacheSize = getUncachedMaxCacheSize(logLevel);
+ return cachedMaxCacheSize;
+};
diff --git a/packages/media/src/video-extraction/keyframe-manager.ts b/packages/media/src/video-extraction/keyframe-manager.ts
index d019d4802d..1624fd84b8 100644
--- a/packages/media/src/video-extraction/keyframe-manager.ts
+++ b/packages/media/src/video-extraction/keyframe-manager.ts
@@ -1,7 +1,7 @@
import type {EncodedPacketSink, VideoSampleSink} from 'mediabunny';
import {
+ getMaxVideoCacheSize,
getTotalCacheStats,
- MAX_CACHE_SIZE,
SAFE_BACK_WINDOW_IN_SECONDS,
} from '../caches';
import type {LogLevel} from '../log';
@@ -114,7 +114,8 @@ export const makeKeyframeManager = () => {
const ensureToStayUnderMaxCacheSize = async (logLevel: LogLevel) => {
let cacheStats = await getTotalCacheStats();
- while (cacheStats.totalSize > MAX_CACHE_SIZE) {
+ const maxCacheSize = getMaxVideoCacheSize(logLevel);
+ while (cacheStats.totalSize > maxCacheSize) {
await deleteOldestKeyframeBank(logLevel);
cacheStats = await getTotalCacheStats();
}
diff --git a/packages/renderer/src/set-props-and-env.ts b/packages/renderer/src/set-props-and-env.ts
index 07a365eedc..2a36c912d1 100644
--- a/packages/renderer/src/set-props-and-env.ts
+++ b/packages/renderer/src/set-props-and-env.ts
@@ -55,11 +55,16 @@ const innerSetPropsAndEnv = async ({
const urlToVisit = normalizeServeUrl(serveUrl);
await page.evaluateOnNewDocument(
- (timeout: number, mainTab: boolean) => {
+ (
+ timeout: number,
+ mainTab: boolean,
+ cacheSizeInBytes: number | null,
+ initMemoryAvailable: number | null,
+ ) => {
window.remotion_puppeteerTimeout = timeout;
window.remotion_isMainTab = mainTab;
- window.remotion_videoCacheSizeInBytes = videoCacheSizeInBytes;
- window.remotion_initialMemoryAvailable = initialMemoryAvailable;
+ window.remotion_videoCacheSizeInBytes = cacheSizeInBytes;
+ window.remotion_initialMemoryAvailable = initMemoryAvailable;
// To make useRemotionEnvironment() work
if (window.process === undefined) {
// @ts-expect-error
@@ -75,6 +80,7 @@ const innerSetPropsAndEnv = async ({
actualTimeout,
isMainTab,
videoCacheSizeInBytes,
+ initialMemoryAvailable,
);
await page.evaluateOnNewDocument(