🎥 Make videos programmatically with React
Fix effect output size CI and cover preview and rendering
Alec Wattsaed50d372e0b
diff --git a/packages/docs/docs/media/video.mdx b/packages/docs/docs/media/video.mdxindex 838fb67c42..7560a5a2ac 100644--- a/packages/docs/docs/media/video.mdx+++ b/packages/docs/docs/media/video.mdx@@ -67,7 +67,7 @@ The URL of the video to be rendered. Can be a remote URL or a local file referenApply [effects](/docs/effects/api) to the video frame after it has been drawn to the canvas.-### `effectsOutputSize?`<AvailableFrom v="4.0.513" />+### `effectsOutputSize?`<AvailableFrom v="4.0.527" />Sets the width and height of the canvas on which [`effects`](#effects) are evaluated. By default, the encoded video dimensions are used.diff --git a/packages/media/src/test/premount-arm-kicks-scheduler.test.ts b/packages/media/src/test/premount-arm-kicks-scheduler.test.tsindex 3b67fcb5a6..71d99450dd 100644--- a/packages/media/src/test/premount-arm-kicks-scheduler.test.ts+++ b/packages/media/src/test/premount-arm-kicks-scheduler.test.ts@@ -38,6 +38,7 @@ const makePlayer = ({tagType: 'audio',getEffects: () => [],getEffectChainState: () => null,+ getEffectsOutputSize: () => null,});};diff --git a/packages/media/src/test/video-effects-output-size.test.tsx b/packages/media/src/test/video-effects-output-size.test.tsxindex 8ee276cdf7..d89eb9f053 100644--- a/packages/media/src/test/video-effects-output-size.test.tsx+++ b/packages/media/src/test/video-effects-output-size.test.tsx@@ -44,51 +44,59 @@ const waitFor = async (predicate: () => boolean) => {throw new Error('Timed out waiting for condition');};-test('applies effects at effectsOutputSize in preview', async () => {- const container = document.createElement('div');- document.body.appendChild(container);+test.each([+ {effectsOutputSize: {width: 320, height: 180}, width: 320, height: 180},+ {effectsOutputSize: undefined, width: 1280, height: 720},+])(+ 'applies effects at $width × $height in preview',+ async ({effectsOutputSize, width, height}) => {+ const container = document.createElement('div');+ document.body.appendChild(container);- const Composition: React.FC = () => {- return (- <Video- data-testid="video"- effects={[passthrough()]}- effectsOutputSize={{width: 320, height: 180}}- muted- src="/bigbuckbunny.mp4"- />+ const Composition: React.FC = () => {+ return (+ <Video+ data-testid="video"+ effects={[passthrough()]}+ effectsOutputSize={effectsOutputSize}+ muted+ src="/bigbuckbunny.mp4"+ />+ );+ };++ const root = createRoot(container);+ root.render(+ <Player+ acknowledgeRemotionLicense+ component={Composition}+ compositionHeight={720}+ compositionWidth={1280}+ durationInFrames={100}+ fps={30}+ initiallyMuted+ inputProps={{}}+ />,);- };- const root = createRoot(container);- root.render(- <Player- acknowledgeRemotionLicense- component={Composition}- compositionHeight={720}- compositionWidth={1280}- durationInFrames={100}- fps={30}- initiallyMuted- inputProps={{}}- />,- );+ try {+ await waitFor(() => {+ const currentCanvas = container.querySelector(+ '[data-testid="video"]',+ ) as HTMLCanvasElement | null;+ return (+ currentCanvas?.width === width && currentCanvas.height === height+ );+ });- try {- await waitFor(() => {- const currentCanvas = container.querySelector(+ const canvas = container.querySelector('[data-testid="video"]',- ) as HTMLCanvasElement | null;- return currentCanvas?.width === 320 && currentCanvas.height === 180;- });-- const canvas = container.querySelector(- '[data-testid="video"]',- ) as HTMLCanvasElement;- expect(canvas.width).toBe(320);- expect(canvas.height).toBe(180);- } finally {- root.unmount();- container.remove();- }-});+ ) as HTMLCanvasElement;+ expect(canvas.width).toBe(width);+ expect(canvas.height).toBe(height);+ } finally {+ root.unmount();+ container.remove();+ }+ },+);diff --git a/packages/media/src/video/props.ts b/packages/media/src/video/props.tsindex bacc82f71b..188268d586 100644--- a/packages/media/src/video/props.ts+++ b/packages/media/src/video/props.ts@@ -95,8 +95,9 @@ export type NativeVideoProps = Omit<export type InnerVideoProps = MandatoryVideoProps &OuterVideoProps &- Omit<OptionalVideoProps, 'effects'> &+ Omit<OptionalVideoProps, 'effects' | 'effectsOutputSize'> &NativeVideoProps & {+ effectsOutputSize: EffectsOutputSize | null;effects: EffectDefinitionAndStack<unknown>[];_remotionInternalStack: string | undefined;};diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsxindex 2f649bad0d..9cdccafa10 100644--- a/packages/media/src/video/video-for-preview.tsx+++ b/packages/media/src/video/video-for-preview.tsx@@ -79,7 +79,7 @@ type VideoForPreviewProps = NativeVideoProps & {readonly setMediaDurationInSeconds: (durationInSeconds: number) => void;readonly _experimentalInitiallyDrawCachedFrame: boolean;readonly effects: EffectDefinitionAndStack<unknown>[];- readonly effectsOutputSize: EffectsOutputSize | undefined;+ readonly effectsOutputSize: EffectsOutputSize | null;readonly refForOutline: React.RefObject<HTMLElement | null>;};diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsxindex 94589a212d..2222cd3a34 100644--- a/packages/media/src/video/video-for-rendering.tsx+++ b/packages/media/src/video/video-for-rendering.tsx@@ -63,7 +63,7 @@ type InnerVideoProps = NativeVideoProps & {readonly requestInit: MediaRequestInit | undefined;readonly objectFit: VideoObjectFit;readonly effects: EffectDefinitionAndStack<unknown>[];- readonly effectsOutputSize: EffectsOutputSize | undefined;+ readonly effectsOutputSize: EffectsOutputSize | null;};type FallbackToOffthreadVideo = {@@ -330,7 +330,14 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({? resolveEffectsOutputSize({sourceWidth: imageBitmap.width,sourceHeight: imageBitmap.height,- effectsOutputSize: effectsOutputSize ?? null,+ effectsOutputSize:+ effectsOutputSize?.width === undefined ||+ effectsOutputSize?.height === undefined+ ? null+ : {+ width: effectsOutputSize.width,+ height: effectsOutputSize.height,+ },}): {width: imageBitmap.width, height: imageBitmap.height};diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsxindex 881cb5eaa4..87091f816e 100644--- a/packages/media/src/video/video.tsx+++ b/packages/media/src/video/video.tsx@@ -171,7 +171,7 @@ const InnerVideo: React.FC<requestInit={requestInit}objectFit={objectFit}effects={effects}- effectsOutputSize={effectsOutputSize}+ effectsOutputSize={effectsOutputSize ?? null}/>);}@@ -206,7 +206,7 @@ const InnerVideo: React.FC<controls={controls}objectFit={objectFit}effects={effects}- effectsOutputSize={effectsOutputSize}+ effectsOutputSize={effectsOutputSize ?? null}_experimentalInitiallyDrawCachedFrame={_experimentalInitiallyDrawCachedFrame}@@ -434,7 +434,7 @@ const VideoInner: React.FC<_experimentalInitiallyDrawCachedFrame ?? false}effects={memoizedEffects}- effectsOutputSize={effectsOutputSize}+ effectsOutputSize={effectsOutputSize ?? null}setMediaDurationInSeconds={setMediaDurationInSeconds}refForOutline={refForOutline}/>diff --git a/packages/web-renderer/src/test/video.test.tsx b/packages/web-renderer/src/test/video.test.tsxindex 2f265c24f1..d675c1fa9a 100644--- a/packages/web-renderer/src/test/video.test.tsx+++ b/packages/web-renderer/src/test/video.test.tsx@@ -1,6 +1,6 @@import {Video} from '@remotion/media';import {Input} from 'mediabunny';-import {AbsoluteFill, staticFile} from 'remotion';+import {AbsoluteFill, Internals, staticFile} from 'remotion';import {expect, test, vi} from 'vitest';import {renderStillOnWeb} from '../render-still-on-web';import '../symbol-dispose';@@ -60,6 +60,61 @@ test('can extract a video frame', async (t) => {}});+test('evaluates video effects at the requested output size when rendering', async (t) => {+ if (t.task.file.projectName === 'chromium') {+ t.skip();+ return;+ }++ const sizes: Array<{width: number; height: number}> = [];+ const effect = Internals.createEffect<+ Record<string, never>,+ CanvasRenderingContext2D+ >({+ type: 'dev.remotion.test.output-size',+ label: 'output-size()',+ documentationLink: null,+ backend: '2d',+ calculateKey: () => 'output-size',+ setup: (target) => target.getContext('2d')!,+ apply: ({source, state, width, height}) => {+ sizes.push({width, height});+ state.drawImage(source, 0, 0, width, height);+ },+ cleanup: () => undefined,+ schema: {},+ validateParams: () => undefined,+ });+ const Component: React.FC = () => (+ <Video+ muted+ src={staticFile('video.mp4')}+ effects={[effect()]}+ effectsOutputSize={{width: 320, height: 180}}+ />+ );+ const result = await renderStillOnWeb({+ licenseKey: 'free-license',+ composition: {+ component: Component,+ id: 'effects-output-size',+ width: 320,+ height: 180,+ fps: 25,+ durationInFrames: 100,+ calculateMetadata: () => Promise.resolve({}),+ },+ frame: 20,+ inputProps: {},+ delayRenderTimeoutInMilliseconds: 5000,+ });+ expect((await result.blob({format: 'png'})).size).toBeGreaterThan(0);+ expect(sizes.length).toBeGreaterThan(0);+ expect(+ sizes.every(({width, height}) => width === 320 && height === 180),+ ).toBe(true);+});+test('cannot render inside an svg tag', async () => {const Component: React.FC = () => {return (