Skip to content

🎥 Make videos programmatically with React

Add timeline loading from props

Vlad M029829302b89
diff --git a/packages/template-ai-video/src/AIVideo.tsx b/packages/template-ai-video/src/AIVideo.tsx
index 5a4cf40743..1b58e05268 100644
--- a/packages/template-ai-video/src/AIVideo.tsx
+++ b/packages/template-ai-video/src/AIVideo.tsx
@@ -1,8 +1,15 @@
import { AbsoluteFill } from "remotion";
import { z } from "zod";
+import { TimelineSchema } from "./types";
-export const aiVideoSchema = z.object({});
+export const aiVideoSchema = z.object({
+ timeline: TimelineSchema,
+ hasWatermark: z.boolean().optional(),
+});
-export const AIVideo: React.FC<z.infer<typeof aiVideoSchema>> = () => {
+export const AIVideo: React.FC<z.infer<typeof aiVideoSchema>> = ({
+ timeline,
+ hasWatermark,
+}) => {
return <AbsoluteFill style={{ backgroundColor: "white" }}></AbsoluteFill>;
};
diff --git a/packages/template-ai-video/src/Root.tsx b/packages/template-ai-video/src/Root.tsx
index 81dc6044f1..ac96987e5b 100644
--- a/packages/template-ai-video/src/Root.tsx
+++ b/packages/template-ai-video/src/Root.tsx
@@ -1,18 +1,67 @@
-import { Composition } from "remotion";
+import {
+ Composition,
+ continueRender,
+ delayRender,
+ getInputProps,
+ staticFile,
+} from "remotion";
import { AIVideo, aiVideoSchema } from "./AIVideo";
+import { useCallback, useEffect, useState } from "react";
+import { Timeline } from "./types";
+
+interface InputProps extends Record<string, unknown> {
+ timelineFileName?: string;
+ hasWatermark?: boolean;
+}
+
+const TargetFps = 60;
export const RemotionRoot: React.FC = () => {
+ const inputProps = getInputProps<InputProps>();
+ const [frameLength, setFrameLength] = useState(1);
+ const [timeline, setTimeline] = useState<Timeline | null>(null);
+ const [handle] = useState(() => delayRender());
+
+ const fetchConfig = useCallback(
+ async (props: InputProps) => {
+ const timelineFile =
+ props.timelineFileName ?? "content/demo/timeline.json";
+ const res = await fetch(staticFile(timelineFile));
+ const json = await res.json();
+
+ const newTimeline = json as Timeline;
+ newTimeline.elements.sort((a, b) => a.startMs - b.startMs);
+
+ const lengthMs =
+ newTimeline.elements[newTimeline.elements.length - 1].endMs / 1000;
+ const legthFrames = Math.floor(lengthMs * TargetFps);
+
+ setFrameLength(legthFrames);
+ setTimeline(newTimeline);
+
+ continueRender(handle);
+ },
+ [handle],
+ );
+
+ useEffect(() => {
+ fetchConfig(inputProps);
+ }, [inputProps, fetchConfig]);
+
return (
<>
<Composition
id="AIVideo"
component={AIVideo}
- durationInFrames={150}
- fps={30}
+ durationInFrames={frameLength}
+ fps={TargetFps}
width={1080}
height={1920}
schema={aiVideoSchema}
- defaultProps={{}}
+ defaultProps={{
+ timeline: timeline!,
+ hasWatermark: inputProps.hasWatermark,
+ }}
/>
</>
);