Skip to content

🎥 Make videos programmatically with React

fix studio not bundling

Jonny Burger32d3f8c5a95a
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 43ab93a6bb..8e3c4b96b6 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -66,7 +66,7 @@ declare global {
remotion_audioEnabled: boolean;
remotion_videoEnabled: boolean;
remotion_puppeteerTimeout: number;
- remotion_broadcastChannel: BroadcastChannel;
+ remotion_broadcastChannel: BroadcastChannel | undefined;
remotion_inputProps: string;
remotion_envVariables: string;
remotion_isMainTab: boolean;
diff --git a/packages/video/bundle.ts b/packages/video/bundle.ts
index 7b0a120a73..9c0523cb72 100644
--- a/packages/video/bundle.ts
+++ b/packages/video/bundle.ts
@@ -8,13 +8,7 @@ console.time('Generated.');
const output = await build({
entrypoints: ['src/index.ts'],
naming: '[name].mjs',
- external: [
- '@remotion/webcodecs',
- 'remotion',
- 'react',
- 'react-dom',
- '@remotion/media-parser',
- ],
+ external: ['remotion', 'react', 'react-dom'],
});
if (!output.success) {
diff --git a/packages/video/src/extract-frame-via-broadcast-channel.ts b/packages/video/src/extract-frame-via-broadcast-channel.ts
index 58499658de..d2b002f80e 100644
--- a/packages/video/src/extract-frame-via-broadcast-channel.ts
+++ b/packages/video/src/extract-frame-via-broadcast-channel.ts
@@ -21,48 +21,54 @@ type ExtractFrameResponse =
errorStack: string;
};
-window.remotion_broadcastChannel.addEventListener('message', async (event) => {
- if (!window.remotion_isMainTab) {
- // Other tabs will also get this message, but only the main tab should process it
- return;
- }
-
- const data = event.data as ExtractFrameRequest;
- if (data.type === 'request') {
- try {
- const sample = await extractFrame({
- src: data.src,
- timestamp: data.timeInSeconds,
- logLevel: data.logLevel,
- });
-
- const frame = sample?.toVideoFrame() ?? null;
- const imageBitmap = frame ? await createImageBitmap(frame) : null;
- if (frame) {
- frame.close();
+// Doesn't exist in studio
+if (window.remotion_broadcastChannel) {
+ window.remotion_broadcastChannel.addEventListener(
+ 'message',
+ async (event) => {
+ if (!window.remotion_isMainTab) {
+ // Other tabs will also get this message, but only the main tab should process it
+ return;
}
- const response: ExtractFrameResponse = {
- type: 'response-success',
- id: data.id,
- frame: imageBitmap,
- };
-
- window.remotion_broadcastChannel.postMessage(response);
- frame?.close();
- } catch (error) {
- const response: ExtractFrameResponse = {
- type: 'response-error',
- id: data.id,
- errorStack: (error as Error).stack ?? 'No stack trace',
- };
-
- window.remotion_broadcastChannel.postMessage(response);
- }
- } else {
- throw new Error('Invalid message: ' + JSON.stringify(data));
- }
-});
+ const data = event.data as ExtractFrameRequest;
+ if (data.type === 'request') {
+ try {
+ const sample = await extractFrame({
+ src: data.src,
+ timestamp: data.timeInSeconds,
+ logLevel: data.logLevel,
+ });
+
+ const frame = sample?.toVideoFrame() ?? null;
+ const imageBitmap = frame ? await createImageBitmap(frame) : null;
+ if (frame) {
+ frame.close();
+ }
+
+ const response: ExtractFrameResponse = {
+ type: 'response-success',
+ id: data.id,
+ frame: imageBitmap,
+ };
+
+ window.remotion_broadcastChannel!.postMessage(response);
+ frame?.close();
+ } catch (error) {
+ const response: ExtractFrameResponse = {
+ type: 'response-error',
+ id: data.id,
+ errorStack: (error as Error).stack ?? 'No stack trace',
+ };
+
+ window.remotion_broadcastChannel!.postMessage(response);
+ }
+ } else {
+ throw new Error('Invalid message: ' + JSON.stringify(data));
+ }
+ },
+ );
+}
export const extractFrameViaBroadcastChannel = async ({
src,
@@ -103,20 +109,20 @@ export const extractFrameViaBroadcastChannel = async ({
if (data.type === 'response-success' && data.id === requestId) {
resolve(data.frame ? data.frame : null);
- window.remotion_broadcastChannel.removeEventListener(
+ window.remotion_broadcastChannel!.removeEventListener(
'message',
onMessage,
);
} else if (data.type === 'response-error' && data.id === requestId) {
reject(data.errorStack);
- window.remotion_broadcastChannel.removeEventListener(
+ window.remotion_broadcastChannel!.removeEventListener(
'message',
onMessage,
);
}
};
- window.remotion_broadcastChannel.addEventListener('message', onMessage);
+ window.remotion_broadcastChannel!.addEventListener('message', onMessage);
});
const request: ExtractFrameRequest = {
@@ -127,7 +133,7 @@ export const extractFrameViaBroadcastChannel = async ({
logLevel,
};
- window.remotion_broadcastChannel.postMessage(request);
+ window.remotion_broadcastChannel!.postMessage(request);
let timeoutId: NodeJS.Timeout | undefined;