Skip to content

🎥 Make videos programmatically with React

Refactor

Vlad M26961bedc016
diff --git a/packages/template-ai-video/src/components/AIVideo.tsx b/packages/template-ai-video/src/components/AIVideo.tsx
index 330b7fa4a6..45b49cb83f 100644
--- a/packages/template-ai-video/src/components/AIVideo.tsx
+++ b/packages/template-ai-video/src/components/AIVideo.tsx
@@ -7,12 +7,13 @@ import {
} from "remotion";
import { z } from "zod";
import { Timeline } from "../lib/types";
-import { IntoFrameDuration, FPS } from "../lib/constants";
+import { IntoFrameDuration } from "../lib/constants";
import { loadFont } from "@remotion/google-fonts/BreeSerif";
import { Background } from "./Background";
import Subtitle from "./Subtitle";
import { useEffect, useState } from "react";
import {
+ calculateFrameTiming,
getAudioPath,
getTimelinePath,
loadTimelineFromFile,
@@ -23,21 +24,6 @@ export const aiVideoSchema = z.object({
hasWatermark: z.boolean(),
});
-const calculateFrameTiming = (
- startMs: number,
- endMs: number,
- options: { includeIntro?: boolean; addIntroOffset?: boolean } = {},
-) => {
- const { includeIntro = false, addIntroOffset = false } = options;
-
- const startFrame =
- (startMs * FPS) / 1000 + (addIntroOffset ? IntoFrameDuration : 0);
- const duration =
- ((endMs - startMs) * FPS) / 1000 + (includeIntro ? IntoFrameDuration : 0);
-
- return { startFrame, duration };
-};
-
export const AIVideo: React.FC<z.infer<typeof aiVideoSchema>> = ({
projectName,
}) => {
diff --git a/packages/template-ai-video/src/components/Background.tsx b/packages/template-ai-video/src/components/Background.tsx
index 54f4a24fd9..4c18879575 100644
--- a/packages/template-ai-video/src/components/Background.tsx
+++ b/packages/template-ai-video/src/components/Background.tsx
@@ -7,37 +7,10 @@ import {
WindowWidth,
} from "../lib/constants";
import { BackgroundElement } from "../lib/types";
-import { getImagePath } from "../lib/utils";
+import { calculateBlur, getImagePath } from "../lib/utils";
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;
project: string;
@@ -71,7 +44,7 @@ export const Background: React.FC<{
const top = -(imgHeight * imgScale - viewSize.height) / 2;
const left = -(imgWidth * imgScale - viewSize.width) / 2;
- const blur = calcBlur({ item, localMs });
+ const blur = calculateBlur({ item, localMs });
const maxBlur = 25;
const currentBlur = maxBlur * blur;
diff --git a/packages/template-ai-video/src/lib/utils.ts b/packages/template-ai-video/src/lib/utils.ts
index d56876c197..bd1897580c 100644
--- a/packages/template-ai-video/src/lib/utils.ts
+++ b/packages/template-ai-video/src/lib/utils.ts
@@ -1,6 +1,6 @@
import { staticFile } from "remotion";
-import { Timeline } from "./types";
-import { FPS } from "./constants";
+import { BackgroundElement, Timeline } from "./types";
+import { FPS, IntoFrameDuration } from "./constants";
export const loadTimelineFromFile = async (filename: string) => {
const res = await fetch(staticFile(filename));
@@ -14,6 +14,48 @@ export const loadTimelineFromFile = async (filename: string) => {
return { timeline, lengthFrames };
};
+export const calculateFrameTiming = (
+ startMs: number,
+ endMs: number,
+ options: { includeIntro?: boolean; addIntroOffset?: boolean } = {},
+) => {
+ const { includeIntro = false, addIntroOffset = false } = options;
+
+ const startFrame =
+ (startMs * FPS) / 1000 + (addIntroOffset ? IntoFrameDuration : 0);
+ const duration =
+ ((endMs - startMs) * FPS) / 1000 + (includeIntro ? IntoFrameDuration : 0);
+
+ return { startFrame, duration };
+};
+
+export const calculateBlur = ({
+ 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 getTimelinePath = (proj: string) =>
`content/${proj}/timeline.json`;