Skip to content

🎥 Make videos programmatically with React

address the rest of the comments

Igor Samokhovets0402d13bfe52
diff --git a/packages/promo-pages/bundle.ts b/packages/promo-pages/bundle.ts
index 93e2bbb167..8509f6db99 100644
--- a/packages/promo-pages/bundle.ts
+++ b/packages/promo-pages/bundle.ts
@@ -66,6 +66,8 @@ if (!result.success) {
const outdir = path.resolve('dist');
for (const output of result.outputs) {
+ // On Windows, Bun may return absolute output paths here. Normalize them back
+ // into the local dist directory so we don't accidentally write invalid paths.
const relativeOutputPath = path.isAbsolute(output.path)
? path.relative(outdir, output.path)
: output.path;
diff --git a/packages/template-electron/remotion/HelloWorld.tsx b/packages/template-electron/remotion/HelloWorld.tsx
index 85ca7e718e..a4358fac64 100644
--- a/packages/template-electron/remotion/HelloWorld.tsx
+++ b/packages/template-electron/remotion/HelloWorld.tsx
@@ -5,10 +5,9 @@ import {
useCurrentFrame,
useVideoConfig,
} from "remotion";
+import type {HelloWorldProps} from "./types";
-export const HelloWorld: React.FC<{
- titleText: string;
-}> = ({titleText}) => {
+export const HelloWorld: React.FC<HelloWorldProps> = ({titleText}) => {
const frame = useCurrentFrame();
const {fps} = useVideoConfig();
const scale = spring({
diff --git a/packages/template-electron/remotion/Root.tsx b/packages/template-electron/remotion/Root.tsx
index 277119b2d2..8a66cd2cac 100644
--- a/packages/template-electron/remotion/Root.tsx
+++ b/packages/template-electron/remotion/Root.tsx
@@ -1,7 +1,14 @@
import {Composition} from "remotion";
import {HelloWorld} from "./HelloWorld";
+import type {HelloWorldProps} from "./types";
+
+const defaultProps: HelloWorldProps = {
+ titleText: "Hello from Electron",
+};
export const RemotionRoot: React.FC = () => {
+
+
return (
<Composition
id="HelloWorld"
@@ -10,9 +17,7 @@ export const RemotionRoot: React.FC = () => {
fps={30}
width={1280}
height={720}
- defaultProps={{
- titleText: "Hello from Electron",
- }}
+ defaultProps={defaultProps}
/>
);
};
diff --git a/packages/template-electron/remotion/types.ts b/packages/template-electron/remotion/types.ts
new file mode 100644
index 0000000000..b170d9d16a
--- /dev/null
+++ b/packages/template-electron/remotion/types.ts
@@ -0,0 +1,3 @@
+export type HelloWorldProps = {
+ titleText: string;
+};
diff --git a/packages/template-electron/src/electron-api.ts b/packages/template-electron/src/electron-api.ts
index c64fff747f..893178ca9f 100644
--- a/packages/template-electron/src/electron-api.ts
+++ b/packages/template-electron/src/electron-api.ts
@@ -1,11 +1,12 @@
+import type {HelloWorldProps} from "../remotion/types";
+
export const RENDER_VIDEO_CHANNEL = "remotion-electron:render-video";
export const RENDER_PROGRESS_CHANNEL = "remotion-electron:render-progress";
export const SELECT_RENDER_OUTPUT_CHANNEL = "remotion-electron:select-render-output";
export const CANCEL_RENDER_CHANNEL = "remotion-electron:cancel-render";
export const RENDER_CANCELLED_MESSAGE = "Render cancelled.";
-export type RenderRequest = {
- titleText: string;
+export type RenderRequest = HelloWorldProps & {
outputPath: string;
};
diff --git a/packages/template-electron/src/main.ts b/packages/template-electron/src/main.ts
index f68f8b3390..5d0cd2d5ee 100644
--- a/packages/template-electron/src/main.ts
+++ b/packages/template-electron/src/main.ts
@@ -78,7 +78,7 @@ function createWindow(): void {
}
}
-ipcMain.handle(SELECT_RENDER_OUTPUT_CHANNEL, async (event) => {
+ipcMain.handle(SELECT_RENDER_OUTPUT_CHANNEL, async (event: IpcMainInvokeEvent) => {
assertTrustedSender(event);
const defaultPath = path.join(
@@ -111,7 +111,9 @@ ipcMain.handle(SELECT_RENDER_OUTPUT_CHANNEL, async (event) => {
};
});
-ipcMain.handle(RENDER_VIDEO_CHANNEL, async (event, input: RenderRequest) => {
+ipcMain.handle(
+ RENDER_VIDEO_CHANNEL,
+ async (event: IpcMainInvokeEvent, input: RenderRequest) => {
assertTrustedSender(event);
if (activeRender) {
@@ -162,9 +164,10 @@ ipcMain.handle(RENDER_VIDEO_CHANNEL, async (event, input: RenderRequest) => {
cancelActiveRender = null;
activeRender = null;
}
-});
+ },
+);
-ipcMain.handle(CANCEL_RENDER_CHANNEL, async (event) => {
+ipcMain.handle(CANCEL_RENDER_CHANNEL, async (event: IpcMainInvokeEvent) => {
assertTrustedSender(event);
if (!activeRender || !cancelActiveRender) {
diff --git a/packages/template-electron/src/remotion-bundle.ts b/packages/template-electron/src/remotion-bundle.ts
index b4c367248c..b313bb7c23 100644
--- a/packages/template-electron/src/remotion-bundle.ts
+++ b/packages/template-electron/src/remotion-bundle.ts
@@ -1,9 +1,11 @@
import {existsSync} from "node:fs";
import {rm} from "node:fs/promises";
import path from "node:path";
+import type {BundleOptions} from "@remotion/bundler";
import type {RenderUpdate} from "./electron-api";
export const PREBUILT_REMOTION_BUNDLE_DIR = "remotion-bundle";
+type BundleProgress = Parameters<NonNullable<BundleOptions["onProgress"]>>[0];
export const getPrebuiltRemotionBundlePath = (projectRoot: string) => {
return path.join(projectRoot, PREBUILT_REMOTION_BUNDLE_DIR);
@@ -31,7 +33,7 @@ export const bundleRemotionProject = async ({
return bundle({
entryPoint: path.join(projectRoot, "remotion/index.ts"),
outDir,
- onProgress(progress) {
+ onProgress(progress: BundleProgress) {
onUpdate?.({
type: "progress",
stage: "bundling",
diff --git a/packages/template-electron/src/render-video.ts b/packages/template-electron/src/render-video.ts
index f47dbc39c9..5e4052da28 100644
--- a/packages/template-electron/src/render-video.ts
+++ b/packages/template-electron/src/render-video.ts
@@ -1,5 +1,7 @@
import {
type CancelSignal,
+ type DownloadBrowserProgressFn,
+ type RenderMediaProgress,
ensureBrowser,
renderMedia,
selectComposition,
@@ -18,6 +20,7 @@ import {
hasPrebuiltRemotionBundle,
} from "./remotion-bundle";
import {getPackagedBrowserPaths} from "./packaged-browser";
+import type {HelloWorldProps} from "../remotion/types";
const compositionId = "HelloWorld";
@@ -120,6 +123,10 @@ export const renderVideo = async ({
wasCancelled = true;
});
+ const inputProps: HelloWorldProps = {
+ titleText: titleText.trim() || "Hello from Electron",
+ };
+
try {
onUpdate?.({
type: "status",
@@ -131,6 +138,13 @@ export const renderVideo = async ({
onBrowserDownload: ({chromeMode}) => {
const browserName =
chromeMode === "chrome-for-testing" ? "Chrome" : "Chrome Headless Shell";
+ const onProgress: DownloadBrowserProgressFn = (progress) => {
+ onUpdate?.({
+ type: "progress",
+ stage: "browser-download",
+ progress: progress.percent,
+ });
+ };
onUpdate?.({
type: "status",
@@ -139,13 +153,7 @@ export const renderVideo = async ({
return {
version: null,
- onProgress: (progress) => {
- onUpdate?.({
- type: "progress",
- stage: "browser-download",
- progress: progress.percent,
- });
- },
+ onProgress,
};
},
});
@@ -169,10 +177,6 @@ export const renderVideo = async ({
projectRoot,
});
- const inputProps = {
- titleText: titleText.trim() || "Hello from Electron",
- };
-
const composition = await selectComposition({
serveUrl,
id: compositionId,
@@ -199,7 +203,7 @@ export const renderVideo = async ({
binariesDirectory,
browserExecutable,
cancelSignal,
- onProgress(progress) {
+ onProgress(progress: RenderMediaProgress) {
onUpdate?.({
type: "progress",
stage: "rendering",