Skip to content

🎥 Make videos programmatically with React

fix drawing initial frame

Igor Samokhovetsdf62869435f8
diff --git a/packages/video/src/media-player.ts b/packages/video/src/media-player.ts
index c3515be018..99928274e1 100644
--- a/packages/video/src/media-player.ts
+++ b/packages/video/src/media-player.ts
@@ -179,6 +179,45 @@ export class MediaPlayer {
}
}
+ public async drawInitialFrame(time: number = 0): Promise<void> {
+ if (!this.initialized || !this.canvasSink) {
+ Log.trace(
+ this.logLevel,
+ `[MediaPlayer] Cannot draw initial frame - not initialized or no canvas sink`,
+ );
+ return;
+ }
+
+ try {
+ Log.trace(
+ this.logLevel,
+ `[MediaPlayer] Drawing initial frame at ${time.toFixed(3)}s`,
+ );
+
+ // create temporary iterator just to get the first frame
+ const tempIterator = this.canvasSink.canvases(time);
+ const firstFrame = (await tempIterator.next()).value;
+
+ if (firstFrame) {
+ this.context.drawImage(firstFrame.canvas, 0, 0);
+ Log.trace(
+ this.logLevel,
+ `[MediaPlayer] Drew initial frame at timestamp ${firstFrame.timestamp.toFixed(3)}s`,
+ );
+ } else {
+ Log.trace(
+ this.logLevel,
+ `[MediaPlayer] No frame available at ${time.toFixed(3)}s`,
+ );
+ }
+
+ // clean up the temporary iterator
+ await tempIterator.return();
+ } catch (error) {
+ Log.error('[MediaPlayer] Failed to draw initial frame', error);
+ }
+ }
+
public async play(): Promise<void> {
if (!this.initialized || !this.sharedAudioContext) {
return;
diff --git a/packages/video/src/new-video-for-preview.tsx b/packages/video/src/new-video-for-preview.tsx
index 8d0c24d9a8..9289b553c2 100644
--- a/packages/video/src/new-video-for-preview.tsx
+++ b/packages/video/src/new-video-for-preview.tsx
@@ -40,6 +40,8 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
const actualFps = videoConfig.fps / playbackRate;
const currentTime = frame / actualFps;
+ const [initialTimestamp] = useState(currentTime);
+
useEffect(() => {
if (!canvasRef.current) return;
if (!sharedAudioContext) return;
@@ -66,6 +68,14 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
logLevel,
`[NewVideoForPreview] MediaPlayer initialized successfully`,
);
+
+ // draw initial frame
+ player.drawInitialFrame(initialTimestamp).catch((error) => {
+ Log.error(
+ '[NewVideoForPreview] Failed to draw initial frame',
+ error,
+ );
+ });
})
.catch((error) => {
Log.error(
@@ -86,8 +96,10 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
mediaPlayerRef.current.dispose();
mediaPlayerRef.current = null;
}
+
+ setMediaPlayerReady(false);
};
- }, [src, logLevel, sharedAudioContext]);
+ }, [src, logLevel, sharedAudioContext, initialTimestamp]);
// sync play/pause state with Remotion timeline (like old VideoForPreview video does)
useEffect(() => {