Skip to content

🎥 Make videos programmatically with React

feature done

Jonny Burger904cbfcf4b7a
diff --git a/packages/docs/docs/artifact.mdx b/packages/docs/docs/artifact.mdx
index 0398d80bc7..f54ac732cd 100644
--- a/packages/docs/docs/artifact.mdx
+++ b/packages/docs/docs/artifact.mdx
@@ -14,9 +14,7 @@ import {Artifact, useCurrentFrame} from 'remotion';
export const MyComp: React.FC = () => {
const frame = useCurrentFrame();
- return frame === 0 ? (
- <Artifact filename="my-file.txt" content="Hello World!" />
- ) : null;
+ return frame === 0 ? <Artifact filename="my-file.txt" content="Hello World!" /> : null;
};
```
@@ -39,9 +37,7 @@ import {Artifact, useCurrentFrame} from 'remotion';
export const MyComp: React.FC = () => {
const frame = useCurrentFrame();
- return frame === 0 ? (
- <Artifact filename="my-file.txt" content="Hello World!" />
- ) : null;
+ return frame === 0 ? <Artifact filename="my-file.txt" content="Hello World!" /> : null;
};
```
@@ -57,6 +53,21 @@ 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.
+## `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).
+
+```tsx twoslash title="Emitting the first frame as a thumbnail"
+import {Artifact, useCurrentFrame} from 'remotion';
+
+export const MyComp: React.FC = () => {
+ const frame = useCurrentFrame();
+ return <>{frame === 0 ? <Artifact content={Artifact.Thumbnail} filename="thumbnail.jpeg" /> : null}</>;
+};
+```
+
+See the [Emitting Thumbnails](/docs/artifacts#emitting-thumbnails) page for more important information.
+
## See also
- [Emitting Artifacts](/docs/artifacts)
diff --git a/packages/docs/docs/artifacts.mdx b/packages/docs/docs/artifacts.mdx
index da73d962fd..37c84da487 100644
--- a/packages/docs/docs/artifacts.mdx
+++ b/packages/docs/docs/artifacts.mdx
@@ -44,11 +44,28 @@ export const MyComp: React.FC = () => {
<Step>1</Step> The asset should only be rendered for one single frame of the video. Otherwise, the asset will get emitted multiple times. <br />
-<Step>2</Step> It is possible to emit multiple assets, but they may not have the same filename.
-<br />
+<Step>2</Step> It is possible to emit multiple assets, but they may not have the same filename. <br />
<Step>3</Step> For the <code>content</code> prop it is possible to pass a <code>string</code>, or a <code>Uint8Array</code> for binary data. Passing an <code>Uint8Array</code> should not be considered faster due to it having to be serialized.
-<br />
+
+## Emitting thumbnails<AvailableFrom v="4.0.290" />
+
+You can emit the image data of the current frame as an artifact.
+
+```tsx twoslash title="Emitting the first frame as a thumbnail"
+import {Artifact, useCurrentFrame} from 'remotion';
+
+export const MyComp: React.FC = () => {
+ const frame = useCurrentFrame();
+ return <>{frame === 0 ? <Artifact content={Artifact.Thumbnail} filename="thumbnail.jpeg" /> : null}</>;
+};
+```
+
+<Step>1</Step> The [`content`](/docs/artifact#content) prop should be set to [`Artifact.Thumbnail`](/docs/artifact#thumbnail). <br />
+
+<Step>2</Step> The [`imageFormat`](/docs/renderer/render-media#imageformat) setting determines the format of the image. The extension you pass is meaningless. <br />
+
+<Step>3</Step> The other rules of artifacts still apply. <br />
## Receiving artifacts
diff --git a/packages/lambda/src/cli/commands/render/progress.ts b/packages/lambda/src/cli/commands/render/progress.ts
index 91899a0705..5fca6ef159 100644
--- a/packages/lambda/src/cli/commands/render/progress.ts
+++ b/packages/lambda/src/cli/commands/render/progress.ts
@@ -219,7 +219,7 @@ export const makeArtifactProgress = <Provider extends CloudProvider>(
CliInternals.chalk.blue(
CliInternals.makeHyperlink({
url: artifact.s3Url,
- fallback: artifact.filename,
+ fallback: artifact.s3Key,
text: artifact.s3Key,
}),
),
diff --git a/packages/renderer/src/filter-asset-types.ts b/packages/renderer/src/filter-asset-types.ts
index a397a237d7..82db928840 100644
--- a/packages/renderer/src/filter-asset-types.ts
+++ b/packages/renderer/src/filter-asset-types.ts
@@ -4,6 +4,7 @@ import type {
TRenderAsset,
} from 'remotion/no-react';
import type {EmittedArtifact} from './serialize-artifact';
+import {truthy} from './truthy';
export const onlyAudioAndVideoAssets = (
assets: TRenderAsset[],
@@ -24,29 +25,35 @@ export const onlyArtifact = ({
(asset) => asset.type === 'artifact',
) as ArtifactAsset[];
- return artifacts.map((artifact): EmittedArtifact => {
- if (artifact.contentType === 'binary' || artifact.contentType === 'text') {
- return {
- frame: artifact.frame,
- content: artifact.content,
- filename: artifact.filename,
- };
- }
-
- if (artifact.contentType === 'thumbnail') {
- if (frameBuffer === null) {
- throw new Error(
- 'A thumbnail artifact was defined to be emitted, but the output was not a video.',
- );
+ return artifacts
+ .map((artifact): EmittedArtifact | null => {
+ if (
+ artifact.contentType === 'binary' ||
+ artifact.contentType === 'text'
+ ) {
+ return {
+ frame: artifact.frame,
+ content: artifact.content,
+ filename: artifact.filename,
+ };
}
- return {
- frame: artifact.frame,
- content: new Uint8Array(frameBuffer),
- filename: artifact.filename,
- };
- }
+ if (artifact.contentType === 'thumbnail') {
+ if (frameBuffer === null) {
+ // A thumbnail artifact was defined to be emitted, but the output was not a video.
+ // Also, in Lambda, there are extra frames which are not video frames.
+ // This could happen if a thumbnail is unconditionally emitted.
+ return null;
+ }
+
+ return {
+ frame: artifact.frame,
+ content: new Uint8Array(frameBuffer),
+ filename: artifact.filename,
+ };
+ }
- throw new Error('Unknown artifact type: ' + (artifact satisfies never));
- });
+ throw new Error('Unknown artifact type: ' + (artifact satisfies never));
+ })
+ .filter(truthy);
};
diff --git a/packages/serverless-client/src/serialize-artifact.ts b/packages/serverless-client/src/serialize-artifact.ts
index 50e26c8998..4fedf11913 100644
--- a/packages/serverless-client/src/serialize-artifact.ts
+++ b/packages/serverless-client/src/serialize-artifact.ts
@@ -11,13 +11,15 @@ export const deserializeArtifact = (
serializedArtifact: SerializedArtifact,
): EmittedArtifact => {
if (serializedArtifact.binary) {
- const content = new TextEncoder().encode(
- atob(serializedArtifact.stringContent),
- );
+ const binaryString = atob(serializedArtifact.stringContent);
+ const bytes = new Uint8Array(binaryString.length);
+ for (let i = 0; i < binaryString.length; i++) {
+ bytes[i] = binaryString.charCodeAt(i);
+ }
return {
filename: serializedArtifact.filename,
- content,
+ content: bytes,
frame: serializedArtifact.frame,
};
}
@@ -33,7 +35,14 @@ export const serializeArtifact = (
artifact: EmittedArtifact,
): SerializedArtifact => {
if (artifact.content instanceof Uint8Array) {
- const b64encoded = btoa(new TextDecoder('utf8').decode(artifact.content));
+ let binary = '';
+ const bytes = new Uint8Array(artifact.content);
+ const len = bytes.byteLength;
+ for (let i = 0; i < len; i++) {
+ binary += String.fromCharCode(bytes[i]);
+ }
+
+ const b64encoded = btoa(binary);
return {
filename: artifact.filename,
stringContent: b64encoded,