Skip to content

🎥 Make videos programmatically with React

scaffold our artifact

Jonny Burger367224479d83
diff --git a/packages/cli/src/benchmark.ts b/packages/cli/src/benchmark.ts
index 83967681f4..2db1ea5cca 100644
--- a/packages/cli/src/benchmark.ts
+++ b/packages/cli/src/benchmark.ts
@@ -489,6 +489,7 @@ export const benchmarkCommand = async (
}).value,
compositionStart: 0,
onBrowserDownload,
+ onArtifact: () => undefined,
},
(run, progress) => {
benchmarkProgress.update(
diff --git a/packages/cli/src/render-flows/render.ts b/packages/cli/src/render-flows/render.ts
index f349351157..2e61f875f1 100644
--- a/packages/cli/src/render-flows/render.ts
+++ b/packages/cli/src/render-flows/render.ts
@@ -11,6 +11,7 @@ import type {
FrameRange,
LogLevel,
NumberOfGifLoops,
+ OnArtifact,
PixelFormat,
ProResProfile,
RenderMediaOnDownload,
@@ -402,6 +403,10 @@ export const renderVideoFlow = async ({
logLevel,
);
+ const onArtifact: OnArtifact = (artifact) => {
+ console.log('artifact', artifact);
+ };
+
const absoluteSeparateAudioTo =
separateAudioTo === null ? null : path.resolve(separateAudioTo);
const exists = existsSync(absoluteOutputFile);
@@ -491,6 +496,7 @@ export const renderVideoFlow = async ({
compositionStart: 0,
forSeamlessAacConcatenation,
onBrowserDownload,
+ onArtifact,
});
Log.info({indent, logLevel}, chalk.blue(`â–¶ ${absoluteOutputFile}`));
@@ -580,6 +586,7 @@ export const renderVideoFlow = async ({
forSeamlessAacConcatenation,
compositionStart: 0,
onBrowserDownload,
+ onArtifact,
});
if (!updatesDontOverwrite) {
updateRenderProgress({newline: true, printToConsole: true});
diff --git a/packages/cloudrun/src/functions/render-media-single-thread.ts b/packages/cloudrun/src/functions/render-media-single-thread.ts
index cd6b677f06..fcdc80d153 100644
--- a/packages/cloudrun/src/functions/render-media-single-thread.ts
+++ b/packages/cloudrun/src/functions/render-media-single-thread.ts
@@ -1,6 +1,10 @@
import type * as ff from '@google-cloud/functions-framework';
import {Storage} from '@google-cloud/storage';
-import type {ChromiumOptions, RenderMediaOnProgress} from '@remotion/renderer';
+import type {
+ ChromiumOptions,
+ OnArtifact,
+ RenderMediaOnProgress,
+} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import {NoReactInternals} from 'remotion/no-react';
import {VERSION} from 'remotion/version';
@@ -59,6 +63,11 @@ export const renderMediaSingleThread = async (
enableMultiProcessOnLinux: true,
};
+ const onArtifact: OnArtifact = (artifact) => {
+ // TODO: Do something with the artifact
+ console.log(artifact);
+ };
+
await RenderInternals.internalRenderMedia({
composition: {
...composition,
@@ -126,6 +135,7 @@ export const renderMediaSingleThread = async (
onBrowserDownload: () => {
throw new Error('Should not download a browser in Cloud Run');
},
+ onArtifact,
});
const storage = new Storage();
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index 8e15291dfa..ac45bec1aa 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -1,4 +1,9 @@
-import type {AudioCodec, BrowserLog, Codec} from '@remotion/renderer';
+import type {
+ AudioCodec,
+ BrowserLog,
+ Codec,
+ OnArtifact,
+} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import fs from 'node:fs';
import path from 'node:path';
@@ -167,6 +172,10 @@ const renderHandler = async ({
params.everyNthFrame,
);
+ const onArtifact: OnArtifact = (artifact) => {
+ console.log(artifact);
+ };
+
await new Promise<void>((resolve, reject) => {
RenderInternals.internalRenderMedia({
repro: false,
@@ -271,6 +280,7 @@ const renderHandler = async ({
onBrowserDownload: () => {
throw new Error('Should not download a browser in Lambda');
},
+ onArtifact,
})
.then(({slowestFrames}) => {
RenderInternals.Log.verbose(
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index d30ac4b6e7..ac072fa033 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -108,7 +108,7 @@ export {X264Preset} from './options/x264-preset';
export {PixelFormat} from './pixel-format';
export {RemotionServer} from './prepare-server';
export {ProResProfile} from './prores-profile';
-export {RenderFramesOptions, renderFrames} from './render-frames';
+export {OnArtifact, RenderFramesOptions, renderFrames} from './render-frames';
export {
InternalRenderMediaOptions,
RenderMediaOnProgress,
diff --git a/packages/renderer/src/render-frames.ts b/packages/renderer/src/render-frames.ts
index 34c5746f48..8ac752ef34 100644
--- a/packages/renderer/src/render-frames.ts
+++ b/packages/renderer/src/render-frames.ts
@@ -2,11 +2,7 @@ import fs from 'node:fs';
import path from 'node:path';
import {performance} from 'perf_hooks';
// eslint-disable-next-line no-restricted-imports
-import type {
- ArtifactAsset,
- AudioOrVideoAsset,
- VideoConfig,
-} from 'remotion/no-react';
+import type {AudioOrVideoAsset, VideoConfig} from 'remotion/no-react';
import {NoReactInternals} from 'remotion/no-react';
import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';
import {downloadAndMapAssetsToFileUrl} from './assets/download-and-map-assets-to-file';
@@ -71,7 +67,9 @@ import {wrapWithErrorHandling} from './wrap-with-error-handling';
const MAX_RETRIES_PER_FRAME = 1;
-export type InternalRenderFramesOptions = {
+export type OnArtifact = (asset: EmittedAsset) => void;
+
+type InternalRenderFramesOptions = {
onStart: null | ((data: OnStartData) => void);
onFrameUpdate:
| null
@@ -105,8 +103,15 @@ export type InternalRenderFramesOptions = {
serializedResolvedPropsWithCustomSchema: string;
parallelEncodingEnabled: boolean;
compositionStart: number;
+ onArtifact: OnArtifact | null;
} & ToOptions<typeof optionsMap.renderFrames>;
+type EmittedAsset = {
+ filename: string;
+ content: string | Buffer;
+ frame: number;
+};
+
type InnerRenderFramesOptions = {
onStart: null | ((data: OnStartData) => void);
onFrameUpdate:
@@ -123,6 +128,7 @@ type InnerRenderFramesOptions = {
everyNthFrame: number;
onBrowserLog: null | ((log: BrowserLog) => void);
onFrameBuffer: null | ((buffer: Buffer, frame: number) => void);
+ onArtifact: OnArtifact | null;
onDownload: RenderMediaOnDownload | null;
timeoutInMilliseconds: number;
scale: number;
@@ -146,10 +152,15 @@ type InnerRenderFramesOptions = {
compositionStart: number;
} & ToOptions<typeof optionsMap.renderFrames>;
+type ArtifactWithoutContent = {
+ frame: number;
+ filename: string;
+};
+
export type FrameAndAssets = {
frame: number;
audioAndVideoAssets: AudioOrVideoAsset[];
- artifactAssets: ArtifactAsset[];
+ artifactAssets: ArtifactWithoutContent[];
};
export type RenderFramesOptions = {
@@ -190,6 +201,7 @@ export type RenderFramesOptions = {
composition: VideoConfig;
muted?: boolean;
concurrency?: number | string | null;
+ onArtifact?: OnArtifact | null;
serveUrl: string;
} & Partial<ToOptions<typeof optionsMap.renderFrames>>;
@@ -511,7 +523,7 @@ const innerRenderFrames = async ({
if (artifact.filename === previousArtifact.filename) {
reject(
new Error(
- `An artifact with output "${artifact.filename}" was already registered at frame ${previousArtifact.frame}, but now registered again at frame ${artifact.frame}.`,
+ `An artifact with output "${artifact.filename}" was already registered at frame ${previousArtifact.frame}, but now registered again at frame ${artifact.frame}. An artifact`,
),
);
return;
@@ -526,7 +538,12 @@ const innerRenderFrames = async ({
assets.push({
audioAndVideoAssets: compressedAssets,
frame,
- artifactAssets,
+ artifactAssets: artifactAssets.map((a) => {
+ return {
+ frame: a.frame,
+ filename: a.filename,
+ };
+ }),
});
for (const renderAsset of compressedAssets) {
downloadAndMapAssetsToFileUrl({
@@ -784,6 +801,7 @@ const internalRenderFramesRaw = ({
forSeamlessAacConcatenation,
compositionStart,
onBrowserDownload,
+ onArtifact,
}: InternalRenderFramesOptions): Promise<RenderFramesOutput> => {
validateDimension(
composition.height,
@@ -913,6 +931,7 @@ const internalRenderFramesRaw = ({
forSeamlessAacConcatenation,
compositionStart,
onBrowserDownload,
+ onArtifact,
});
}),
])
@@ -1007,6 +1026,7 @@ export const renderFrames = (
offthreadVideoCacheSizeInBytes,
binariesDirectory,
onBrowserDownload,
+ onArtifact,
} = options;
if (!composition) {
@@ -1078,5 +1098,6 @@ export const renderFrames = (
onBrowserDownload:
onBrowserDownload ??
defaultBrowserDownloadProgress({indent, logLevel, api: 'renderFrames()'}),
+ onArtifact: onArtifact ?? null,
});
};
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index fce7039b5a..191035cdc7 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -53,6 +53,7 @@ import {prespawnFfmpeg} from './prespawn-ffmpeg';
import {shouldUseParallelEncoding} from './prestitcher-memory-usage';
import type {ProResProfile} from './prores-profile';
import {validateSelectedCodecAndProResCombination} from './prores-profile';
+import type {OnArtifact} from './render-frames';
import {internalRenderFrames} from './render-frames';
import {
disableRepro,
@@ -125,6 +126,7 @@ export type InternalRenderMediaOptions = {
concurrency: number | string | null;
binariesDirectory: string | null;
compositionStart: number;
+ onArtifact: OnArtifact | null;
} & MoreRenderMediaOptions;
type Prettify<T> = {
@@ -179,6 +181,7 @@ export type RenderMediaOptions = Prettify<{
colorSpace?: ColorSpace;
repro?: boolean;
binariesDirectory?: string | null;
+ onArtifact?: OnArtifact;
}> &
Partial<MoreRenderMediaOptions>;
@@ -241,6 +244,7 @@ const internalRenderMediaRaw = ({
forSeamlessAacConcatenation,
compositionStart,
onBrowserDownload,
+ onArtifact,
}: InternalRenderMediaOptions): Promise<RenderMediaResult> => {
if (repro) {
enableRepro({
@@ -649,6 +653,7 @@ const internalRenderMediaRaw = ({
compositionStart,
forSeamlessAacConcatenation,
onBrowserDownload,
+ onArtifact,
});
return renderFramesProc;
@@ -863,6 +868,7 @@ export const renderMedia = ({
separateAudioTo,
forSeamlessAacConcatenation,
onBrowserDownload,
+ onArtifact,
}: RenderMediaOptions): Promise<RenderMediaResult> => {
if (quality !== undefined) {
console.warn(
@@ -937,6 +943,7 @@ export const renderMedia = ({
onBrowserDownload:
onBrowserDownload ??
defaultBrowserDownloadProgress({indent, logLevel, api: 'renderMedia()'}),
+ onArtifact: onArtifact ?? null,
// TODO: In the future, introduce this as a public API when launching the distributed rendering API
compositionStart: 0,
});