Skip to content

🎥 Make videos programmatically with React

max frame cache items

Jonny Burger7513bdd8d468
diff --git a/packages/renderer/src/compositor/compositor.ts b/packages/renderer/src/compositor/compositor.ts
index a82d12c70b..5389391fbb 100644
--- a/packages/renderer/src/compositor/compositor.ts
+++ b/packages/renderer/src/compositor/compositor.ts
@@ -1,4 +1,5 @@
import {spawn} from 'child_process';
+import os from 'os';
import {dynamicLibraryPathOptions} from '../call-ffmpeg';
import {getActualConcurrency} from '../get-concurrency';
import {serializeCommand} from './compose';
@@ -25,9 +26,20 @@ type Waiter = {
reject: (err: Error) => void;
};
-export const startLongRunningCompositor = () => {
+export const getIdealMaximumFrameCacheItems = () => {
+ const freeMemory = os.freemem();
+ // Assuming 1 frame is approximately 6MB
+ // Assuming only half the available memory should be used
+ const max = Math.floor(freeMemory / (1024 * 1024 * 6));
+
+ // Never store more than 500 frames
+ return Math.min(max, 500);
+};
+
+export const startLongRunningCompositor = (maximumFrameCacheItems: number) => {
return startCompositor('StartLongRunningProcess', {
concurrency: getActualConcurrency(null),
+ maximum_frame_cache_items: maximumFrameCacheItems,
});
};
diff --git a/packages/renderer/src/compositor/payloads.ts b/packages/renderer/src/compositor/payloads.ts
index b0e1238072..f3bda4f693 100644
--- a/packages/renderer/src/compositor/payloads.ts
+++ b/packages/renderer/src/compositor/payloads.ts
@@ -51,6 +51,7 @@ export type CompositorCommand = {
};
StartLongRunningProcess: {
concurrency: number;
+ maximum_frame_cache_items: number;
};
GetOpenVideoStats: {};
DeliberatePanic: {};
diff --git a/packages/renderer/src/test/compositor-rust.test.ts b/packages/renderer/src/test/compositor-rust.test.ts
index f7f56e15e9..9f532bc536 100644
--- a/packages/renderer/src/test/compositor-rust.test.ts
+++ b/packages/renderer/src/test/compositor-rust.test.ts
@@ -1,10 +1,15 @@
import {expect, test} from 'vitest';
-import {startLongRunningCompositor} from '../compositor/compositor';
+import {
+ getIdealMaximumFrameCacheItems,
+ startLongRunningCompositor,
+} from '../compositor/compositor';
test(
'Compositor should process messages in the right order',
async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
const matching = await Promise.all(
new Array(100).fill(true).map(async (_, i) => {
diff --git a/packages/renderer/src/test/extract-frame-rust.test.ts b/packages/renderer/src/test/extract-frame-rust.test.ts
index 50a00750f3..7e85dc9cf3 100644
--- a/packages/renderer/src/test/extract-frame-rust.test.ts
+++ b/packages/renderer/src/test/extract-frame-rust.test.ts
@@ -1,5 +1,8 @@
import {expect, test} from 'vitest';
-import {startLongRunningCompositor} from '../compositor/compositor';
+import {
+ getIdealMaximumFrameCacheItems,
+ startLongRunningCompositor,
+} from '../compositor/compositor';
import {exampleVideos} from './example-videos';
const BMP_HEADER_SIZE = 54;
@@ -7,7 +10,9 @@ const BMP_HEADER_SIZE = 54;
test(
'Should be able to extract a frame using Rust',
async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
const data = await compositor.executeCommand('ExtractFrame', {
input: exampleVideos.bigBuckBunny,
@@ -34,7 +39,9 @@ test(
test(
'Should be able to get a PNG',
async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
const data = await compositor.executeCommand('ExtractFrame', {
input: exampleVideos.transparentWebm,
@@ -55,9 +62,13 @@ test(
);
test('Should be able to start two compositors', async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
- const compositor2 = startLongRunningCompositor();
+ const compositor2 = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
await compositor.executeCommand('ExtractFrame', {
input: exampleVideos.bigBuckBunny,
@@ -72,7 +83,9 @@ test('Should be able to start two compositors', async () => {
});
test('Should be able to seek backwards', async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
const data = await compositor.executeCommand('ExtractFrame', {
input: exampleVideos.bigBuckBunny,
@@ -94,7 +107,9 @@ test('Should be able to seek backwards', async () => {
test(
'Should be able to extract a frame that has no file extension',
async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
const data = await compositor.executeCommand('ExtractFrame', {
input: exampleVideos.framerWithoutFileExtension,
@@ -112,7 +127,9 @@ test(
test(
'Should get the last frame if out of range',
async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
const data = await compositor.executeCommand('ExtractFrame', {
input: exampleVideos.framerWithoutFileExtension,
@@ -139,7 +156,9 @@ test(
test(
'Should get the last frame of a corrupted video',
async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
const data = await compositor.executeCommand('ExtractFrame', {
input: exampleVideos.corrupted,
@@ -160,7 +179,9 @@ test(
);
test('Should be able to extract a frame with abnormal DAR', async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
const data = await compositor.executeCommand('ExtractFrame', {
input: exampleVideos.customDar,
@@ -181,7 +202,9 @@ test('Should be able to extract a frame with abnormal DAR', async () => {
});
test('Should be able to extract the frames in reverse order', async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
let prevPixel = '';
@@ -220,7 +243,9 @@ test('Should be able to extract the frames in reverse order', async () => {
});
test('Last frame should be fast', async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
const time = Date.now();
@@ -274,7 +299,9 @@ test('Last frame should be fast', async () => {
});
test('Two different starting times should not result in big seeking', async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
const expected = [];
diff --git a/packages/renderer/src/test/rust-errors.test.ts b/packages/renderer/src/test/rust-errors.test.ts
index a1cfdaad5e..797a813d98 100644
--- a/packages/renderer/src/test/rust-errors.test.ts
+++ b/packages/renderer/src/test/rust-errors.test.ts
@@ -1,9 +1,14 @@
import {expect, test} from 'vitest';
import {callCompositor, serializeCommand} from '../compositor/compose';
-import {startLongRunningCompositor} from '../compositor/compositor';
+import {
+ getIdealMaximumFrameCacheItems,
+ startLongRunningCompositor,
+} from '../compositor/compositor';
test('Should get Rust errors in a good way', async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
try {
await compositor.executeCommand('ExtractFrame', {
@@ -20,7 +25,9 @@ test('Should get Rust errors in a good way', async () => {
});
test('Handle panics', async () => {
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
try {
await compositor.executeCommand('DeliberatePanic', {});
diff --git a/packages/renderer/src/test/rust-memory-usage.test.ts b/packages/renderer/src/test/rust-memory-usage.test.ts
index 5238bb414e..b20020b6f5 100644
--- a/packages/renderer/src/test/rust-memory-usage.test.ts
+++ b/packages/renderer/src/test/rust-memory-usage.test.ts
@@ -1,6 +1,9 @@
import {execSync} from 'child_process';
import {expect, test} from 'vitest';
-import {startLongRunningCompositor} from '../compositor/compositor';
+import {
+ getIdealMaximumFrameCacheItems,
+ startLongRunningCompositor,
+} from '../compositor/compositor';
import {exampleVideos} from './example-videos';
test('Memory usage should be determined ', async () => {
@@ -8,7 +11,9 @@ test('Memory usage should be determined ', async () => {
return;
}
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
expect(
getMemoryUsageByPid((compositor.pid as Number).toString())
@@ -97,7 +102,9 @@ test('Should be able to take commands for freeing up memory', async () => {
return;
}
- const compositor = startLongRunningCompositor();
+ const compositor = startLongRunningCompositor(
+ getIdealMaximumFrameCacheItems()
+ );
expect(
getMemoryUsageByPid((compositor.pid as Number).toString())