Skip to content

🎥 Make videos programmatically with React

use it as composition id

JonnyBurgerdc6ef0436a69
diff --git a/packages/template-ai-video/cli/cli.ts b/packages/template-ai-video/cli/cli.ts
index c75d52bb97..b0565febaa 100644
--- a/packages/template-ai-video/cli/cli.ts
+++ b/packages/template-ai-video/cli/cli.ts
@@ -79,8 +79,8 @@ class ContentFS {
getSlug(): string {
return this.title
.toLowerCase()
- .replace(/[^a-z0-9]+/g, "_")
- .replace(/^_+|_+$/g, "");
+ .replace(/[^a-z0-9]+/g, "-")
+ .replace(/^-+|-+$/g, "");
}
}
@@ -130,14 +130,14 @@ async function generateStory(options: GenerateOptions) {
{
type: "text",
name: "title",
- message: "Enter the title of the story:",
+ message: "Title of the story:",
initial: title,
validate: (value) => value.length > 0 || "Title is required",
},
{
type: "text",
name: "topic",
- message: "Enter the topic of the story:",
+ message: "Topic of the story:",
initial: topic,
validate: (value) => value.length > 0 || "Topic is required",
},
@@ -197,10 +197,13 @@ async function generateStory(options: GenerateOptions) {
for (let i = 0; i < storyWithDetails.content.length; i++) {
const storyItem = storyWithDetails.content[i];
imagesSpinner.text = `[${i * 2 + 1}/${storyWithDetails.content.length * 2}] Generating image for ${storyItem.text}`;
- await generateAiImage(
- storyItem.imageDescription,
- contentFs.getImagePath(storyItem.uid),
- );
+ await generateAiImage({
+ prompt: storyItem.imageDescription,
+ path: contentFs.getImagePath(storyItem.uid),
+ onRetry: (attempt) => {
+ imagesSpinner.text = `[${i * 2 + 1}/${storyWithDetails.content.length * 2}] Generating image for ${storyItem.text} (retry ${attempt + 1})`;
+ },
+ });
imagesSpinner.text = `[${i * 2 + 2}/${storyWithDetails.content.length * 2}] Generating voice for ${storyItem.text}`;
const timings = await generateVoice(
storyItem.text,
@@ -218,6 +221,7 @@ async function generateStory(options: GenerateOptions) {
finalSpinner.succeed(chalk.green("Final result generated!"));
console.log(chalk.green.bold("\n✨ Story generation complete!\n"));
+ console.log("Run " + chalk.blue("npm run dev") + " to preview the story");
return {};
} catch (error) {
diff --git a/packages/template-ai-video/cli/service.ts b/packages/template-ai-video/cli/service.ts
index 29da6cd666..35fbfad337 100644
--- a/packages/template-ai-video/cli/service.ts
+++ b/packages/template-ai-video/cli/service.ts
@@ -61,28 +61,56 @@ function saveUint8ArrayToPng(uint8Array: Uint8Array, filePath: string) {
fs.writeFileSync(filePath, buffer as Uint8Array);
}
-export const generateAiImage = async (prompt: string, path: string) => {
- const res = await fetch("https://api.openai.com/v1/images/generations", {
- method: "POST",
- headers: {
- Authorization: `Bearer ${apiKey}`,
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- model: "dall-e-3",
- prompt,
- size: `${IMAGE_WIDTH}x${IMAGE_HEIGHT}`,
- response_format: "b64_json",
- }),
- });
-
- if (!res.ok) throw new Error(`OpenAI error: ${await res.text()}`);
-
- const data = await res.json();
- const buffer = Buffer.from(data.data[0].b64_json, "base64");
- const uint8Array = new Uint8Array(buffer);
+export const generateAiImage = async ({
+ prompt,
+ path,
+ onRetry,
+}: {
+ prompt: string;
+ path: string;
+ onRetry: (attempt: number) => void;
+}) => {
+ const maxRetries = 3;
+ let attempt = 0;
+ let lastError: Error | null = null;
+
+ while (attempt < maxRetries) {
+ const res = await fetch("https://api.openai.com/v1/images/generations", {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${apiKey}`,
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ model: "dall-e-3",
+ prompt,
+ size: `${IMAGE_WIDTH}x${IMAGE_HEIGHT}`,
+ response_format: "b64_json",
+ }),
+ });
+
+ if (res.ok) {
+ const data = await res.json();
+ const buffer = Buffer.from(data.data[0].b64_json, "base64");
+ const uint8Array = new Uint8Array(buffer);
+
+ saveUint8ArrayToPng(uint8Array, path);
+ return;
+ } else {
+ lastError = new Error(
+ `OpenAI error (attempt ${attempt + 1}): ${await res.text()}`,
+ );
+ attempt++;
+ if (attempt < maxRetries) {
+ // Wait 1 second before retrying
+ await new Promise((resolve) => setTimeout(resolve, 1000));
+ }
+ onRetry(attempt);
+ }
+ }
- saveUint8ArrayToPng(uint8Array, path);
+ // Ran out of retries, throw the last error
+ throw lastError!;
};
export const getGenerateStoryPrompt = (title: string, topic: string) => {
diff --git a/packages/template-ai-video/public/content/history_of_venus/audio/11f4911a-f39f-4567-8963-41511d05b593.mp3 b/packages/template-ai-video/public/content/history-of-venus/audio/11f4911a-f39f-4567-8963-41511d05b593.mp3
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/audio/11f4911a-f39f-4567-8963-41511d05b593.mp3
rename to packages/template-ai-video/public/content/history-of-venus/audio/11f4911a-f39f-4567-8963-41511d05b593.mp3
diff --git a/packages/template-ai-video/public/content/history_of_venus/audio/20cf0ab8-49cb-4ead-816d-6af0b588668a.mp3 b/packages/template-ai-video/public/content/history-of-venus/audio/20cf0ab8-49cb-4ead-816d-6af0b588668a.mp3
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/audio/20cf0ab8-49cb-4ead-816d-6af0b588668a.mp3
rename to packages/template-ai-video/public/content/history-of-venus/audio/20cf0ab8-49cb-4ead-816d-6af0b588668a.mp3
diff --git a/packages/template-ai-video/public/content/history_of_venus/audio/33b24fbd-dc71-4692-87af-fdff7661c222.mp3 b/packages/template-ai-video/public/content/history-of-venus/audio/33b24fbd-dc71-4692-87af-fdff7661c222.mp3
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/audio/33b24fbd-dc71-4692-87af-fdff7661c222.mp3
rename to packages/template-ai-video/public/content/history-of-venus/audio/33b24fbd-dc71-4692-87af-fdff7661c222.mp3
diff --git a/packages/template-ai-video/public/content/history_of_venus/audio/5584e376-d02e-48fc-9e47-fc7bf86000af.mp3 b/packages/template-ai-video/public/content/history-of-venus/audio/5584e376-d02e-48fc-9e47-fc7bf86000af.mp3
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/audio/5584e376-d02e-48fc-9e47-fc7bf86000af.mp3
rename to packages/template-ai-video/public/content/history-of-venus/audio/5584e376-d02e-48fc-9e47-fc7bf86000af.mp3
diff --git a/packages/template-ai-video/public/content/history_of_venus/audio/83e869e5-18fd-4de9-b70f-dc0f180da912.mp3 b/packages/template-ai-video/public/content/history-of-venus/audio/83e869e5-18fd-4de9-b70f-dc0f180da912.mp3
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/audio/83e869e5-18fd-4de9-b70f-dc0f180da912.mp3
rename to packages/template-ai-video/public/content/history-of-venus/audio/83e869e5-18fd-4de9-b70f-dc0f180da912.mp3
diff --git a/packages/template-ai-video/public/content/history_of_venus/audio/90917ba0-8b7c-4309-a784-4f1fdc28d9fb.mp3 b/packages/template-ai-video/public/content/history-of-venus/audio/90917ba0-8b7c-4309-a784-4f1fdc28d9fb.mp3
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/audio/90917ba0-8b7c-4309-a784-4f1fdc28d9fb.mp3
rename to packages/template-ai-video/public/content/history-of-venus/audio/90917ba0-8b7c-4309-a784-4f1fdc28d9fb.mp3
diff --git a/packages/template-ai-video/public/content/history_of_venus/audio/919d538b-3407-440a-b26a-73a5321a3af0.mp3 b/packages/template-ai-video/public/content/history-of-venus/audio/919d538b-3407-440a-b26a-73a5321a3af0.mp3
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/audio/919d538b-3407-440a-b26a-73a5321a3af0.mp3
rename to packages/template-ai-video/public/content/history-of-venus/audio/919d538b-3407-440a-b26a-73a5321a3af0.mp3
diff --git a/packages/template-ai-video/public/content/history_of_venus/audio/99af6411-714b-4b72-b66e-f6c02a184493.mp3 b/packages/template-ai-video/public/content/history-of-venus/audio/99af6411-714b-4b72-b66e-f6c02a184493.mp3
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/audio/99af6411-714b-4b72-b66e-f6c02a184493.mp3
rename to packages/template-ai-video/public/content/history-of-venus/audio/99af6411-714b-4b72-b66e-f6c02a184493.mp3
diff --git a/packages/template-ai-video/public/content/history_of_venus/descriptor.json b/packages/template-ai-video/public/content/history-of-venus/descriptor.json
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/descriptor.json
rename to packages/template-ai-video/public/content/history-of-venus/descriptor.json
diff --git a/packages/template-ai-video/public/content/history_of_venus/images/11f4911a-f39f-4567-8963-41511d05b593.png b/packages/template-ai-video/public/content/history-of-venus/images/11f4911a-f39f-4567-8963-41511d05b593.png
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/images/11f4911a-f39f-4567-8963-41511d05b593.png
rename to packages/template-ai-video/public/content/history-of-venus/images/11f4911a-f39f-4567-8963-41511d05b593.png
diff --git a/packages/template-ai-video/public/content/history_of_venus/images/20cf0ab8-49cb-4ead-816d-6af0b588668a.png b/packages/template-ai-video/public/content/history-of-venus/images/20cf0ab8-49cb-4ead-816d-6af0b588668a.png
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/images/20cf0ab8-49cb-4ead-816d-6af0b588668a.png
rename to packages/template-ai-video/public/content/history-of-venus/images/20cf0ab8-49cb-4ead-816d-6af0b588668a.png
diff --git a/packages/template-ai-video/public/content/history_of_venus/images/33b24fbd-dc71-4692-87af-fdff7661c222.png b/packages/template-ai-video/public/content/history-of-venus/images/33b24fbd-dc71-4692-87af-fdff7661c222.png
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/images/33b24fbd-dc71-4692-87af-fdff7661c222.png
rename to packages/template-ai-video/public/content/history-of-venus/images/33b24fbd-dc71-4692-87af-fdff7661c222.png
diff --git a/packages/template-ai-video/public/content/history_of_venus/images/5584e376-d02e-48fc-9e47-fc7bf86000af.png b/packages/template-ai-video/public/content/history-of-venus/images/5584e376-d02e-48fc-9e47-fc7bf86000af.png
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/images/5584e376-d02e-48fc-9e47-fc7bf86000af.png
rename to packages/template-ai-video/public/content/history-of-venus/images/5584e376-d02e-48fc-9e47-fc7bf86000af.png
diff --git a/packages/template-ai-video/public/content/history_of_venus/images/83e869e5-18fd-4de9-b70f-dc0f180da912.png b/packages/template-ai-video/public/content/history-of-venus/images/83e869e5-18fd-4de9-b70f-dc0f180da912.png
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/images/83e869e5-18fd-4de9-b70f-dc0f180da912.png
rename to packages/template-ai-video/public/content/history-of-venus/images/83e869e5-18fd-4de9-b70f-dc0f180da912.png
diff --git a/packages/template-ai-video/public/content/history_of_venus/images/90917ba0-8b7c-4309-a784-4f1fdc28d9fb.png b/packages/template-ai-video/public/content/history-of-venus/images/90917ba0-8b7c-4309-a784-4f1fdc28d9fb.png
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/images/90917ba0-8b7c-4309-a784-4f1fdc28d9fb.png
rename to packages/template-ai-video/public/content/history-of-venus/images/90917ba0-8b7c-4309-a784-4f1fdc28d9fb.png
diff --git a/packages/template-ai-video/public/content/history_of_venus/images/919d538b-3407-440a-b26a-73a5321a3af0.png b/packages/template-ai-video/public/content/history-of-venus/images/919d538b-3407-440a-b26a-73a5321a3af0.png
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/images/919d538b-3407-440a-b26a-73a5321a3af0.png
rename to packages/template-ai-video/public/content/history-of-venus/images/919d538b-3407-440a-b26a-73a5321a3af0.png
diff --git a/packages/template-ai-video/public/content/history_of_venus/images/99af6411-714b-4b72-b66e-f6c02a184493.png b/packages/template-ai-video/public/content/history-of-venus/images/99af6411-714b-4b72-b66e-f6c02a184493.png
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/images/99af6411-714b-4b72-b66e-f6c02a184493.png
rename to packages/template-ai-video/public/content/history-of-venus/images/99af6411-714b-4b72-b66e-f6c02a184493.png
diff --git a/packages/template-ai-video/public/content/history_of_venus/timeline.json b/packages/template-ai-video/public/content/history-of-venus/timeline.json
similarity index 100%
rename from packages/template-ai-video/public/content/history_of_venus/timeline.json
rename to packages/template-ai-video/public/content/history-of-venus/timeline.json
diff --git a/packages/template-ai-video/src/Root.tsx b/packages/template-ai-video/src/Root.tsx
index 0eefd9783c..a7dbc6d9ac 100644
--- a/packages/template-ai-video/src/Root.tsx
+++ b/packages/template-ai-video/src/Root.tsx
@@ -1,36 +1,42 @@
-import { Composition } from "remotion";
+import { Composition, getStaticFiles } from "remotion";
import { AIVideo, aiVideoSchema } from "./components/AIVideo";
-import { FPS } from "./lib/constants";
+import { FPS, INTRO_DURATION } from "./lib/constants";
import { getTimelinePath, loadTimelineFromFile } from "./lib/utils";
export const RemotionRoot: React.FC = () => {
+ const staticFiles = getStaticFiles();
+ const timelines = staticFiles
+ .filter((file) => file.name.endsWith("timeline.json"))
+ .map((file) => file.name.split("/")[1]);
+
return (
<>
- <Composition
- id="AIVideo"
- component={AIVideo}
- fps={FPS}
- width={1080}
- height={1920}
- schema={aiVideoSchema}
- defaultProps={{
- projectName: "history_of_venus",
- timeline: null,
- }}
- calculateMetadata={async ({ props }) => {
- const { lengthFrames, timeline } = await loadTimelineFromFile(
- getTimelinePath(props.projectName),
- );
+ {timelines.map((storyName) => (
+ <Composition
+ id={storyName}
+ component={AIVideo}
+ fps={FPS}
+ width={1080}
+ height={1920}
+ schema={aiVideoSchema}
+ defaultProps={{
+ timeline: null,
+ }}
+ calculateMetadata={async ({ props }) => {
+ const { lengthFrames, timeline } = await loadTimelineFromFile(
+ getTimelinePath(storyName),
+ );
- return {
- durationInFrames: lengthFrames,
- props: {
- ...props,
- timeline,
- },
- };
- }}
- />
+ return {
+ durationInFrames: lengthFrames + INTRO_DURATION,
+ props: {
+ ...props,
+ timeline,
+ },
+ };
+ }}
+ />
+ ))}
</>
);
};
diff --git a/packages/template-ai-video/src/components/AIVideo.tsx b/packages/template-ai-video/src/components/AIVideo.tsx
index b62582caf1..ce8e9578be 100644
--- a/packages/template-ai-video/src/components/AIVideo.tsx
+++ b/packages/template-ai-video/src/components/AIVideo.tsx
@@ -1,4 +1,4 @@
-import { AbsoluteFill, Sequence, staticFile } from "remotion";
+import { AbsoluteFill, Sequence, staticFile, useVideoConfig } from "remotion";
import { z } from "zod";
import { Audio } from "@remotion/media";
import { TimelineSchema } from "../lib/types";
@@ -9,20 +9,20 @@ import Subtitle from "./Subtitle";
import { calculateFrameTiming, getAudioPath } from "../lib/utils";
export const aiVideoSchema = z.object({
- projectName: z.string().min(1),
timeline: TimelineSchema.nullable(),
});
const { fontFamily } = loadFont();
export const AIVideo: React.FC<z.infer<typeof aiVideoSchema>> = ({
- projectName,
timeline,
}) => {
if (!timeline) {
throw new Error("Expected timeline to be fetched");
}
+ const { id } = useVideoConfig();
+
return (
<AbsoluteFill style={{ backgroundColor: "white" }}>
<Sequence durationInFrames={INTRO_DURATION}>
@@ -68,7 +68,7 @@ export const AIVideo: React.FC<z.infer<typeof aiVideoSchema>> = ({
durationInFrames={duration}
premountFor={3 * FPS}
>
- <Background project={projectName} item={element} />
+ <Background project={id} item={element} />
</Sequence>
);
})}
@@ -105,9 +105,7 @@ export const AIVideo: React.FC<z.infer<typeof aiVideoSchema>> = ({
durationInFrames={duration}
premountFor={3 * FPS}
>
- <Audio
- src={staticFile(getAudioPath(projectName, element.audioUrl))}
- />
+ <Audio src={staticFile(getAudioPath(id, element.audioUrl))} />
</Sequence>
);
})}
diff --git a/packages/template-ai-video/src/components/Background.tsx b/packages/template-ai-video/src/components/Background.tsx
index f41fab8b46..e2baf142b7 100644
--- a/packages/template-ai-video/src/components/Background.tsx
+++ b/packages/template-ai-video/src/components/Background.tsx
@@ -50,7 +50,7 @@ export const Background: React.FC<{
const currentBlur = maxBlur * blur;
return (
- <AbsoluteFill style={{ backgroundColor: "blue" }}>
+ <AbsoluteFill>
<Img
src={staticFile(getImagePath(project, item.imageUrl))}
style={{