Skip to content

🎥 Make videos programmatically with React

Add Frames and FPS to RenderControls and reflect them in Rendering Process

ASchwad33959ef39b10
diff --git a/packages/template-prompt-to-motion-graphics/src/app/code-examples/page.tsx b/packages/template-prompt-to-motion-graphics/src/app/code-examples/page.tsx
index bdbbe93423..9f464427be 100644
--- a/packages/template-prompt-to-motion-graphics/src/app/code-examples/page.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/app/code-examples/page.tsx
@@ -31,6 +31,8 @@ function DemoPageContent() {
}, [searchParams]);
const [sidebarOpen, setSidebarOpen] = useState(true);
+ const [durationInFrames, setDurationInFrames] = useState(selectedExample.durationInFrames);
+ const [fps, setFps] = useState(selectedExample.fps);
const selectedButtonRef = useRef<HTMLButtonElement>(null);
const { code, Component, error, isCompiling, setCode, compileCode } =
@@ -52,6 +54,8 @@ function DemoPageContent() {
if (example) {
setCode(example.code);
compileCode(example.code);
+ setDurationInFrames(example.durationInFrames);
+ setFps(example.fps);
router.replace(`/code-examples?example=${exampleId}`, {
scroll: false,
});
@@ -169,8 +173,10 @@ function DemoPageContent() {
/>
<AnimationPlayer
Component={Component}
- durationInFrames={selectedExample.durationInFrames}
- fps={selectedExample.fps}
+ durationInFrames={durationInFrames}
+ fps={fps}
+ onDurationChange={setDurationInFrames}
+ onFpsChange={setFps}
isCompiling={isCompiling}
isStreaming={false}
error={error}
diff --git a/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsx b/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsx
index 2b878887ea..96b0405af8 100644
--- a/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/app/generate/page.tsx
@@ -12,7 +12,6 @@ import {
type StreamPhase,
type PromptInputRef,
} from "../../components/PromptInput";
-import { SettingsModal } from "../../components/SettingsModal";
import { examples } from "../../templates";
import { useAnimationState } from "../../hooks/useAnimationState";
@@ -118,17 +117,7 @@ function GeneratePageContent() {
}, [initialPrompt, hasAutoStarted]);
return (
- <PageLayout
- showLogoAsLink
- rightContent={
- <SettingsModal
- durationInFrames={durationInFrames}
- onDurationChange={setDurationInFrames}
- fps={fps}
- onFpsChange={setFps}
- />
- }
- >
+ <PageLayout showLogoAsLink>
<div className="flex-1 flex flex-col min-w-0 px-12 pb-8 gap-8 overflow-hidden">
<div className="flex-1 flex overflow-hidden gap-8">
<CodeEditor
@@ -141,6 +130,8 @@ function GeneratePageContent() {
Component={Component}
durationInFrames={durationInFrames}
fps={fps}
+ onDurationChange={setDurationInFrames}
+ onFpsChange={setFps}
isCompiling={isCompiling}
isStreaming={isStreaming}
error={apiError || error}
diff --git a/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer.tsx b/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer.tsx
index 68f83a5ed2..8e8b5033a9 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer.tsx
@@ -3,6 +3,7 @@
import React from "react";
import { Player, type ErrorFallback } from "@remotion/player";
import { RenderControls } from "./RenderControls";
+import { SettingsModal } from "./SettingsModal";
const renderErrorFallback: ErrorFallback = ({ error }) => {
return (
@@ -23,17 +24,21 @@ interface AnimationPlayerProps {
Component: React.ComponentType | null;
durationInFrames: number;
fps: number;
+ onDurationChange: (duration: number) => void;
+ onFpsChange: (fps: number) => void;
isCompiling: boolean;
isStreaming: boolean;
error: string | null;
errorType?: "compilation" | "api";
- code?: string;
+ code: string;
}
export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({
Component,
durationInFrames,
fps,
+ onDurationChange,
+ onFpsChange,
isCompiling,
isStreaming,
error,
@@ -138,7 +143,15 @@ export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({
clickToPlay={false}
/>
</div>
- <RenderControls code={code} />
+ <div className="flex items-center justify-between gap-6">
+ <RenderControls code={code} durationInFrames={durationInFrames} fps={fps} />
+ <SettingsModal
+ durationInFrames={durationInFrames}
+ onDurationChange={onDurationChange}
+ fps={fps}
+ onFpsChange={onFpsChange}
+ />
+ </div>
</div>
</div>
);
diff --git a/packages/template-prompt-to-motion-graphics/src/components/RenderControls.tsx b/packages/template-prompt-to-motion-graphics/src/components/RenderControls.tsx
index de2f67c7ef..1453708826 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/RenderControls.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/RenderControls.tsx
@@ -4,22 +4,29 @@ import { useEffect, useRef } from "react";
import { Download } from "lucide-react";
import { Button } from "@/components/ui/button";
import { ErrorComp } from "./Error";
-import { ProgressBar } from "./ProgressBar";
import { useRendering } from "../helpers/use-rendering";
export const RenderControls: React.FC<{
code: string;
-}> = ({ code }) => {
- const { renderMedia, state, undo } = useRendering({ code });
- const previousCodeRef = useRef(code);
+ durationInFrames: number;
+ fps: number;
+}> = ({ code, durationInFrames, fps }) => {
+ const { renderMedia, state, undo } = useRendering({ code, durationInFrames, fps });
+ const previousPropsRef = useRef({ code, durationInFrames, fps });
- // Reset rendering state when code changes
+ // Reset rendering state when code, duration, or fps changes
useEffect(() => {
- if (previousCodeRef.current !== code && state.status !== "init") {
+ const prev = previousPropsRef.current;
+ const hasChanged =
+ prev.code !== code ||
+ prev.durationInFrames !== durationInFrames ||
+ prev.fps !== fps;
+
+ if (hasChanged && state.status !== "init") {
undo();
}
- previousCodeRef.current = code;
- }, [code, state.status, undo]);
+ previousPropsRef.current = { code, durationInFrames, fps };
+ }, [code, durationInFrames, fps, state.status, undo]);
if (
state.status === "init" ||
@@ -47,12 +54,20 @@ export const RenderControls: React.FC<{
}
if (state.status === "rendering") {
+ const progress = Math.round(state.progress * 100);
+
return (
- <div className="flex flex-col gap-2">
- <ProgressBar progress={state.progress} />
- <p className="text-sm text-muted-foreground text-center">
- Rendering... {Math.round(state.progress * 100)}%
- </p>
+ <div className="flex flex-col gap-2 w-full">
+ <div className="flex items-center justify-between text-sm">
+ <span className="text-muted-foreground">Rendering...</span>
+ <span className="font-medium">{progress}%</span>
+ </div>
+ <div className="w-full h-2 bg-muted rounded-full overflow-hidden">
+ <div
+ className="h-full bg-primary transition-all duration-300 rounded-full"
+ style={{ width: `${progress}%` }}
+ />
+ </div>
</div>
);
}
diff --git a/packages/template-prompt-to-motion-graphics/src/components/SettingsModal.tsx b/packages/template-prompt-to-motion-graphics/src/components/SettingsModal.tsx
index dc0985889f..9e04ca8744 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/SettingsModal.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/SettingsModal.tsx
@@ -1,6 +1,6 @@
"use client";
-import { useState } from "react";
+import { useState, useEffect } from "react";
import {
Dialog,
DialogContent,
@@ -27,6 +27,36 @@ export function SettingsModal({
onFpsChange,
}: SettingsModalProps) {
const [open, setOpen] = useState(false);
+ const [localDuration, setLocalDuration] = useState(String(durationInFrames));
+ const [localFps, setLocalFps] = useState(String(fps));
+
+ // Sync local state when props change
+ useEffect(() => {
+ setLocalDuration(String(durationInFrames));
+ setLocalFps(String(fps));
+ }, [durationInFrames, fps]);
+
+ const handleDurationBlur = () => {
+ const parsed = parseInt(localDuration);
+ if (isNaN(parsed) || parsed < 1) {
+ setLocalDuration(String(durationInFrames));
+ } else {
+ const clamped = Math.min(1000, Math.max(1, parsed));
+ setLocalDuration(String(clamped));
+ onDurationChange(clamped);
+ }
+ };
+
+ const handleFpsBlur = () => {
+ const parsed = parseInt(localFps);
+ if (isNaN(parsed) || parsed < 1) {
+ setLocalFps(String(fps));
+ } else {
+ const clamped = Math.min(60, Math.max(1, parsed));
+ setLocalFps(String(clamped));
+ onFpsChange(clamped);
+ }
+ };
return (
<Dialog open={open} onOpenChange={setOpen}>
@@ -56,10 +86,9 @@ export function SettingsModal({
type="number"
min={1}
max={1000}
- value={durationInFrames}
- onChange={(e) =>
- onDurationChange(Math.max(1, parseInt(e.target.value) || 1))
- }
+ value={localDuration}
+ onChange={(e) => setLocalDuration(e.target.value)}
+ onBlur={handleDurationBlur}
className="w-full px-3 py-2 rounded border border-border bg-input text-foreground text-sm font-sans focus:outline-none focus:border-primary"
/>
</div>
@@ -72,12 +101,9 @@ export function SettingsModal({
type="number"
min={1}
max={60}
- value={fps}
- onChange={(e) =>
- onFpsChange(
- Math.max(1, Math.min(60, parseInt(e.target.value) || 30)),
- )
- }
+ value={localFps}
+ onChange={(e) => setLocalFps(e.target.value)}
+ onBlur={handleFpsBlur}
className="w-full px-3 py-2 rounded border border-border bg-input text-foreground text-sm font-sans focus:outline-none focus:border-primary"
/>
</div>
diff --git a/packages/template-prompt-to-motion-graphics/src/remotion/index.tsx b/packages/template-prompt-to-motion-graphics/src/remotion/index.tsx
index e960522298..97f18ff28e 100644
--- a/packages/template-prompt-to-motion-graphics/src/remotion/index.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/remotion/index.tsx
@@ -15,9 +15,11 @@ const RemotionRoot: React.FC = () => {
fps={30}
width={1920}
height={1080}
- defaultProps={{
- code: defaultCode,
- }}
+ defaultProps={{ code: defaultCode }}
+ calculateMetadata={({ props }) => ({
+ durationInFrames: props.durationInFrames as number,
+ fps: props.fps as number,
+ })}
/>
</>
);
diff --git a/packages/template-prompt-to-motion-graphics/types/constants.ts b/packages/template-prompt-to-motion-graphics/types/constants.ts
index ec50a39996..d7ec0d090a 100644
--- a/packages/template-prompt-to-motion-graphics/types/constants.ts
+++ b/packages/template-prompt-to-motion-graphics/types/constants.ts
@@ -4,4 +4,6 @@ export const COMP_NAME = "DynamicComp";
export const CompositionProps = z.object({
code: z.string(),
+ durationInFrames: z.number(),
+ fps: z.number(),
});