🎥 Make videos programmatically with React
offthread video cache
Jonny Burger38b7292f21d3
diff --git a/packages/renderer/src/extract-frame-from-video.ts b/packages/renderer/src/extract-frame-from-video.tsindex aa5051dbc3..bea1f05768 100644--- a/packages/renderer/src/extract-frame-from-video.ts+++ b/packages/renderer/src/extract-frame-from-video.ts@@ -2,6 +2,11 @@ import execa from 'execa';import {FfmpegExecutable} from 'remotion';import {Readable} from 'stream';import {frameToFfmpegTimestamp} from './frame-to-ffmpeg-timestamp';+import {+ getLastFrameFromCache,+ LastFrameOptions,+ setLastFrameInCache,+} from './last-frame-from-video-cache';import {pLimit} from './p-limit';export function streamToString(stream: Readable) {@@ -13,15 +18,13 @@ export function streamToString(stream: Readable) {});}-export const getLastFrameOfVideo = async ({+const limit = pLimit(5);++const getLastFrameOfVideoUnlimited = async ({ffmpegExecutable,offset,src,-}: {- ffmpegExecutable: FfmpegExecutable;- offset: number;- src: string;-}): Promise<Buffer> => {+}: LastFrameOptions): Promise<Buffer> => {if (offset > 100) {throw new Error('could not get last frame of ' +@@ -84,7 +87,19 @@ export const getLastFrameOfVideo = async ({return stdoutBuffer;};-const limit = pLimit(5);+const getLastFrameOfVideo = async (+ options: LastFrameOptions+): Promise<Buffer> => {+ const fromCache = getLastFrameFromCache(options);+ if (fromCache) {+ return fromCache;+ }++ const result = await limit(getLastFrameOfVideoUnlimited, options);+ setLastFrameInCache(options, result);++ return result;+};type Options = {time: number;diff --git a/packages/renderer/src/last-frame-from-video-cache.ts b/packages/renderer/src/last-frame-from-video-cache.tsnew file mode 100644index 0000000000..096cfbdd17--- /dev/null+++ b/packages/renderer/src/last-frame-from-video-cache.ts@@ -0,0 +1,71 @@+// OffthreadVideo requires sometimes that the last frame of a video gets extracted, however, this can be slow. We allocate a cache for it but that can be garbage collected++import {FfmpegExecutable} from 'remotion';++export type LastFrameOptions = {+ ffmpegExecutable: FfmpegExecutable;+ offset: number;+ src: string;+};++let map: Record<string, {lastAccessed: number; data: Buffer}> = {};++const MAX_CACHE_SIZE = 50 * 1024 * 1024; // 50MB++let bufferSize = 0;++const makeLastFrameCacheKey = (options: LastFrameOptions) => {+ return [options.ffmpegExecutable, options.offset, options.src].join('-');+};++export const setLastFrameInCache = (+ options: LastFrameOptions,+ data: Buffer+) => {+ const key = makeLastFrameCacheKey(options);+ if (map[key]) {+ bufferSize -= map[key].data.byteLength;+ }++ map[key] = {data, lastAccessed: Date.now()};+ bufferSize += data.byteLength;+ ensureMaxSize();+};++export const getLastFrameFromCache = (+ options: LastFrameOptions+): Buffer | null => {+ const key = makeLastFrameCacheKey(options);++ if (!map[key]) {+ return null;+ }++ map[key].lastAccessed = Date.now();+ return map[key].data ?? null;+};++export const removedLastFrameFromCache = (key: string) => {+ if (!map[key]) {+ return;+ }++ bufferSize -= map[key].data.byteLength;++ delete map[key];+};++export const ensureMaxSize = () => {+ // eslint-disable-next-line no-unmodified-loop-condition+ while (bufferSize > MAX_CACHE_SIZE) {+ const earliest = Object.entries(map).sort((a, b) => {+ return a[1].lastAccessed - b[1].lastAccessed;+ })[0];++ removedLastFrameFromCache(earliest[0]);+ }+};++export const clearLastFileCache = () => {+ map = {};+};diff --git a/packages/renderer/src/test/last-frame-video-cache.test.ts b/packages/renderer/src/test/last-frame-video-cache.test.tsnew file mode 100644index 0000000000..3a01e1ddbc--- /dev/null+++ b/packages/renderer/src/test/last-frame-video-cache.test.ts@@ -0,0 +1,28 @@+import crypto from 'crypto';+import {+ clearLastFileCache,+ getLastFrameFromCache,+ setLastFrameInCache,+} from '../last-frame-from-video-cache';++test('Last frame video cache', () => {+ expect(getLastFrameFromCache('1')).toBe(null);++ const buf = crypto.randomBytes(10 * 1024 * 1024);+ setLastFrameInCache('1', buf);+ expect(getLastFrameFromCache('1')?.byteLength).toBe(10 * 1024 * 1024);+ setLastFrameInCache('2', buf);+ setLastFrameInCache('3', buf);+ setLastFrameInCache('4', buf);+ setLastFrameInCache('5', buf);+ setLastFrameInCache('6', buf);++ expect(getLastFrameFromCache('1')?.byteLength ?? 0).toBe(0);+ expect(getLastFrameFromCache('2')?.byteLength).toBe(10 * 1024 * 1024);+ expect(getLastFrameFromCache('3')?.byteLength).toBe(10 * 1024 * 1024);+ expect(getLastFrameFromCache('4')?.byteLength).toBe(10 * 1024 * 1024);+ expect(getLastFrameFromCache('5')?.byteLength).toBe(10 * 1024 * 1024);+ expect(getLastFrameFromCache('6')?.byteLength).toBe(10 * 1024 * 1024);++ clearLastFileCache();+});