Skip to content

🎥 Make videos programmatically with React

`remotion`: Allow defining a custom downloadBehavior for `<Artifact>`

Jonny Burger58b4d0b6821f
diff --git a/packages/core/src/Artifact.tsx b/packages/core/src/Artifact.tsx
index c3ee846bd9..37ede8ba6e 100644
--- a/packages/core/src/Artifact.tsx
+++ b/packages/core/src/Artifact.tsx
@@ -1,6 +1,7 @@
import type React from 'react';
import {useContext, useEffect, useState} from 'react';
import {RenderAssetManager} from './RenderAssetManager';
+import type {DownloadBehavior} from './download-behavior';
import {getRemotionEnvironment} from './get-remotion-environment';
import {useCurrentFrame} from './use-current-frame';
@@ -9,9 +10,10 @@ const ArtifactThumbnail = Symbol('Thumbnail');
export const Artifact: React.FC<{
readonly filename: string;
readonly content: string | Uint8Array | typeof ArtifactThumbnail;
+ readonly downloadBehavior?: DownloadBehavior | null;
}> & {
Thumbnail: typeof ArtifactThumbnail;
-} = ({filename, content}) => {
+} = ({filename, content, downloadBehavior}) => {
const {registerRenderAsset, unregisterRenderAsset} =
useContext(RenderAssetManager);
@@ -36,6 +38,7 @@ export const Artifact: React.FC<{
filename,
frame,
contentType: 'binary',
+ downloadBehavior: downloadBehavior ?? null,
});
} else if (content === ArtifactThumbnail) {
registerRenderAsset({
@@ -44,6 +47,7 @@ export const Artifact: React.FC<{
filename,
frame,
contentType: 'thumbnail',
+ downloadBehavior: downloadBehavior ?? null,
});
} else {
registerRenderAsset({
@@ -53,6 +57,7 @@ export const Artifact: React.FC<{
filename,
frame,
contentType: 'text',
+ downloadBehavior: downloadBehavior ?? null,
});
}
@@ -67,6 +72,7 @@ export const Artifact: React.FC<{
id,
registerRenderAsset,
unregisterRenderAsset,
+ downloadBehavior,
]);
return null;
diff --git a/packages/core/src/CompositionManager.tsx b/packages/core/src/CompositionManager.tsx
index 8170e4239d..4c66e7c96b 100644
--- a/packages/core/src/CompositionManager.tsx
+++ b/packages/core/src/CompositionManager.tsx
@@ -23,6 +23,7 @@ import {RenderAssetManagerProvider} from './RenderAssetManager.js';
import {ResolveCompositionConfig} from './ResolveCompositionConfig.js';
import {SequenceManagerProvider} from './SequenceManager.js';
import {SharedAudioContextProvider} from './audio/shared-audio-tags.js';
+import type {DownloadBehavior} from './download-behavior.js';
import type {InferProps, PropsIfHasProps} from './props-if-has-props.js';
export type TComposition<
@@ -151,6 +152,7 @@ export type ArtifactAsset = {
id: string;
filename: string;
frame: number;
+ downloadBehavior: DownloadBehavior | null;
} & DiscriminatedArtifact;
export type TRenderAsset = AudioOrVideoAsset | ArtifactAsset;
diff --git a/packages/core/src/download-behavior.ts b/packages/core/src/download-behavior.ts
new file mode 100644
index 0000000000..fc08501f9c
--- /dev/null
+++ b/packages/core/src/download-behavior.ts
@@ -0,0 +1,8 @@
+export type DownloadBehavior =
+ | {
+ type: 'play-in-browser';
+ }
+ | {
+ type: 'download';
+ fileName: string | null;
+ };
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 1d0a1cdbf7..1ad8bf5f5f 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -120,6 +120,7 @@ export {
export type {CanvasContent} from './CompositionManagerContext.js';
export {getInputProps} from './config/input-props.js';
export {continueRender, delayRender} from './delay-render.js';
+export {DownloadBehavior} from './download-behavior.js';
export * from './easing.js';
export * from './Folder.js';
export * from './freeze.js';
diff --git a/packages/core/src/no-react.ts b/packages/core/src/no-react.ts
index af1dcdb78c..259c85d2a8 100644
--- a/packages/core/src/no-react.ts
+++ b/packages/core/src/no-react.ts
@@ -3,6 +3,7 @@ export type {
AudioOrVideoAsset,
TRenderAsset,
} from './CompositionManager';
+export {DownloadBehavior} from './download-behavior';
export {
EasingFunction,
ExtrapolateType,
@@ -11,6 +12,7 @@ export {
} from './interpolate';
export {random, RandomSeed} from './random.js';
export type {VideoConfig} from './video-config';
+
import {
DELAY_RENDER_CALLSTACK_TOKEN,
DELAY_RENDER_CLEAR_TOKEN,
diff --git a/packages/docs/docs/artifact.mdx b/packages/docs/docs/artifact.mdx
index f54ac732cd..903f446f58 100644
--- a/packages/docs/docs/artifact.mdx
+++ b/packages/docs/docs/artifact.mdx
@@ -53,6 +53,18 @@ Must match the regex `/^([0-9a-zA-Z-!_.*'()/:&$@=;+,?]+)/g`.
A `string` or `Uint8Array` that is the content of the file that will be emitted. Don't consider an `Uint8Array` to be faster, because it needs to be serialized.
+### `downloadBehavior?`<AvailableFrom v="4.0.296" />
+
+Only applies to serverless rendering.
+
+How the output file should behave when accessed through the output link in the browser.
+Either:
+
+- `{"type": "play-in-browser"}` - the default. The video will play in the browser.
+- `{"type": "download", fileName: null}` or `{"type": "download", fileName: "download.mp4"}` - a `Content-Disposition` header will be added which makes the browser download the file. You can optionally override the filename.
+
+The default behavior is the same download behavior you defined for the main rendering output.
+
## `Artifact.Thumbnail`<AvailableFrom v="4.0.290" />
A special symbol that if you pass it to the [`content`](/docs/artifact#content) prop, it will [emit the image data of the current frame as an artifact](/docs/artifacts#emitting-thumbnails).
diff --git a/packages/renderer/eslint.config.mjs b/packages/renderer/eslint.config.mjs
index 20ee3a48fb..96a0bc364d 100644
--- a/packages/renderer/eslint.config.mjs
+++ b/packages/renderer/eslint.config.mjs
@@ -8,15 +8,36 @@ export default [
rules: {
...config.rules,
'@typescript-eslint/no-use-before-define': 'off',
- 'no-restricted-imports': [
+ },
+ ignores: ['src/browser/**'],
+ },
+ {
+ rules: {
+ '@typescript-eslint/no-restricted-imports': [
'error',
{
patterns: ['@remotion/*/src/*', 'remotion/src/*'],
- paths: ['remotion', 'react', 'react-dom'],
+ paths: [
+ {
+ name: 'remotion',
+ message: 'Dont import the runtime',
+ allowTypeImports: true,
+ },
+ {
+ name: 'react',
+ message: 'Dont import the runtime',
+ allowTypeImports: true,
+ },
+ {
+ name: 'react-dom',
+ message: 'Dont import the runtime',
+ allowTypeImports: true,
+ },
+ ],
},
],
},
- ignores: ['src/browser/**'],
+ ignores: ['src/test/**'],
},
{
...config,
diff --git a/packages/renderer/src/filter-asset-types.ts b/packages/renderer/src/filter-asset-types.ts
index 82db928840..9377ee709f 100644
--- a/packages/renderer/src/filter-asset-types.ts
+++ b/packages/renderer/src/filter-asset-types.ts
@@ -35,6 +35,7 @@ export const onlyArtifact = ({
frame: artifact.frame,
content: artifact.content,
filename: artifact.filename,
+ downloadBehavior: artifact.downloadBehavior,
};
}
@@ -50,6 +51,7 @@ export const onlyArtifact = ({
frame: artifact.frame,
content: new Uint8Array(frameBuffer),
filename: artifact.filename,
+ downloadBehavior: artifact.downloadBehavior,
};
}
diff --git a/packages/renderer/src/serialize-artifact.ts b/packages/renderer/src/serialize-artifact.ts
index 45b067da23..21c5193f56 100644
--- a/packages/renderer/src/serialize-artifact.ts
+++ b/packages/renderer/src/serialize-artifact.ts
@@ -1,5 +1,8 @@
+import type {DownloadBehavior} from 'remotion';
+
export type EmittedArtifact = {
filename: string;
content: string | Uint8Array;
frame: number;
+ downloadBehavior: DownloadBehavior | null;
};
diff --git a/packages/serverless-client/src/constants.ts b/packages/serverless-client/src/constants.ts
index 39a4281d1a..d5dee26f0a 100644
--- a/packages/serverless-client/src/constants.ts
+++ b/packages/serverless-client/src/constants.ts
@@ -14,6 +14,7 @@ import type {
X264Preset,
} from '@remotion/renderer';
import type {BrowserSafeApis} from '@remotion/renderer/client';
+import type {DownloadBehavior} from 'remotion';
import type {ExpensiveChunk} from './most-expensive-chunks';
import type {ChunkRetry, CloudProvider, ReceivedArtifact} from './types';
import type {EnhancedErrorInfo} from './write-error-to-storage';
@@ -84,15 +85,6 @@ export const serverlessCodecs = [
export type ServerlessCodec = (typeof serverlessCodecs)[number];
export type Privacy = 'public' | 'private' | 'no-acl';
-export type DownloadBehavior =
- | {
- type: 'play-in-browser';
- }
- | {
- type: 'download';
- fileName: string | null;
- };
-
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
diff --git a/packages/serverless-client/src/index.ts b/packages/serverless-client/src/index.ts
index 05571e7a2f..b3804448f5 100644
--- a/packages/serverless-client/src/index.ts
+++ b/packages/serverless-client/src/index.ts
@@ -19,7 +19,6 @@ export {
type CustomCredentials,
type CustomCredentialsWithoutSensitiveData,
type DeleteAfter,
- type DownloadBehavior,
type OutNameInput,
type OutNameInputWithoutCredentials,
type OutNameOutput,
@@ -53,7 +52,7 @@ export type {
export type {BrowserSafeApis} from '@remotion/renderer/client';
export {wrapWithErrorHandling} from '@remotion/renderer/error-handling';
export {makeStreamer, makeStreamPayloadMessage} from '@remotion/streaming';
-export type {VideoConfig} from 'remotion/no-react';
+export type {DownloadBehavior, VideoConfig} from 'remotion/no-react';
export {VERSION} from 'remotion/version';
export {Await} from './await';
export {calculateChunkTimes} from './calculate-chunk-times';
diff --git a/packages/serverless-client/src/provider-implementation.ts b/packages/serverless-client/src/provider-implementation.ts
index 5acdc65cff..6a8736fe48 100644
--- a/packages/serverless-client/src/provider-implementation.ts
+++ b/packages/serverless-client/src/provider-implementation.ts
@@ -1,11 +1,7 @@
import type {LogLevel} from '@remotion/renderer';
import type {Readable} from 'node:stream';
-import type {
- CustomCredentials,
- DownloadBehavior,
- Privacy,
- ServerlessRoutines,
-} from './constants';
+import type {DownloadBehavior} from 'remotion';
+import type {CustomCredentials, Privacy, ServerlessRoutines} from './constants';
import type {RenderMetadata} from './render-metadata';
import type {ServerlessReturnValues} from './return-values';
import type {OnMessage} from './streaming/streaming';
diff --git a/packages/serverless-client/src/render-metadata.ts b/packages/serverless-client/src/render-metadata.ts
index 1c1074acc8..b5cd131ab3 100644
--- a/packages/serverless-client/src/render-metadata.ts
+++ b/packages/serverless-client/src/render-metadata.ts
@@ -4,8 +4,8 @@ import type {
StillImageFormat,
VideoImageFormat,
} from '@remotion/renderer';
+import type {DownloadBehavior} from 'remotion';
import type {
- DownloadBehavior,
OutNameInputWithoutCredentials,
Privacy,
SerializedInputProps,
diff --git a/packages/serverless-client/src/serialize-artifact.ts b/packages/serverless-client/src/serialize-artifact.ts
index 4fedf11913..2c40261afc 100644
--- a/packages/serverless-client/src/serialize-artifact.ts
+++ b/packages/serverless-client/src/serialize-artifact.ts
@@ -1,10 +1,12 @@
import type {EmittedArtifact} from '@remotion/renderer';
+import type {DownloadBehavior} from 'remotion/no-react';
export type SerializedArtifact = {
filename: string;
stringContent: string;
frame: number;
binary: boolean;
+ downloadBehavior: DownloadBehavior | null;
};
export const deserializeArtifact = (
@@ -21,6 +23,7 @@ export const deserializeArtifact = (
filename: serializedArtifact.filename,
content: bytes,
frame: serializedArtifact.frame,
+ downloadBehavior: serializedArtifact.downloadBehavior,
};
}
@@ -28,6 +31,7 @@ export const deserializeArtifact = (
filename: serializedArtifact.filename,
content: serializedArtifact.stringContent,
frame: serializedArtifact.frame,
+ downloadBehavior: serializedArtifact.downloadBehavior,
};
};
@@ -48,6 +52,7 @@ export const serializeArtifact = (
stringContent: b64encoded,
frame: artifact.frame,
binary: true,
+ downloadBehavior: artifact.downloadBehavior,
};
}
@@ -56,5 +61,6 @@ export const serializeArtifact = (
stringContent: artifact.content,
frame: artifact.frame,
binary: false,
+ downloadBehavior: artifact.downloadBehavior,
};
};
diff --git a/packages/serverless-client/src/validate-download-behavior.ts b/packages/serverless-client/src/validate-download-behavior.ts
index 66c53b7f77..b0ca3409f7 100644
--- a/packages/serverless-client/src/validate-download-behavior.ts
+++ b/packages/serverless-client/src/validate-download-behavior.ts
@@ -1,4 +1,4 @@
-import type {DownloadBehavior} from './constants';
+import type {DownloadBehavior} from 'remotion/no-react';
export const validateDownloadBehavior = (downloadBehavior: unknown) => {
if (downloadBehavior === null || downloadBehavior === undefined) {
diff --git a/packages/serverless/src/handlers/launch.ts b/packages/serverless/src/handlers/launch.ts
index dc69b95145..5fe2171155 100644
--- a/packages/serverless/src/handlers/launch.ts
+++ b/packages/serverless/src/handlers/launch.ts
@@ -414,7 +414,7 @@ const innerLaunchHandler = async <Provider extends CloudProvider>({
region,
privacy: params.privacy,
expectedBucketOwner: options.expectedBucketOwner,
- downloadBehavior: params.downloadBehavior,
+ downloadBehavior: artifact.downloadBehavior ?? params.downloadBehavior,
customCredentials,
forcePathStyle: params.forcePathStyle,
})
diff --git a/packages/serverless/src/merge-chunks.ts b/packages/serverless/src/merge-chunks.ts
index 0d20f0cffb..cb7cc00d82 100644
--- a/packages/serverless/src/merge-chunks.ts
+++ b/packages/serverless/src/merge-chunks.ts
@@ -5,11 +5,11 @@ import type {
LogLevel,
} from '@remotion/renderer';
+import type {DownloadBehavior} from '@remotion/serverless-client';
import {
inspectErrors,
type CloudProvider,
type CustomCredentials,
- type DownloadBehavior,
type PostRenderData,
type Privacy,
type ProviderSpecifics,