Spaces:
Sleeping
Sleeping
File size: 5,541 Bytes
cfe45d5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import { useEffect, useState } from "react";
import {
AbsoluteFill,
Audio,
Sequence,
staticFile,
CalculateMetadataFunction,
} from "remotion";
import { LAYOUT_REGISTRY, LayoutType, SceneLayoutProps } from "./components/layouts";
import { TransitionWipe } from "./components/Transitions";
import { LogoOverlay } from "./components/LogoOverlay";
// βββ Types βββββββββββββββββββββββββββββββββββββββββββββββββββ
interface SceneData {
id: number;
order: number;
title: string;
narration: string;
layout: LayoutType;
layoutProps: Record<string, any>;
durationSeconds: number;
voiceoverFile: string | null;
images: string[];
}
interface VideoData {
projectName: string;
heroImage?: string | null;
accentColor: string;
bgColor: string;
textColor: string;
logo?: string | null;
logoPosition?: string;
logoOpacity?: number;
aspectRatio?: string;
scenes: SceneData[];
}
interface VideoProps {
dataUrl: string;
}
// βββ Calculate actual duration from data.json ββββββββββββββββ
export const calculateVideoMetadata: CalculateMetadataFunction<VideoProps> =
async ({ props }) => {
const FPS = 30;
try {
const url = staticFile(props.dataUrl.replace(/^\//, ""));
const res = await fetch(url);
if (!res.ok) throw new Error(`Failed to fetch ${url}`);
const data: VideoData = await res.json();
const totalSeconds = data.scenes.reduce(
(sum, s) => sum + (s.durationSeconds || 5),
0
);
const totalFrames = Math.ceil((totalSeconds + 2) * FPS);
const isPortrait = data.aspectRatio === "portrait";
return {
durationInFrames: Math.max(totalFrames, FPS * 5),
fps: FPS,
width: isPortrait ? 1080 : 1920,
height: isPortrait ? 1920 : 1080,
};
} catch (e) {
console.warn("calculateVideoMetadata fallback:", e);
return {
durationInFrames: FPS * 300,
fps: FPS,
width: 1920,
height: 1080,
};
}
};
// βββ Main composition ββββββββββββββββββββββββββββββββββββββββ
export const ExplainerVideo: React.FC<VideoProps> = ({ dataUrl }) => {
const [data, setData] = useState<VideoData | null>(null);
useEffect(() => {
fetch(staticFile(dataUrl.replace(/^\//, "")))
.then((res) => res.json())
.then(setData)
.catch(() => {
setData({
projectName: "Blog2Video Preview",
accentColor: "#7C3AED",
bgColor: "#FFFFFF",
textColor: "#000000",
scenes: [
{
id: 1,
order: 1,
title: "Welcome",
narration: "This is a preview of your Blog2Video project.",
layout: "text_narration",
layoutProps: {},
durationSeconds: 5,
voiceoverFile: null,
images: [],
},
],
});
});
}, [dataUrl]);
if (!data) {
return (
<AbsoluteFill
style={{
backgroundColor: "#FFFFFF",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<p style={{ color: "#000000", fontSize: 36 }}>Loading...</p>
</AbsoluteFill>
);
}
const FPS = 30;
let currentFrame = 0;
return (
<AbsoluteFill style={{ backgroundColor: data.bgColor || "#FFFFFF" }}>
{data.scenes.map((scene, index) => {
const durationFrames = Math.round(scene.durationSeconds * FPS);
const startFrame = currentFrame;
currentFrame += durationFrames;
// Pick layout component from registry
const LayoutComponent =
LAYOUT_REGISTRY[scene.layout] || LAYOUT_REGISTRY.text_narration;
// Resolve image URL via staticFile
const imageUrl =
scene.images.length > 0 ? staticFile(scene.images[0]) : undefined;
// Build props for the layout component
const layoutProps: SceneLayoutProps = {
title: scene.title,
narration: scene.narration,
imageUrl,
accentColor: data.accentColor || "#7C3AED",
bgColor: data.bgColor || "#FFFFFF",
textColor: data.textColor || "#000000",
aspectRatio: data.aspectRatio || "landscape",
...scene.layoutProps,
};
return (
<Sequence
key={scene.id}
from={startFrame}
durationInFrames={durationFrames}
name={scene.title}
>
<LayoutComponent {...layoutProps} />
{/* Voiceover audio */}
{scene.voiceoverFile && (
<Audio src={staticFile(scene.voiceoverFile)} />
)}
{/* Transition overlay */}
{index < data.scenes.length - 1 && (
<Sequence from={durationFrames - 15} durationInFrames={15}>
<TransitionWipe />
</Sequence>
)}
</Sequence>
);
})}
{/* Logo overlay β spans entire video */}
{data.logo && (
<LogoOverlay
src={staticFile(data.logo)}
position={data.logoPosition || "bottom_right"}
maxOpacity={data.logoOpacity ?? 0.9}
aspectRatio={data.aspectRatio || "landscape"}
/>
)}
</AbsoluteFill>
);
};
|