Skip to content

🎥 Make videos programmatically with React

Add RenderControls to Player

ASchwad5a9c7b85f8c4
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 8fcb2c49a6..bdbbe93423 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
@@ -174,6 +174,7 @@ function DemoPageContent() {
isCompiling={isCompiling}
isStreaming={false}
error={error}
+ code={code}
/>
</div>
</div>
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 70a4a4497d..2b878887ea 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
@@ -6,7 +6,6 @@ import { useSearchParams } from "next/navigation";
import { Loader2 } from "lucide-react";
import { CodeEditor } from "../../components/CodeEditor";
import { AnimationPlayer } from "../../components/AnimationPlayer";
-import { RenderControls } from "../../components/RenderControls";
import { PageLayout } from "../../components/PageLayout";
import {
PromptInput,
@@ -138,18 +137,16 @@ function GeneratePageContent() {
isStreaming={isStreaming}
streamPhase={streamPhase}
/>
- <div className="flex flex-3 flex-col min-w-0 gap-4">
- <AnimationPlayer
- Component={Component}
- durationInFrames={durationInFrames}
- fps={fps}
- isCompiling={isCompiling}
- isStreaming={isStreaming}
- error={apiError || error}
- errorType={apiError ? "api" : "compilation"}
- />
- <RenderControls code={code} />
- </div>
+ <AnimationPlayer
+ Component={Component}
+ durationInFrames={durationInFrames}
+ fps={fps}
+ isCompiling={isCompiling}
+ isStreaming={isStreaming}
+ error={apiError || error}
+ errorType={apiError ? "api" : "compilation"}
+ code={code}
+ />
</div>
<PromptInput
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 bb01d99212..68f83a5ed2 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/AnimationPlayer.tsx
@@ -2,6 +2,7 @@
import React from "react";
import { Player, type ErrorFallback } from "@remotion/player";
+import { RenderControls } from "./RenderControls";
const renderErrorFallback: ErrorFallback = ({ error }) => {
return (
@@ -26,6 +27,7 @@ interface AnimationPlayerProps {
isStreaming: boolean;
error: string | null;
errorType?: "compilation" | "api";
+ code?: string;
}
export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({
@@ -36,6 +38,7 @@ export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({
isStreaming,
error,
errorType = "compilation",
+ code,
}) => {
if (isStreaming) {
return (
@@ -110,8 +113,10 @@ export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({
return (
<div className="flex flex-3 flex-col items-center bg-background min-w-0">
- <div className="w-full max-w-[1200px]">
- <h2 className="text-sm font-medium text-muted-foreground mb-3">Video Preview</h2>
+ <div className="w-full max-w-[1200px] flex flex-col gap-3">
+ <h2 className="text-sm font-medium text-muted-foreground">
+ Video Preview
+ </h2>
<div className="w-full aspect-video rounded-lg overflow-hidden shadow-[0_0_60px_rgba(0,0,0,0.5)]">
<Player
key={Component.toString()}
@@ -133,6 +138,7 @@ export const AnimationPlayer: React.FC<AnimationPlayerProps> = ({
clickToPlay={false}
/>
</div>
+ <RenderControls code={code} />
</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 d18c88de10..de2f67c7ef 100644
--- a/packages/template-prompt-to-motion-graphics/src/components/RenderControls.tsx
+++ b/packages/template-prompt-to-motion-graphics/src/components/RenderControls.tsx
@@ -1,7 +1,8 @@
"use client";
+import { useEffect, useRef } from "react";
+import { Download } from "lucide-react";
import { Button } from "@/components/ui/button";
-import { DownloadButton } from "./DownloadButton";
import { ErrorComp } from "./Error";
import { ProgressBar } from "./ProgressBar";
import { useRendering } from "../helpers/use-rendering";
@@ -10,33 +11,73 @@ export const RenderControls: React.FC<{
code: string;
}> = ({ code }) => {
const { renderMedia, state, undo } = useRendering({ code });
+ const previousCodeRef = useRef(code);
- return (
- <div className="flex flex-col gap-2">
- {state.status === "init" ||
- state.status === "invoking" ||
- state.status === "error" ? (
- <>
- <Button
- disabled={state.status === "invoking" || !code}
- loading={state.status === "invoking"}
- onClick={renderMedia}
- >
- Render video
+ // Reset rendering state when code changes
+ useEffect(() => {
+ if (previousCodeRef.current !== code && state.status !== "init") {
+ undo();
+ }
+ previousCodeRef.current = code;
+ }, [code, state.status, undo]);
+
+ if (
+ state.status === "init" ||
+ state.status === "invoking" ||
+ state.status === "error"
+ ) {
+ return (
+ <div className="flex flex-col gap-2">
+ <Button
+ disabled={state.status === "invoking" || !code}
+ loading={state.status === "invoking"}
+ onClick={renderMedia}
+ className="w-full"
+ >
+ <Download className="w-4 h-4 mr-2" />
+ {state.status === "invoking"
+ ? "Starting render..."
+ : "Render & Download"}
+ </Button>
+ {state.status === "error" && (
+ <ErrorComp message={state.error.message} />
+ )}
+ </div>
+ );
+ }
+
+ if (state.status === "rendering") {
+ 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>
+ );
+ }
+
+ if (state.status === "done") {
+ return (
+ <div className="flex gap-2">
+ <a href={state.url} className="flex-1">
+ <Button className="w-full">
+ <Download className="w-4 h-4 mr-2" />
+ Download ({formatBytes(state.size)})
</Button>
- {state.status === "error" ? (
- <ErrorComp message={state.error.message} />
- ) : null}
- </>
- ) : null}
- {state.status === "rendering" || state.status === "done" ? (
- <div className="flex flex-col gap-2">
- <ProgressBar
- progress={state.status === "rendering" ? state.progress : 1}
- />
- <DownloadButton undo={undo} state={state} />
- </div>
- ) : null}
- </div>
- );
+ </a>
+ <Button variant="outline" onClick={undo} className="flex-shrink-0">
+ Render again
+ </Button>
+ </div>
+ );
+ }
+
+ return null;
};
+
+function formatBytes(bytes: number): string {
+ if (bytes < 1024) return `${bytes} B`;
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
+}