Skip to content

🎥 Make videos programmatically with React

trim off the end of an audio chunk, don't overschedule

Jonny Burger197b98e753e2
diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsx
index 78e93890c2..5fefd76391 100644
--- a/packages/media/src/audio/audio-for-preview.tsx
+++ b/packages/media/src/audio/audio-for-preview.tsx
@@ -203,6 +203,7 @@ const AudioForPreviewAssertedShowing: React.FC<
isPostmounting: initialIsPostmounting.current,
isPremounting: initialIsPremounting.current,
globalPlaybackRate: initialGlobalPlaybackRate.current,
+ durationInFrames: videoConfig.durationInFrames,
onVideoFrameCallback: null,
playing: initialPlaying.current,
});
@@ -423,6 +424,15 @@ const AudioForPreviewAssertedShowing: React.FC<
mediaPlayer.setLoop(loop);
}, [loop, mediaPlayerReady]);
+ useLayoutEffect(() => {
+ const mediaPlayer = mediaPlayerRef.current;
+ if (!mediaPlayer || !mediaPlayerReady) {
+ return;
+ }
+
+ mediaPlayer.setDurationInFrames(videoConfig.durationInFrames);
+ }, [videoConfig.durationInFrames, mediaPlayerReady]);
+
useLayoutEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer || !mediaPlayerReady) {
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index 4259fb9c10..9b1346f2e2 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -51,6 +51,7 @@ export class MediaPlayer {
private trimBefore: number | undefined;
private trimAfter: number | undefined;
+ private durationInFrames: number;
private totalDuration: number | undefined;
@@ -85,6 +86,7 @@ export class MediaPlayer {
bufferState,
isPremounting,
isPostmounting,
+ durationInFrames,
onVideoFrameCallback,
playing,
}: {
@@ -103,6 +105,7 @@ export class MediaPlayer {
bufferState: ReturnType<typeof useBufferState>;
isPremounting: boolean;
isPostmounting: boolean;
+ durationInFrames: number;
onVideoFrameCallback: null | ((frame: CanvasImageSource) => void);
playing: boolean;
}) {
@@ -121,6 +124,7 @@ export class MediaPlayer {
this.bufferState = bufferState;
this.isPremounting = isPremounting;
this.isPostmounting = isPostmounting;
+ this.durationInFrames = durationInFrames;
this.nonceManager = makeNonceManager();
this.onVideoFrameCallback = onVideoFrameCallback;
this.playing = playing;
@@ -167,7 +171,7 @@ export class MediaPlayer {
}
private getEndTime(): number {
- return calculateEndTime({
+ const mediaEndTime = calculateEndTime({
mediaDurationInSeconds: this.totalDuration!,
ifNoMediaDuration: 'fail',
src: this.src,
@@ -175,6 +179,17 @@ export class MediaPlayer {
trimBefore: this.trimBefore,
fps: this.fps,
});
+
+ if (this.loop) {
+ return mediaEndTime;
+ }
+
+ // Cap at the media time corresponding to the end of the sequence
+ const sequenceEndMediaTime =
+ (this.durationInFrames / this.fps) * this.playbackRate +
+ (this.trimBefore ?? 0) / this.fps;
+
+ return Math.min(mediaEndTime, sequenceEndMediaTime);
}
private async _initialize(
@@ -611,6 +626,10 @@ export class MediaPlayer {
this.loop = loop;
}
+ public setDurationInFrames(durationInFrames: number): void {
+ this.durationInFrames = durationInFrames;
+ }
+
public async dispose(): Promise<void> {
if (this.initializationPromise) {
try {
diff --git a/packages/media/src/test/media-player.test.ts b/packages/media/src/test/media-player.test.ts
index 1dbdb0ec35..a110ad3475 100644
--- a/packages/media/src/test/media-player.test.ts
+++ b/packages/media/src/test/media-player.test.ts
@@ -37,6 +37,7 @@ test('dispose should immediately unblock playback delays', async () => {
bufferState,
isPremounting: false,
isPostmounting: false,
+ durationInFrames: 300,
onVideoFrameCallback: null,
playing: false,
});
diff --git a/packages/media/src/test/trim-change-seek.test.ts b/packages/media/src/test/trim-change-seek.test.ts
index 391e7ae32b..1822f1975d 100644
--- a/packages/media/src/test/trim-change-seek.test.ts
+++ b/packages/media/src/test/trim-change-seek.test.ts
@@ -18,6 +18,7 @@ test('setTrimBefore and setTrimAfter should update frame when paused', async ()
bufferState: {delayPlayback: () => ({unblock: () => {}})},
isPremounting: false,
isPostmounting: false,
+ durationInFrames: 300,
onVideoFrameCallback: null,
playing: false,
});
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index c802003f4c..533138deb4 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -222,6 +222,7 @@ const VideoForPreviewAssertedShowing: React.FC<
isPremounting: initialIsPremounting.current,
isPostmounting: initialIsPostmounting.current,
globalPlaybackRate: initialGlobalPlaybackRate.current,
+ durationInFrames: videoConfig.durationInFrames,
onVideoFrameCallback: initialOnVideoFrameRef.current ?? null,
playing: initialPlaying.current,
});
@@ -473,6 +474,15 @@ const VideoForPreviewAssertedShowing: React.FC<
mediaPlayer.setLoop(loop);
}, [loop, mediaPlayerReady]);
+ useLayoutEffect(() => {
+ const mediaPlayer = mediaPlayerRef.current;
+ if (!mediaPlayer || !mediaPlayerReady) {
+ return;
+ }
+
+ mediaPlayer.setDurationInFrames(videoConfig.durationInFrames);
+ }, [videoConfig.durationInFrames, mediaPlayerReady]);
+
useLayoutEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer || !mediaPlayerReady) {