Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: Add `seamlessTransition` prop to avoid black frames between sliced video segments

Jonny Burgerb0ff02608751
diff --git a/packages/example/src/SlicedVideo/index.tsx b/packages/example/src/SlicedVideo/index.tsx
index 420f3862a4..5235eec76d 100644
--- a/packages/example/src/SlicedVideo/index.tsx
+++ b/packages/example/src/SlicedVideo/index.tsx
@@ -21,7 +21,12 @@ export const SlicedVideo: React.FC = () => {
durationInFrames={SLICE_DURATION_FRAMES}
premountFor={PREMOUNT_SEC * fps}
>
- <Video src={src} trimBefore={from} debugAudioScheduling />
+ <Video
+ src={src}
+ trimBefore={from}
+ debugAudioScheduling
+ seamlessTransition
+ />
</Sequence>
);
})}
diff --git a/packages/media/src/video-iterator-manager.ts b/packages/media/src/video-iterator-manager.ts
index 8ade5a780f..e52c9dc796 100644
--- a/packages/media/src/video-iterator-manager.ts
+++ b/packages/media/src/video-iterator-manager.ts
@@ -39,8 +39,13 @@ export const videoIteratorManager = ({
let currentDelayHandle: {unblock: () => void} | null = null;
if (canvas) {
- canvas.width = videoTrack.displayWidth;
- canvas.height = videoTrack.displayHeight;
+ if (
+ canvas.width !== videoTrack.displayWidth ||
+ canvas.height !== videoTrack.displayHeight
+ ) {
+ canvas.width = videoTrack.displayWidth;
+ canvas.height = videoTrack.displayHeight;
+ }
}
const canvasSink = new CanvasSink(videoTrack, {
diff --git a/packages/media/src/video/props.ts b/packages/media/src/video/props.ts
index 3605a03a33..9b55457175 100644
--- a/packages/media/src/video/props.ts
+++ b/packages/media/src/video/props.ts
@@ -67,6 +67,7 @@ type OptionalVideoProps = {
onError: MediaOnError | undefined;
credentials: RequestCredentials | undefined;
objectFit: VideoObjectFit;
+ seamlessTransition: boolean;
};
export type InnerVideoProps = MandatoryVideoProps &
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index f81e3077cc..713f6ddc18 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -26,6 +26,7 @@ import {useLoopDisplay} from '../show-in-timeline';
import {useCommonEffects} from '../use-common-effects';
import {useMediaInTimeline} from '../use-media-in-timeline';
import type {FallbackOffthreadVideoProps, VideoObjectFit} from './props';
+import {cacheVideoFrame, getCachedVideoFrame} from './video-frame-cache';
import {warnAboutObjectFitInStyleOrClassName} from './warn-object-fit-css';
const {
@@ -67,6 +68,7 @@ type VideoForPreviewProps = {
readonly onError: MediaOnError | undefined;
readonly credentials: RequestCredentials | undefined;
readonly objectFit: VideoObjectFit;
+ readonly seamlessTransition: boolean;
};
type VideoForPreviewAssertedShowingProps = VideoForPreviewProps & {
@@ -101,6 +103,7 @@ const VideoForPreviewAssertedShowing: React.FC<
credentials,
controls,
objectFit: objectFitProp,
+ seamlessTransition,
}) => {
const src = usePreload(unpreloadedSrc);
@@ -205,6 +208,51 @@ const VideoForPreviewAssertedShowing: React.FC<
const initialMuted = useRef(effectiveMuted);
const initialDurationInFrames = useRef(videoConfig.durationInFrames);
const initialSequenceOffset = useRef(sequenceOffset);
+ const hasDrawnRealFrameRef = useRef(false);
+
+ useLayoutEffect(() => {
+ if (!seamlessTransition) {
+ return;
+ }
+
+ const canvas = canvasRef.current;
+ if (!canvas) {
+ return;
+ }
+
+ const cached = getCachedVideoFrame(src);
+ if (!cached) {
+ return;
+ }
+
+ canvas.width = cached.width;
+ canvas.height = cached.height;
+ const ctx = canvas.getContext('2d', {
+ alpha: true,
+ desynchronized: true,
+ });
+ if (!ctx) {
+ return;
+ }
+
+ ctx.drawImage(cached, 0, 0);
+ }, [seamlessTransition, src]);
+
+ useLayoutEffect(() => {
+ if (!seamlessTransition) {
+ return;
+ }
+
+ const canvas = canvasRef.current;
+
+ return () => {
+ if (!canvas || !hasDrawnRealFrameRef.current) {
+ return;
+ }
+
+ cacheVideoFrame(src, canvas);
+ };
+ }, [seamlessTransition, src]);
useEffect(() => {
if (!sharedAudioContext) return;
@@ -301,6 +349,7 @@ const VideoForPreviewAssertedShowing: React.FC<
if (result.type === 'success') {
setMediaPlayerReady(true);
setMediaDurationInSeconds(result.durationInSeconds);
+ hasDrawnRealFrameRef.current = true;
}
})
.catch((error) => {
@@ -354,6 +403,7 @@ const VideoForPreviewAssertedShowing: React.FC<
setMediaPlayerReady(false);
setShouldFallbackToNativeVideo(false);
+ hasDrawnRealFrameRef.current = false;
};
}, [
audioStreamIndex,
diff --git a/packages/media/src/video/video-frame-cache.ts b/packages/media/src/video/video-frame-cache.ts
new file mode 100644
index 0000000000..b252bb48c3
--- /dev/null
+++ b/packages/media/src/video/video-frame-cache.ts
@@ -0,0 +1,29 @@
+const cache = new Map<string, OffscreenCanvas>();
+
+export const cacheVideoFrame = (
+ src: string,
+ sourceCanvas: HTMLCanvasElement | OffscreenCanvas,
+): void => {
+ const {width, height} = sourceCanvas;
+ if (width === 0 || height === 0) {
+ return;
+ }
+
+ let cached = cache.get(src);
+ if (!cached || cached.width !== width || cached.height !== height) {
+ cached = new OffscreenCanvas(width, height);
+ cache.set(src, cached);
+ }
+
+ const ctx = cached.getContext('2d');
+ if (!ctx) {
+ return;
+ }
+
+ ctx.clearRect(0, 0, width, height);
+ ctx.drawImage(sourceCanvas, 0, 0);
+};
+
+export const getCachedVideoFrame = (src: string): OffscreenCanvas | null => {
+ return cache.get(src) ?? null;
+};
diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsx
index 23a7c121b5..8eab5fc59e 100644
--- a/packages/media/src/video/video.tsx
+++ b/packages/media/src/video/video.tsx
@@ -88,6 +88,7 @@ const InnerVideo: React.FC<
credentials,
controls,
objectFit,
+ seamlessTransition,
}) => {
const environment = useRemotionEnvironment();
@@ -177,6 +178,7 @@ const InnerVideo: React.FC<
credentials={credentials}
controls={controls}
objectFit={objectFit}
+ seamlessTransition={seamlessTransition}
/>
);
};
@@ -214,6 +216,7 @@ const VideoInner: React.FC<
credentials,
controls,
objectFit,
+ seamlessTransition,
from,
durationInFrames,
}) => {
@@ -258,6 +261,7 @@ const VideoInner: React.FC<
credentials={credentials}
controls={controls}
objectFit={objectFit ?? 'contain'}
+ seamlessTransition={seamlessTransition ?? false}
/>
</Sequence>
);