packages/docs/docs/artifacts.mdx
Sometimes you wish to generate additional files when rendering your video. For example:
.srt subtitle file.txt containing chapters of the videoCREDITS file for the assets used in the videoYou can use the <Artifact> component to emit arbitrary files from your video.
:::note
Emitting artifacts is not currently supported by @remotion/cloudrun.
:::
// @filename: subtitles.tsx
export const generateSubtitles = () => {
return ``;
};
// @filename: MyComp.tsx
// ---cut---
import React from 'react';
import {Artifact, useCurrentFrame} from 'remotion';
import {generateSubtitles} from './subtitles';
export const MyComp: React.FC = () => {
const frame = useCurrentFrame();
return <>{frame === 0 ? <Artifact filename="captions.srt" content={generateSubtitles()} /> : null}</>;
};
<Step>1</Step> The asset should only be rendered for one single frame of the video. Otherwise, the asset will get emitted multiple times.
<Step>2</Step> It is possible to emit multiple assets, but they may not have the same filename.
<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.
You can emit the image data of the current frame as an artifact.
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 prop should be set to Artifact.Thumbnail.
<Step>2</Step> The imageFormat setting determines the format of the image. The extension you pass is meaningless.
<Step>3</Step> The other rules of artifacts still apply.
Artifacts get saved to out/[composition-id]/[filename] when rendering a video.
renderMedia(), renderStill() or renderFrames()Use the onArtifact callback to receive the artifacts.
import type {VideoConfig} from 'remotion';
const composition: VideoConfig = {
width: 100,
height: 100,
fps: 30,
defaultProps: {},
props: {},
defaultCodec: null,
defaultOutName: null,
defaultVideoImageFormat: null,
defaultPixelFormat: null,
defaultProResProfile: null,
defaultSampleRate: null,
id: 'hi',
durationInFrames: 100,
};
const serveUrl = 'http://localhost:8080';
const inputProps = {};
import fs from 'fs';
// ---cut---
import {renderMedia, OnArtifact} from '@remotion/renderer';
const onArtifact: OnArtifact = (artifact) => {
console.log(artifact.filename); // string
console.log(artifact.content); // string | Uint8Array
console.log(artifact.frame); // number, frame in the composition which emitted this
// Example action: Write the artifact to disk
fs.writeFileSync(artifact.filename, artifact.content);
};
await renderMedia({
composition,
serveUrl,
onArtifact,
codec: 'h264',
inputProps,
});
renderMediaOnWeb() or renderStillOnWeb()Use the onArtifact callback to receive artifacts when rendering using renderMediaOnWeb().
import React from 'react';
import {Artifact, useCurrentFrame} from 'remotion';
import {renderMediaOnWeb, EmittedArtifact} from '@remotion/web-renderer';
const composition = {
id: 'my-comp',
width: 1920,
height: 1080,
fps: 30,
durationInFrames: 90,
};
// ---cut---
const MyComp: React.FC = () => {
const frame = useCurrentFrame();
return <>{frame === 0 && <Artifact filename="metadata.json" content={JSON.stringify({title: 'My Video'})} />}</>;
};
const artifacts: EmittedArtifact[] = [];
await renderMediaOnWeb({
composition: {
...composition,
component: MyComp,
},
onArtifact: (artifact) => {
console.log(artifact.filename); // string
console.log(artifact.content); // string | Uint8Array
console.log(artifact.frame); // number
artifacts.push(artifact);
},
});
The same callback is available for renderStillOnWeb():
import React from 'react';
import {Artifact} from 'remotion';
import {renderStillOnWeb, EmittedArtifact} from '@remotion/web-renderer';
const composition = {
id: 'my-comp',
width: 1920,
height: 1080,
fps: 30,
durationInFrames: 1,
};
// ---cut---
const MyComp: React.FC = () => {
return <Artifact filename="thumbnail.png" content={Artifact.Thumbnail} />;
};
const artifacts: EmittedArtifact[] = [];
await renderStillOnWeb({
composition: {
...composition,
component: MyComp,
},
frame: 0,
onArtifact: (artifact) => {
artifacts.push(artifact);
},
});
When using npx remotion lambda render or npx remotion lambda still, artifacts get saved to the S3 bucket under the key renders/[render-id]/artifacts/[filename].
They will get logged to the console and you can click them to download them.
The --privacy option also applies to artifacts.
renderMediaOnLambda()When using renderMediaOnLambda(), artifacts get saved to the S3 bucket under the key renders/[render-id]/artifacts/[filename].
You can obtain a list of currently received assets from getRenderProgress().
import {getRenderProgress} from '@remotion/lambda/client';
const renderProgress = await getRenderProgress({
renderId: 'hi',
functionName: 'hi',
bucketName: 'hi',
region: 'eu-central-1',
});
for (const artifact of renderProgress.artifacts) {
console.log(artifact.filename); // "hello-world.txt"
console.log(artifact.sizeInBytes); // 12
console.log(artifact.s3Url); // "https://s3.eu-central-1.amazonaws.com/remotion-lambda-abcdef/renders/abcdef/artifacts/hello-world.txt"
console.log(artifact.s3Key); // "renders/abcdef/artifacts/hello-world.txt"
}
renderStillOnLambda()When using renderStillOnLambda(), artifacts get saved to the S3 bucket under the key renders/[render-id]/artifacts/[filename].
You can obtain a list of received assets from artifacts field of renderStillOnLambda().
const serveUrl = 'http://localhost:8080';
const inputProps = {};
const functionName = 'hi';
const composition = 'hi';
const privacy = 'public' as const;
const imageFormat = 'png' as const;
const region = 'eu-central-1' as const;
// ---cut---
import {renderStillOnLambda} from '@remotion/lambda/client';
const stillResponse = await renderStillOnLambda({
functionName,
region,
serveUrl,
composition,
inputProps,
imageFormat,
privacy,
});
for (const artifact of stillResponse.artifacts) {
console.log(artifact.filename); // "hello-world.txt"
console.log(artifact.sizeInBytes); // 12
console.log(artifact.s3Url); // "https://s3.eu-central-1.amazonaws.com/remotion-lambda-abcdef/renders/abcdef/artifacts/hello-world.txt"
console.log(artifact.s3Key); // "renders/abcdef/artifacts/hello-world.txt"
}
In the Cloud Run Alpha, emitting artifacts is not supported and will throw an error.
We plan on revising Cloud Run to use the same runtime as Lambda in the future and will bring this feature along.