Skip to content

🎥 Make videos programmatically with React

Add constants and background

Vlad M26e7a1c47f9f
diff --git a/packages/template-ai-video/src/Root.tsx b/packages/template-ai-video/src/Root.tsx
index ac96987e5b..1d902b2992 100644
--- a/packages/template-ai-video/src/Root.tsx
+++ b/packages/template-ai-video/src/Root.tsx
@@ -8,14 +8,13 @@ import {
import { AIVideo, aiVideoSchema } from "./AIVideo";
import { useCallback, useEffect, useState } from "react";
import { Timeline } from "./types";
+import { FPS } from "./constants";
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);
@@ -34,7 +33,7 @@ export const RemotionRoot: React.FC = () => {
const lengthMs =
newTimeline.elements[newTimeline.elements.length - 1].endMs / 1000;
- const legthFrames = Math.floor(lengthMs * TargetFps);
+ const legthFrames = Math.floor(lengthMs * FPS);
setFrameLength(legthFrames);
setTimeline(newTimeline);
@@ -54,7 +53,7 @@ export const RemotionRoot: React.FC = () => {
id="AIVideo"
component={AIVideo}
durationInFrames={frameLength}
- fps={TargetFps}
+ fps={FPS}
width={1080}
height={1920}
schema={aiVideoSchema}
diff --git a/packages/template-ai-video/src/components/Background.tsx b/packages/template-ai-video/src/components/Background.tsx
new file mode 100644
index 0000000000..7c91cc2875
--- /dev/null
+++ b/packages/template-ai-video/src/components/Background.tsx
@@ -0,0 +1,88 @@
+import { AbsoluteFill, Img, useCurrentFrame } from "remotion";
+import { FPS, WindowsHeight, WindowsWidth } from "../constants";
+import { BackgroundElement } from "../types";
+
+const EXTRA_SCALE = 0.2;
+
+const calcBlur = ({
+ item,
+ localMs,
+}: {
+ item: BackgroundElement;
+ localMs: number;
+}) => {
+ const maxBlur = 1;
+ const fadeMs = 1000;
+
+ const startMs = item.startMs;
+ const endMs = item.endMs;
+
+ const { enterTransition } = item;
+ const { exitTransition } = item;
+
+ if (enterTransition === "blur" && localMs < fadeMs) {
+ return (1 - localMs / fadeMs) * maxBlur;
+ }
+
+ if (exitTransition === "blur" && localMs > endMs - startMs - fadeMs) {
+ return (1 - (endMs - startMs - localMs) / fadeMs) * maxBlur;
+ }
+
+ return 0;
+};
+
+export const Background: React.FC<{
+ item: BackgroundElement;
+}> = ({ item }) => {
+ const frame = useCurrentFrame();
+ // const durationFrames = ((item.endMs - item.startMs) * FPS)/1000;
+ const localMs = (frame / FPS) * 1000;
+
+ const viewSize = { width: WindowsWidth, height: WindowsHeight };
+ const imageRatio = 1.0;
+
+ const imgWidth = viewSize.height;
+ const imgHeight = imgWidth * imageRatio;
+ let animScale = 1 + EXTRA_SCALE;
+
+ const currentScaleAnim = item.animations?.find(
+ (anim) =>
+ anim.type === "scale" && anim.startMs <= localMs && anim.endMs >= localMs,
+ );
+
+ if (currentScaleAnim) {
+ const progress =
+ (localMs - currentScaleAnim.startMs) /
+ (currentScaleAnim.endMs - currentScaleAnim.startMs);
+ animScale =
+ EXTRA_SCALE +
+ progress * (currentScaleAnim.to - currentScaleAnim.from) +
+ currentScaleAnim.from;
+ }
+
+ const imgScale = animScale;
+ const top = -(imgHeight * imgScale - viewSize.height) / 2;
+ const left = -(imgWidth * imgScale - viewSize.width) / 2;
+
+ const blur = calcBlur({ item, localMs });
+ const maxBlur = 25;
+
+ const currentBlur = maxBlur * blur;
+
+ return (
+ <AbsoluteFill style={{ backgroundColor: "blue" }}>
+ <Img
+ src={item.imageUrl}
+ style={{
+ width: imgWidth * imgScale,
+ height: imgHeight * imgScale,
+ position: "absolute",
+ top,
+ left,
+ filter: `blur(${currentBlur}px)`,
+ WebkitFilter: `blur(${currentBlur}px)`,
+ }}
+ />
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/template-ai-video/src/constants.ts b/packages/template-ai-video/src/constants.ts
new file mode 100644
index 0000000000..0c953139f2
--- /dev/null
+++ b/packages/template-ai-video/src/constants.ts
@@ -0,0 +1,3 @@
+export const FPS = 30;
+export const WindowsWidth = 1080;
+export const WindowsHeight = 1920;