🎥 Make videos programmatically with React
Move timeline from props to filename to avoid rendering issues
Vlad Mbe9ea9a09f17
diff --git a/packages/template-ai-video/src/AIVideo.tsx b/packages/template-ai-video/src/AIVideo.tsxindex 53cb4776f7..08165fce95 100644--- a/packages/template-ai-video/src/AIVideo.tsx+++ b/packages/template-ai-video/src/AIVideo.tsx@@ -1,27 +1,48 @@-import { AbsoluteFill, Html5Audio, Sequence } from "remotion";+import { AbsoluteFill, Html5Audio, Sequence, useDelayRender } from "remotion";import { z } from "zod";-import { TimelineSchema } from "./types";-import { FirstFrameOffset, FPS } from "./constants";+import { Timeline } from "./types";+import { IntoFrameDuration, FPS } from "./constants";import { loadFont } from "@remotion/google-fonts/Signika";import { Background } from "./components/Background";import Subtitle from "./components/Subtitle";+import { useEffect, useState } from "react";+import { loadTimelineFromFile } from "./utils";export const aiVideoSchema = z.object({- timeline: TimelineSchema,- hasWatermark: z.boolean().optional(),+ timelineFile: z.string().min(1),+ hasWatermark: z.boolean(),});export const AIVideo: React.FC<z.infer<typeof aiVideoSchema>> = ({- timeline,+ timelineFile,}) => {const { fontFamily } = loadFont();+ const [timeline, setTimeline] = useState<Timeline | null>();+ const { delayRender, continueRender } = useDelayRender();++ useEffect(() => {+ const handle = delayRender("Loading timeline...");++ const fetchConfig = async () => {+ const { timeline } = await loadTimelineFromFile(timelineFile);+ setTimeline(timeline);+ continueRender(handle);+ };++ fetchConfig();++ return () => {+ continueRender(handle);+ };+ }, [timelineFile]);+const ready = true;return (<AbsoluteFill style={{ backgroundColor: "white" }}>{ready && timeline && (<>- <Sequence durationInFrames={FirstFrameOffset}>+ <Sequence durationInFrames={IntoFrameDuration}><AbsoluteFillstyle={{justifyContent: "center",@@ -52,7 +73,7 @@ export const AIVideo: React.FC<z.infer<typeof aiVideoSchema>> = ({const elementStartFrame = (element.startMs * FPS) / 1000;const durationInFrames =((element.endMs - element.startMs) * FPS) / 1000 +- (index === 0 ? FirstFrameOffset : 0);+ (index === 0 ? IntoFrameDuration : 0);return (<Sequence@@ -67,7 +88,7 @@ export const AIVideo: React.FC<z.infer<typeof aiVideoSchema>> = ({{timeline.text.map((element, index) => {const elementStartFrame =- (element.startMs * FPS) / 1000 + FirstFrameOffset;+ (element.startMs * FPS) / 1000 + IntoFrameDuration;const durationInFrames =((element.endMs - element.startMs) * FPS) / 1000;@@ -84,7 +105,7 @@ export const AIVideo: React.FC<z.infer<typeof aiVideoSchema>> = ({{timeline.audio.map((element, index) => {const elementStartFrame =- (element.startMs * FPS) / 1000 + FirstFrameOffset;+ (element.startMs * FPS) / 1000 + IntoFrameDuration;const durationInFrames =((element.endMs - element.startMs) * FPS) / 1000;diff --git a/packages/template-ai-video/src/Root.tsx b/packages/template-ai-video/src/Root.tsxindex 1d902b2992..976d9718cc 100644--- a/packages/template-ai-video/src/Root.tsx+++ b/packages/template-ai-video/src/Root.tsx@@ -3,12 +3,11 @@ import {continueRender,delayRender,getInputProps,- staticFile,} from "remotion";import { AIVideo, aiVideoSchema } from "./AIVideo";-import { useCallback, useEffect, useState } from "react";-import { Timeline } from "./types";-import { FPS } from "./constants";+import { useEffect, useState } from "react";+import { FPS, WindowsHeight, WindowsWidth } from "./constants";+import { loadTimelineFromFile } from "./utils";interface InputProps extends Record<string, unknown> {timelineFileName?: string;@@ -18,34 +17,25 @@ interface InputProps extends Record<string, unknown> {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 timelineFile =+ inputProps.timelineFileName ?? "content/demo/timeline.json";- const newTimeline = json as Timeline;- newTimeline.elements.sort((a, b) => a.startMs - b.startMs);+ useEffect(() => {+ const handle = delayRender("Calculating FPS duration...");- const lengthMs =- newTimeline.elements[newTimeline.elements.length - 1].endMs / 1000;- const legthFrames = Math.floor(lengthMs * FPS);+ const fetchConfig = async () => {+ const { lengthFrames } = await loadTimelineFromFile(timelineFile);+ setFrameLength(lengthFrames);+ continueRender(handle);+ };- setFrameLength(legthFrames);- setTimeline(newTimeline);+ fetchConfig();+ return () => {continueRender(handle);- },- [handle],- );-- useEffect(() => {- fetchConfig(inputProps);- }, [inputProps, fetchConfig]);+ };+ }, [timelineFile]);return (<>@@ -54,12 +44,12 @@ export const RemotionRoot: React.FC = () => {component={AIVideo}durationInFrames={frameLength}fps={FPS}- width={1080}- height={1920}+ width={WindowsWidth}+ height={WindowsHeight}schema={aiVideoSchema}defaultProps={{- timeline: timeline!,- hasWatermark: inputProps.hasWatermark,+ timelineFile,+ hasWatermark: !!inputProps.hasWatermark,}}/></>diff --git a/packages/template-ai-video/src/constants.ts b/packages/template-ai-video/src/constants.tsindex 3b3185a13e..53727e0f2e 100644--- a/packages/template-ai-video/src/constants.ts+++ b/packages/template-ai-video/src/constants.ts@@ -1,4 +1,4 @@export const FPS = 30;export const WindowsWidth = 1080;export const WindowsHeight = 1920;-export const FirstFrameOffset = 1;+export const IntoFrameDuration = 1;diff --git a/packages/template-ai-video/src/utils.ts b/packages/template-ai-video/src/utils.tsnew file mode 100644index 0000000000..f328632223--- /dev/null+++ b/packages/template-ai-video/src/utils.ts@@ -0,0 +1,15 @@+import { staticFile } from "remotion";+import { Timeline } from "./types";+import { FPS } from "./constants";++export const loadTimelineFromFile = async (filename: string) => {+ const res = await fetch(staticFile(filename));+ const json = await res.json();+ const timeline = json as Timeline;+ timeline.elements.sort((a, b) => a.startMs - b.startMs);++ const lengthMs = timeline.elements[timeline.elements.length - 1].endMs / 1000;+ const lengthFrames = Math.floor(lengthMs * FPS);++ return { timeline, lengthFrames };+};