Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: Convert sample to frame earlier to avoid it being closed in the meanwhile

Jonny Burger91a2b616399d
diff --git a/packages/media/src/extract-frame-and-audio.ts b/packages/media/src/extract-frame-and-audio.ts
index 5f1c157c83..0584a79ffd 100644
--- a/packages/media/src/extract-frame-and-audio.ts
+++ b/packages/media/src/extract-frame-and-audio.ts
@@ -106,10 +106,10 @@ export const extractFrameAndAudio = async ({
return {
type: 'success',
- frame: video?.sample
+ frame: video?.frame
? await rotateFrame({
- frame: video.sample.toVideoFrame(),
- rotation: video.sample.rotation,
+ frame: video.frame,
+ rotation: video.rotation,
})
: null,
audio: audio?.data ?? null,
diff --git a/packages/media/src/test/looping.test.ts b/packages/media/src/test/looping.test.ts
index 22713f27a9..4628d03b4e 100644
--- a/packages/media/src/test/looping.test.ts
+++ b/packages/media/src/test/looping.test.ts
@@ -44,7 +44,7 @@ test(
});
expect(result.type).toBe('success');
assert(result.type === 'success');
- outputTimestamps.push(result.sample?.timestamp ?? 0);
+ outputTimestamps.push(result.frame?.timestamp ?? 0);
}
expect(realTimestamps).toEqual([
diff --git a/packages/media/src/test/video-after-end.test.ts b/packages/media/src/test/video-after-end.test.ts
index 670a1638b7..f2ffa44a8e 100644
--- a/packages/media/src/test/video-after-end.test.ts
+++ b/packages/media/src/test/video-after-end.test.ts
@@ -18,7 +18,7 @@ test('Should render last frame for timestamps after video end', async () => {
});
assert(result.type === 'success');
- expect(result.sample?.timestamp).toBe(9.96);
+ expect(result.frame?.timestamp).toBe(9.96);
keyframeManager.clearAll('info');
});
diff --git a/packages/media/src/test/video-non-zero-start.test.ts b/packages/media/src/test/video-non-zero-start.test.ts
index 1000ddae66..339261593b 100644
--- a/packages/media/src/test/video-non-zero-start.test.ts
+++ b/packages/media/src/test/video-non-zero-start.test.ts
@@ -24,21 +24,21 @@ test('Should render first frame for videos starting after timestamp 0', async ()
// Should successfully extract (no error thrown)
expect(result.type).toBe('success');
assert(result.type === 'success');
- assert(result.sample, 'Frame should be returned');
+ assert(result.frame, 'Frame should be returned');
// Frame should have valid dimensions
- expect(result.sample.codedWidth).toBe(640);
- expect(result.sample.codedHeight).toBe(480);
+ expect(result.frame.codedWidth).toBe(640);
+ expect(result.frame.codedHeight).toBe(480);
// Frame timestamp should be at video start (0.15s), NOT at requested time (0s)
// This proves we're returning the first actual frame
- expect(result.sample.timestamp).toBeCloseTo(0.15, 2);
+ expect(result.frame.timestamp).toBeCloseTo(0.15, 2);
// Verify frame has pixel data (not completely black)
// testsrc generates a test pattern with colors and gradients
- const bufferSize = result.sample.codedWidth * result.sample.codedHeight * 4;
+ const bufferSize = result.frame.codedWidth * result.frame.codedHeight * 4;
const buffer = new Uint8Array(bufferSize);
- await result.sample.copyTo(buffer);
+ await result.frame.copyTo(buffer);
const hasNonZeroPixels = buffer.some((byte) => byte > 0);
expect(hasNonZeroPixels).toBe(true);
diff --git a/packages/media/src/test/video-with-no-frames-in-beginning.test.ts b/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
index a79a76c441..c66f88bfec 100644
--- a/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
+++ b/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
@@ -145,12 +145,12 @@ test('in rendering, should also be smart', async () => {
});
assert(frame.type === 'success');
if (lastFrame) {
- expect(frame.sample === lastFrame).toBe(true);
+ expect(frame.frame === lastFrame).toBe(true);
continue;
}
- expect(frame.sample?.timestamp).toBe(4.045);
- lastFrame = frame.sample;
+ expect(frame.frame?.timestamp).toBe(4.045);
+ lastFrame = frame.frame;
}
const firstRealFrame = await extractFrame({
@@ -166,7 +166,7 @@ test('in rendering, should also be smart', async () => {
});
assert(firstRealFrame.type === 'success');
- expect(firstRealFrame.sample?.timestamp).toBe(4.979);
+ expect(firstRealFrame.frame?.timestamp).toBe(4.979);
keyframeManager.clearAll('info');
});
diff --git a/packages/media/src/video-extraction/extract-frame.ts b/packages/media/src/video-extraction/extract-frame.ts
index 008d80aa8e..b5876d818e 100644
--- a/packages/media/src/video-extraction/extract-frame.ts
+++ b/packages/media/src/video-extraction/extract-frame.ts
@@ -1,4 +1,3 @@
-import type {VideoSample} from 'mediabunny';
import {Internals, type LogLevel} from 'remotion';
import {keyframeManager} from '../caches';
import {getSink} from '../get-sink';
@@ -7,7 +6,8 @@ import {getTimeInSeconds} from '../get-time-in-seconds';
type ExtractFrameResult =
| {
type: 'success';
- sample: VideoSample | null;
+ frame: VideoFrame | null;
+ rotation: number;
durationInSeconds: number | null;
}
| {type: 'cannot-decode'; durationInSeconds: number | null}
@@ -87,7 +87,8 @@ const extractFrameInternal = async ({
if (timeInSeconds === null) {
return {
type: 'success',
- sample: null,
+ frame: null,
+ rotation: 0,
durationInSeconds: await sink.getDuration(),
};
}
@@ -107,16 +108,19 @@ const extractFrameInternal = async ({
if (!keyframeBank) {
return {
type: 'success',
- sample: null,
+ frame: null,
+ rotation: 0,
durationInSeconds: await sink.getDuration(),
};
}
const frame = await keyframeBank.getFrameFromTimestamp(timeInSeconds);
+ const rotation = frame?.rotation ?? 0;
return {
type: 'success',
- sample: frame,
+ frame: frame?.toVideoFrame() ?? null,
+ rotation,
durationInSeconds: await sink.getDuration(),
};
} catch (err) {