Skip to content

🎥 Make videos programmatically with React

scope vp9 cache

Jonny Burger4530a88b4858
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index b37440a83c..fe1023b4bd 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -43,12 +43,7 @@ checkMultipleRemotionVersions();
export * from './AbsoluteFill';
export * from './audio';
export * from './Composition';
-export {
- DownloadMap,
- SmallTCompMetadata,
- TAsset,
- TCompMetadata,
-} from './CompositionManager';
+export {SmallTCompMetadata, TAsset, TCompMetadata} from './CompositionManager';
export {Config, ConfigType} from './config';
export {getInputProps} from './config/input-props';
export * from './delay-render';
diff --git a/packages/renderer/src/assets/convert-assets-to-file-urls.ts b/packages/renderer/src/assets/convert-assets-to-file-urls.ts
index 34aef8f199..a25f7c1e8d 100644
--- a/packages/renderer/src/assets/convert-assets-to-file-urls.ts
+++ b/packages/renderer/src/assets/convert-assets-to-file-urls.ts
@@ -1,6 +1,7 @@
-import type {DownloadMap, TAsset} from 'remotion';
+import type {TAsset} from 'remotion';
import type {RenderMediaOnDownload} from './download-and-map-assets-to-file';
import {downloadAndMapAssetsToFileUrl} from './download-and-map-assets-to-file';
+import type {DownloadMap} from './download-map';
const chunk = <T>(input: T[], size: number) => {
return input.reduce<T[][]>((arr, item, idx) => {
diff --git a/packages/renderer/src/assets/download-map.ts b/packages/renderer/src/assets/download-map.ts
index ad76412dd2..d644168582 100644
--- a/packages/renderer/src/assets/download-map.ts
+++ b/packages/renderer/src/assets/download-map.ts
@@ -10,6 +10,13 @@ type EncodingStatus =
}
| undefined;
+export type SpecialVCodecForTransparency = 'vp9' | 'vp8' | 'none';
+
+export type Vp9Result = {
+ specialVcodec: SpecialVCodecForTransparency;
+ needsResize: [number, number] | null;
+};
+
export type DownloadMap = {
isDownloadingMap: {
[src: string]:
@@ -28,6 +35,7 @@ export type DownloadMap = {
listeners: {[key: string]: {[downloadDir: string]: (() => void)[]}};
lastFrameMap: Record<string, {lastAccessed: number; data: Buffer}>;
isBeyondLastFrameMap: Record<string, number>;
+ isVp9VideoCache: Record<string, Vp9Result>;
ensureFileHasPresentationTimestamp: Record<string, EncodingStatus>;
};
@@ -48,5 +56,6 @@ export const makeDownloadMap = (): DownloadMap => {
lastFrameMap: {},
isBeyondLastFrameMap: {},
ensureFileHasPresentationTimestamp: {},
+ isVp9VideoCache: {},
};
};
diff --git a/packages/renderer/src/extract-frame-from-video.ts b/packages/renderer/src/extract-frame-from-video.ts
index 9f3dcfce9d..893bd171a3 100644
--- a/packages/renderer/src/extract-frame-from-video.ts
+++ b/packages/renderer/src/extract-frame-from-video.ts
@@ -1,10 +1,13 @@
import execa from 'execa';
-import type {DownloadMap, OffthreadVideoImageFormat} from 'remotion';
+import type {OffthreadVideoImageFormat} from 'remotion';
+import type {
+ DownloadMap,
+ SpecialVCodecForTransparency,
+} from './assets/download-map';
import {getVideoStreamDuration} from './assets/get-video-stream-duration';
import {ensurePresentationTimestamps} from './ensure-presentation-timestamp';
import type {FfmpegExecutable} from './ffmpeg-executable';
import {frameToFfmpegTimestamp} from './frame-to-ffmpeg-timestamp';
-import type {SpecialVCodecForTransparency} from './get-video-info';
import {getVideoInfo} from './get-video-info';
import {isBeyondLastFrame, markAsBeyondLastFrame} from './is-beyond-last-frame';
import type {LastFrameOptions} from './last-frame-from-video-cache';
@@ -252,6 +255,7 @@ const extractFrameFromVideoFn = async ({
// audio rendering, so we work with 2 different files
const src = await ensurePresentationTimestamps(downloadMap, options.src);
const {specialVcodec, needsResize} = await getVideoInfo(
+ downloadMap,
src,
ffprobeExecutable
);
diff --git a/packages/renderer/src/get-compositions.ts b/packages/renderer/src/get-compositions.ts
index 7ffbb5dcd3..26fd562347 100644
--- a/packages/renderer/src/get-compositions.ts
+++ b/packages/renderer/src/get-compositions.ts
@@ -1,4 +1,5 @@
-import type {DownloadMap, TCompMetadata} from 'remotion';
+import type {TCompMetadata} from 'remotion';
+import type {DownloadMap} from './assets/download-map';
import {makeDownloadMap} from './assets/download-map';
import type {BrowserExecutable} from './browser-executable';
import type {BrowserLog} from './browser-log';
@@ -122,7 +123,6 @@ export const getCompositions = async (
return innerGetCompositions(
serveUrl,
page,
- // @ts-expect-error
config ?? {},
offthreadPort
);
diff --git a/packages/renderer/src/get-video-info.ts b/packages/renderer/src/get-video-info.ts
index e20fcdc030..0e5f865b59 100644
--- a/packages/renderer/src/get-video-info.ts
+++ b/packages/renderer/src/get-video-info.ts
@@ -1,25 +1,18 @@
import execa from 'execa';
+import type {DownloadMap, Vp9Result} from './assets/download-map';
import {calculateDisplayVideoSize} from './calculate-sar-dar-pixels';
import type {FfmpegExecutable} from './ffmpeg-executable';
import {pLimit} from './p-limit';
-type Result = {
- specialVcodec: SpecialVCodecForTransparency;
- needsResize: [number, number] | null;
-};
-
-export type SpecialVCodecForTransparency = 'vp9' | 'vp8' | 'none';
-
-const isVp9VideoCache: Record<string, Result> = {};
-
const limit = pLimit(1);
async function getVideoInfoUnlimited(
+ downloadMap: DownloadMap,
src: string,
ffprobeExecutable: FfmpegExecutable
-): Promise<Result> {
- if (typeof isVp9VideoCache[src] !== 'undefined') {
- return isVp9VideoCache[src];
+): Promise<Vp9Result> {
+ if (typeof downloadMap.isVp9VideoCache[src] !== 'undefined') {
+ return downloadMap.isVp9VideoCache[src];
}
const task = await execa(ffprobeExecutable ?? 'ffprobe', [src]);
@@ -54,19 +47,22 @@ async function getVideoInfoUnlimited(
}
}
- const result: Result = {
+ const result: Vp9Result = {
specialVcodec: isVp9 ? 'vp9' : isVp8 ? 'vp8' : 'none',
needsResize,
};
- isVp9VideoCache[src] = result;
+ downloadMap.isVp9VideoCache[src] = result;
- return isVp9VideoCache[src];
+ return downloadMap.isVp9VideoCache[src];
}
export const getVideoInfo = (
+ downloadMap: DownloadMap,
src: string,
ffprobeExecutable: FfmpegExecutable
-): Promise<Result> => {
- return limit(() => getVideoInfoUnlimited(src, ffprobeExecutable));
+): Promise<Vp9Result> => {
+ return limit(() =>
+ getVideoInfoUnlimited(downloadMap, src, ffprobeExecutable)
+ );
};
diff --git a/packages/renderer/src/is-beyond-last-frame.ts b/packages/renderer/src/is-beyond-last-frame.ts
index e0c5d64167..b72df428f7 100644
--- a/packages/renderer/src/is-beyond-last-frame.ts
+++ b/packages/renderer/src/is-beyond-last-frame.ts
@@ -1,4 +1,4 @@
-import type {DownloadMap} from 'remotion';
+import type {DownloadMap} from './assets/download-map';
export const isBeyondLastFrame = (
downloadMap: DownloadMap,
diff --git a/packages/renderer/src/last-frame-from-video-cache.ts b/packages/renderer/src/last-frame-from-video-cache.ts
index ff6c320a3b..882219063d 100644
--- a/packages/renderer/src/last-frame-from-video-cache.ts
+++ b/packages/renderer/src/last-frame-from-video-cache.ts
@@ -1,9 +1,11 @@
// 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 type {OffthreadVideoImageFormat} from 'remotion';
-import type {DownloadMap} from './assets/download-map';
+import type {
+ DownloadMap,
+ SpecialVCodecForTransparency,
+} from './assets/download-map';
import type {FfmpegExecutable} from './ffmpeg-executable';
-import type {SpecialVCodecForTransparency} from './get-video-info';
export type LastFrameOptions = {
ffmpegExecutable: FfmpegExecutable;
diff --git a/packages/renderer/src/offthread-video-server.ts b/packages/renderer/src/offthread-video-server.ts
index 434819d9fe..99150e80bb 100644
--- a/packages/renderer/src/offthread-video-server.ts
+++ b/packages/renderer/src/offthread-video-server.ts
@@ -1,9 +1,10 @@
import type {RequestListener} from 'http';
-import type {DownloadMap, OffthreadVideoImageFormat} from 'remotion';
+import type {OffthreadVideoImageFormat} from 'remotion';
import {Internals} from 'remotion';
import {URLSearchParams} from 'url';
import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';
import {downloadAsset} from './assets/download-and-map-assets-to-file';
+import type {DownloadMap} from './assets/download-map';
import {extractFrameFromVideo} from './extract-frame-from-video';
import type {FfmpegExecutable} from './ffmpeg-executable';
diff --git a/packages/renderer/src/prepare-server.ts b/packages/renderer/src/prepare-server.ts
index 7ee7992c6a..09e69f02c1 100644
--- a/packages/renderer/src/prepare-server.ts
+++ b/packages/renderer/src/prepare-server.ts
@@ -1,7 +1,7 @@
import {existsSync} from 'fs';
import path from 'path';
-import type {DownloadMap} from 'remotion';
import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';
+import type {DownloadMap} from './assets/download-map';
import type {FfmpegExecutable} from './ffmpeg-executable';
import {isServeUrl} from './is-serve-url';
import {serveStatic} from './serve-static';
diff --git a/packages/renderer/src/render-frames.ts b/packages/renderer/src/render-frames.ts
index 8b8e280758..a12024fa86 100644
--- a/packages/renderer/src/render-frames.ts
+++ b/packages/renderer/src/render-frames.ts
@@ -1,9 +1,10 @@
import fs from 'fs';
import path from 'path';
-import type {DownloadMap, SmallTCompMetadata, TAsset} from 'remotion';
+import type {SmallTCompMetadata, TAsset} from 'remotion';
import {Internals} from 'remotion';
import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';
import {downloadAndMapAssetsToFileUrl} from './assets/download-and-map-assets-to-file';
+import type {DownloadMap} from './assets/download-map';
import {makeDownloadMap} from './assets/download-map';
import {DEFAULT_BROWSER} from './browser';
import type {BrowserExecutable} from './browser-executable';
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index 6881be6d5d..c6d4daa922 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -2,9 +2,10 @@ import type {ExecaChildProcess} from 'execa';
import fs from 'fs';
import os from 'os';
import path from 'path';
-import type {DownloadMap, SmallTCompMetadata} from 'remotion';
+import type {SmallTCompMetadata} from 'remotion';
import {Internals} from 'remotion';
import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';
+import type {DownloadMap} from './assets/download-map';
import {makeDownloadMap} from './assets/download-map';
import type {BrowserExecutable} from './browser-executable';
import type {BrowserLog} from './browser-log';
diff --git a/packages/renderer/src/render-still.ts b/packages/renderer/src/render-still.ts
index e630f92d0a..7e74179011 100644
--- a/packages/renderer/src/render-still.ts
+++ b/packages/renderer/src/render-still.ts
@@ -1,8 +1,9 @@
import fs, {statSync} from 'fs';
import path from 'path';
-import type {DownloadMap, SmallTCompMetadata} from 'remotion';
+import type {SmallTCompMetadata} from 'remotion';
import {Internals} from 'remotion';
import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';
+import type {DownloadMap} from './assets/download-map';
import {makeDownloadMap} from './assets/download-map';
import {DEFAULT_BROWSER} from './browser';
import type {BrowserExecutable} from './browser-executable';
diff --git a/packages/renderer/src/serve-static.ts b/packages/renderer/src/serve-static.ts
index 7701a251f5..326f62f999 100644
--- a/packages/renderer/src/serve-static.ts
+++ b/packages/renderer/src/serve-static.ts
@@ -1,6 +1,6 @@
import http from 'http';
-import type {DownloadMap} from 'remotion';
import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';
+import type {DownloadMap} from './assets/download-map';
import type {FfmpegExecutable} from './ffmpeg-executable';
import {getDesiredPort} from './get-port';
import {startOffthreadVideoServer} from './offthread-video-server';
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index cab5715e0f..096d86eeea 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -2,13 +2,13 @@ import execa from 'execa';
import fs, {unlinkSync} from 'fs';
import {readFile} from 'fs/promises';
import path from 'path';
-import type {DownloadMap, TAsset} from 'remotion';
+import type {TAsset} from 'remotion';
import {Internals} from 'remotion';
import {calculateAssetPositions} from './assets/calculate-asset-positions';
import {convertAssetsToFileUrls} from './assets/convert-assets-to-file-urls';
import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';
import {markAllAssetsAsDownloaded} from './assets/download-and-map-assets-to-file';
-import type {RenderAssetInfo} from './assets/download-map';
+import type {DownloadMap, RenderAssetInfo} from './assets/download-map';
import type {Assets} from './assets/types';
import type {Codec} from './codec';
import {DEFAULT_CODEC} from './codec';
diff --git a/packages/renderer/src/test/last-frame-video-cache.test.ts b/packages/renderer/src/test/last-frame-video-cache.test.ts
index 30af426c40..fde50bbbe0 100644
--- a/packages/renderer/src/test/last-frame-video-cache.test.ts
+++ b/packages/renderer/src/test/last-frame-video-cache.test.ts
@@ -1,6 +1,6 @@
import crypto from 'crypto';
-import type {DownloadMap} from 'remotion';
import {expect, test} from 'vitest';
+import type {DownloadMap} from '../assets/download-map';
import {makeDownloadMap} from '../assets/download-map';
import type {LastFrameOptions} from '../last-frame-from-video-cache';
import {