Skip to content

🎥 Make videos programmatically with React

artifact

Jonny Burgerb95253e2a5f1
diff --git a/packages/core/src/Artifact.tsx b/packages/core/src/Artifact.tsx
index 9c38bf60d7..b2fd295a43 100644
--- a/packages/core/src/Artifact.tsx
+++ b/packages/core/src/Artifact.tsx
@@ -1,15 +1,17 @@
import type React from 'react';
import {useContext, useEffect, useState} from 'react';
import {RenderAssetManager} from './RenderAssetManager';
+import {useCurrentFrame} from './use-current-frame';
// TODO: Not register in development?
export const Artifact: React.FC<{
- filename: string;
- content: string;
+ readonly filename: string;
+ readonly content: string;
}> = ({filename, content}) => {
// TODO: Validate filename and content
const {registerRenderAsset, unregisterRenderAsset} =
useContext(RenderAssetManager);
+ const frame = useCurrentFrame();
const [id] = useState(() => {
return String(Math.random());
@@ -21,12 +23,20 @@ export const Artifact: React.FC<{
id,
content,
filename,
+ frame,
});
return () => {
return unregisterRenderAsset(id);
};
- }, [content, filename, id, registerRenderAsset, unregisterRenderAsset]);
+ }, [
+ content,
+ filename,
+ frame,
+ id,
+ registerRenderAsset,
+ unregisterRenderAsset,
+ ]);
return null;
};
diff --git a/packages/core/src/CompositionManager.tsx b/packages/core/src/CompositionManager.tsx
index b21e617883..7bed4510d9 100644
--- a/packages/core/src/CompositionManager.tsx
+++ b/packages/core/src/CompositionManager.tsx
@@ -144,6 +144,7 @@ export type ArtifactAsset = {
id: string;
filename: string;
content: string;
+ frame: number;
};
export type TRenderAsset = AudioOrVideoAsset | ArtifactAsset;
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 20e943ef7f..ee5ed32611 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -4,9 +4,9 @@ import {
CalculateMetadataFunction,
Composition,
Folder,
- Still,
getInputProps,
staticFile,
+ Still,
} from 'remotion';
import {z} from 'zod';
import {TwentyTwoKHzAudio} from './22KhzAudio';
@@ -32,8 +32,8 @@ import {Layers} from './Layers';
import {ManyAudio} from './ManyAudio';
import {MissingImg} from './MissingImg';
import {
- OffthreadRemoteVideo,
calculateMetadataFn,
+ OffthreadRemoteVideo,
} from './OffthreadRemoteVideo/OffthreadRemoteVideo';
import {OrbScene} from './Orb';
import {ShapesMorph} from './Paths/ShapesMorph';
@@ -47,8 +47,8 @@ import RiveVehicle from './Rive/RiveExample';
import {ScalePath} from './ScalePath';
import {
ArrayTest,
- SchemaTest,
schemaArrayTestSchema,
+ SchemaTest,
schemaTestSchema,
} from './SchemaTest';
import {Scripts} from './Scripts';
@@ -74,6 +74,7 @@ import {
} from './StudioApis/SaveDefaultProps';
import {TriggerCalculateMetadata} from './StudioApis/TriggerCalculateMetadata';
import {WriteStaticFile} from './StudioApis/WriteStaticFile';
+import './style.css';
import {Tailwind} from './Tailwind';
import {TenFrameTester} from './TenFrameTester';
import {TextStroke} from './TextStroke';
@@ -90,7 +91,6 @@ import {VideoSpeed} from './VideoSpeed';
import {VideoTesting} from './VideoTesting';
import {WarpDemoOuter} from './WarpText';
import {WarpDemo2} from './WarpText/demo2';
-import './style.css';
import {WatchStaticDemo} from './watch-static';
if (alias !== 'alias') {
throw new Error('should support TS aliases');
@@ -98,6 +98,7 @@ if (alias !== 'alias') {
// @ts-expect-error no types
import styles from './styles.module.scss';
+import {SubtitleArtifact} from './SubtitleArtifact/SubtitleArtifact';
if (!styles.hithere) {
throw new Error('should support SCSS modules');
@@ -1309,6 +1310,16 @@ export const Index: React.FC = () => {
defaultProps={{color: 'green'}}
/>
</Folder>
+ <Folder name="Artifacts">
+ <Composition
+ id="subtitle"
+ component={SubtitleArtifact}
+ fps={30}
+ height={1000}
+ width={1000}
+ durationInFrames={10}
+ />
+ </Folder>
</>
);
};
diff --git a/packages/example/src/SubtitleArtifact/SubtitleArtifact.tsx b/packages/example/src/SubtitleArtifact/SubtitleArtifact.tsx
new file mode 100644
index 0000000000..56e892fdcf
--- /dev/null
+++ b/packages/example/src/SubtitleArtifact/SubtitleArtifact.tsx
@@ -0,0 +1,6 @@
+import React from 'react';
+import {Experimental} from 'remotion';
+
+export const SubtitleArtifact: React.FC = () => {
+ return <Experimental.Artifact content="Hello World!" filename="hello.txt" />;
+};
diff --git a/packages/renderer/src/render-frames.ts b/packages/renderer/src/render-frames.ts
index e587592ecb..34c5746f48 100644
--- a/packages/renderer/src/render-frames.ts
+++ b/packages/renderer/src/render-frames.ts
@@ -498,9 +498,27 @@ const innerRenderFrames = async ({
.map((a) => a.audioAndVideoAssets)
.flat(2);
+ const previousArtifactAssets = assets
+ .filter(truthy)
+ .map((a) => a.artifactAssets)
+ .flat(2);
+
const audioAndVideoAssets = onlyAudioAndVideoAssets(collectedAssets);
const artifactAssets = onlyArtifact(collectedAssets);
+ for (const artifact of artifactAssets) {
+ for (const previousArtifact of previousArtifactAssets) {
+ 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}.`,
+ ),
+ );
+ return;
+ }
+ }
+ }
+
const compressedAssets = audioAndVideoAssets.map((asset) => {
return compressAsset(previousAudioRenderAssets, asset);
});