🎥 Make videos programmatically with React
`@remotion/media`: Simple `debugOverlay`
JonnyBurgerafe55952e702
diff --git a/packages/docs/docs/media/video.mdx b/packages/docs/docs/media/video.mdxindex ef99b8733c..72f35318b6 100644--- a/packages/docs/docs/media/video.mdx+++ b/packages/docs/docs/media/video.mdx@@ -287,6 +287,10 @@ By default, if the video cannot be embedded using this tag, [a fallback to `<OffPass this prop to disable the fallback and fail the render instead.+### `debugOverlay?`++Shows a debug overlay on the video. This is useful for debugging the video playback.+## See also- [Source code for this component](https://github.com/remotion-dev/remotion/blob/main/packages/media/src/video/video.tsx)diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsxindex dc83be1c05..c376d06ae0 100644--- a/packages/media/src/audio/audio-for-preview.tsx+++ b/packages/media/src/audio/audio-for-preview.tsx@@ -148,6 +148,7 @@ const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({canvas: null,playbackRate,audioStreamIndex: audioStreamIndex ?? 0,+ debugOverlay: false,});mediaPlayerRef.current = player;diff --git a/packages/media/src/video/media-player.ts b/packages/media/src/video/media-player.tsindex 752d4f4c58..192a41f802 100644--- a/packages/media/src/video/media-player.ts+++ b/packages/media/src/video/media-player.ts@@ -8,6 +8,7 @@ import {} from 'mediabunny';import type {LogLevel} from 'remotion';import {Internals} from 'remotion';+import {drawPreviewOverlay} from '../debug-overlay/preview-overlay';import {getTimeInSeconds} from '../get-time-in-seconds';import {isNetworkError} from '../is-network-error';import {sleep, TimeoutError, withTimeout} from './timeout-utils';@@ -83,6 +84,8 @@ export class MediaPlayer {private mediaEnded = false;+ private debugOverlay = false;+private onVideoFrameCallback?: (frame: CanvasImageSource) => void;constructor({@@ -96,6 +99,7 @@ export class MediaPlayer {playbackRate,audioStreamIndex,fps,+ debugOverlay,}: {canvas: HTMLCanvasElement | null;src: string;@@ -107,6 +111,7 @@ export class MediaPlayer {playbackRate: number;audioStreamIndex: number;fps: number;+ debugOverlay: boolean;}) {this.canvas = canvas ?? null;this.src = src;@@ -118,6 +123,7 @@ export class MediaPlayer {this.trimAfter = trimAfter;this.audioStreamIndex = audioStreamIndex ?? 0;this.fps = fps;+ this.debugOverlay = debugOverlay;if (canvas) {const context = canvas.getContext('2d', {@@ -380,6 +386,10 @@ export class MediaPlayer {}}+ public setDebugOverlay(debugOverlay: boolean): void {+ this.debugOverlay = debugOverlay;+ }+public setPlaybackRate(rate: number): void {this.playbackRate = rate;}@@ -511,6 +521,7 @@ export class MediaPlayer {if (this.context && this.nextFrame) {this.context.clearRect(0, 0, this.canvas!.width, this.canvas!.height);this.context.drawImage(this.nextFrame.canvas, 0, 0);+ this.drawDebugOverlay();}if (this.onVideoFrameCallback && this.canvas) {@@ -546,6 +557,13 @@ export class MediaPlayer {}};+ private drawDebugOverlay(): void {+ if (!this.debugOverlay) return;+ if (this.context && this.canvas) {+ drawPreviewOverlay(this.context, this.nextFrame!);+ }+ }+private startVideoIterator = async (timeToSeek: number): Promise<void> => {if (!this.canvasSink) {return;@@ -572,13 +590,10 @@ export class MediaPlayer {`[MediaPlayer] Drew initial frame ${firstFrame.timestamp.toFixed(3)}s`,);this.context.drawImage(firstFrame.canvas, 0, 0);-- if (this.onVideoFrameCallback && this.canvas) {- this.onVideoFrameCallback(this.canvas);- }}this.nextFrame = secondFrame ?? null;+ this.drawDebugOverlay();if (secondFrame) {Internals.Log.trace(diff --git a/packages/media/src/video/props.ts b/packages/media/src/video/props.tsindex e831c6f61c..d898caac67 100644--- a/packages/media/src/video/props.ts+++ b/packages/media/src/video/props.ts@@ -46,6 +46,7 @@ type OptionalVideoProps = {trimBefore: number | undefined;toneFrequency: number;showInTimeline: boolean;+ debugOverlay: boolean;};export type InnerVideoProps = MandatoryVideoProps &diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsxindex 19a957a55e..6d8826913e 100644--- a/packages/media/src/video/video-for-preview.tsx+++ b/packages/media/src/video/video-for-preview.tsx@@ -39,6 +39,7 @@ type VideoForPreviewProps = {readonly disallowFallbackToOffthreadVideo: boolean;readonly fallbackOffthreadVideoProps: FallbackOffthreadVideoProps;readonly audioStreamIndex: number;+ readonly debugOverlay: boolean;};export const VideoForPreview: React.FC<VideoForPreviewProps> = ({@@ -60,6 +61,7 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({disallowFallbackToOffthreadVideo,fallbackOffthreadVideoProps,audioStreamIndex,+ debugOverlay,}) => {const src = usePreload(unpreloadedSrc);@@ -152,6 +154,7 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({fps: videoConfig.fps,playbackRate,audioStreamIndex,+ debugOverlay,});mediaPlayerRef.current = player;@@ -265,6 +268,7 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({playbackRate,disallowFallbackToOffthreadVideo,audioStreamIndex,+ debugOverlay,]);const classNameValue = useMemo(() => {@@ -354,6 +358,15 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({mediaPlayer.setVolume(userPreferredVolume);}, [userPreferredVolume, mediaPlayerReady]);+ useEffect(() => {+ const mediaPlayer = mediaPlayerRef.current;+ if (!mediaPlayer || !mediaPlayerReady) {+ return;+ }++ mediaPlayer.setDebugOverlay(debugOverlay);+ }, [debugOverlay, mediaPlayerReady]);+const effectivePlaybackRate = useMemo(() => playbackRate * globalPlaybackRate,[playbackRate, globalPlaybackRate],diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsxindex 93769d30fd..2895c139d9 100644--- a/packages/media/src/video/video.tsx+++ b/packages/media/src/video/video.tsx@@ -29,6 +29,7 @@ const InnerVideo: React.FC<InnerVideoProps> = ({stack,toneFrequency,showInTimeline,+ debugOverlay,}) => {const environment = useRemotionEnvironment();@@ -107,6 +108,7 @@ const InnerVideo: React.FC<InnerVideoProps> = ({stack={stack ?? null}disallowFallbackToOffthreadVideo={disallowFallbackToOffthreadVideo}fallbackOffthreadVideoProps={fallbackOffthreadVideoProps}+ debugOverlay={debugOverlay ?? false}/>);};@@ -133,6 +135,7 @@ export const Video: React.FC<VideoProps> = ({volume,stack,toneFrequency,+ debugOverlay,}) => {return (<InnerVideo@@ -164,6 +167,7 @@ export const Video: React.FC<VideoProps> = ({volume={volume ?? 1}toneFrequency={toneFrequency ?? 1}stack={stack}+ debugOverlay={debugOverlay ?? false}/>);};