Skip to content

🎥 Make videos programmatically with React

alright

Jonny Burger3c544e5dd971
diff --git a/packages/media/src/video-extraction/extract-frame.ts b/packages/media/src/video-extraction/extract-frame.ts
index d3ac35ce3b..008d80aa8e 100644
--- a/packages/media/src/video-extraction/extract-frame.ts
+++ b/packages/media/src/video-extraction/extract-frame.ts
@@ -65,6 +65,13 @@ const extractFrameInternal = async ({
return {type: 'network-error'};
}
+ if (video === 'cannot-decode-alpha') {
+ return {
+ type: 'cannot-decode-alpha',
+ durationInSeconds: mediaDurationInSeconds,
+ };
+ }
+
const timeInSeconds = getTimeInSeconds({
loop,
mediaDurationInSeconds,
@@ -90,7 +97,6 @@ const extractFrameInternal = async ({
// Should be able to remove once upgraded to Chrome 145
try {
const keyframeBank = await keyframeManager.requestKeyframeBank({
- packetSink: video.packetSink,
videoSampleSink: video.sampleSink,
timestamp: timeInSeconds,
src,
@@ -98,13 +104,6 @@ const extractFrameInternal = async ({
maxCacheSize,
});
- if (keyframeBank === 'has-alpha') {
- return {
- type: 'cannot-decode-alpha',
- durationInSeconds: await sink.getDuration(),
- };
- }
-
if (!keyframeBank) {
return {
type: 'success',
diff --git a/packages/media/src/video-extraction/get-frames-since-keyframe.ts b/packages/media/src/video-extraction/get-frames-since-keyframe.ts
index 3b7eaf867f..a7939ec1e2 100644
--- a/packages/media/src/video-extraction/get-frames-since-keyframe.ts
+++ b/packages/media/src/video-extraction/get-frames-since-keyframe.ts
@@ -9,12 +9,12 @@ import {
VideoSampleSink,
WEBM,
} from 'mediabunny';
+import {canBrowserUseWebGl2} from '../browser-can-use-webgl2';
import {isNetworkError} from '../is-type-of-error';
import {rememberActualMatroskaTimestamps} from './remember-actual-matroska-timestamps';
type VideoSinks = {
sampleSink: VideoSampleSink;
- packetSink: EncodedPacketSink;
};
type AudioSinks = {
@@ -31,6 +31,7 @@ export type VideoSinkResult =
| VideoSinks
| 'no-video-track'
| 'cannot-decode'
+ | 'cannot-decode-alpha'
| 'unknown-container-format'
| 'network-error';
@@ -83,9 +84,23 @@ export const getSinks = async (src: string) => {
return 'cannot-decode';
}
+ const sampleSink = new VideoSampleSink(videoTrack);
+ const packetSink = new EncodedPacketSink(videoTrack);
+
+ // Try to get the keypacket at the requested timestamp.
+ // If it returns null (timestamp is before the first keypacket), fall back to the first packet.
+ // This matches mediabunny's internal behavior and handles videos that don't start at timestamp 0.
+ const startPacket = await packetSink.getFirstPacket({
+ verifyKeyPackets: true,
+ });
+
+ const hasAlpha = startPacket?.sideData.alpha;
+ if (hasAlpha && !canBrowserUseWebGl2()) {
+ return 'cannot-decode-alpha';
+ }
+
return {
- sampleSink: new VideoSampleSink(videoTrack),
- packetSink: new EncodedPacketSink(videoTrack),
+ sampleSink,
};
};
diff --git a/packages/media/src/video-extraction/keyframe-manager.ts b/packages/media/src/video-extraction/keyframe-manager.ts
index cefecde400..c05b3c58fc 100644
--- a/packages/media/src/video-extraction/keyframe-manager.ts
+++ b/packages/media/src/video-extraction/keyframe-manager.ts
@@ -1,6 +1,5 @@
-import type {EncodedPacketSink, VideoSampleSink} from 'mediabunny';
+import type {VideoSampleSink} from 'mediabunny';
import {Internals, type LogLevel} from 'remotion';
-import {canBrowserUseWebGl2} from '../browser-can-use-webgl2';
import {getTotalCacheStats, SAFE_WINDOW_OF_MONOTONICITY} from '../caches';
import {renderTimestampRange} from '../render-timestamp-range';
import {type KeyframeBank, makeKeyframeBank} from './keyframe-bank';
@@ -57,7 +56,7 @@ export const makeKeyframeManager = () => {
return {count, totalSize};
};
- const getTheKeyframeBankMostInThePast = async () => {
+ const getTheKeyframeBankMostInThePast = () => {
let mostInThePast = null;
let mostInThePastBank = null;
@@ -196,38 +195,16 @@ export const makeKeyframeManager = () => {
};
const getKeyframeBankOrRefetch = async ({
- packetSink,
timestamp,
videoSampleSink,
src,
logLevel,
}: {
- packetSink: EncodedPacketSink;
timestamp: number;
videoSampleSink: VideoSampleSink;
src: string;
logLevel: LogLevel;
- }): Promise<KeyframeBank | 'has-alpha' | null> => {
- // Try to get the keypacket at the requested timestamp.
- // If it returns null (timestamp is before the first keypacket), fall back to the first packet.
- // This matches mediabunny's internal behavior and handles videos that don't start at timestamp 0.
- const startPacket = await packetSink.getFirstPacket({
- verifyKeyPackets: true,
- });
-
- // TODO: Should move out of this function
- const hasAlpha = startPacket?.sideData.alpha;
- if (hasAlpha && !canBrowserUseWebGl2()) {
- return 'has-alpha';
- }
-
- if (!startPacket) {
- // e.g. https://discord.com/channels/809501355504959528/809501355504959531/1424400511070765086
- // The video has an offset and the first frame is at time 0.033sec
- // we shall not crash here but handle it gracefully
- return null;
- }
-
+ }): Promise<KeyframeBank | null> => {
// The start packet timestamp can be higher than the packets following it
// https://discord.com/channels/809501355504959528/1001500302375125055/1456710188865159343
// e.g. key packet timestamp is 0.08sec, but the next packet is 0.04sec
@@ -279,7 +256,6 @@ export const makeKeyframeManager = () => {
};
const requestKeyframeBank = async ({
- packetSink,
timestamp,
videoSampleSink,
src,
@@ -287,7 +263,6 @@ export const makeKeyframeManager = () => {
maxCacheSize,
}: {
timestamp: number;
- packetSink: EncodedPacketSink;
videoSampleSink: VideoSampleSink;
src: string;
logLevel: LogLevel;
@@ -295,14 +270,13 @@ export const makeKeyframeManager = () => {
}) => {
await ensureToStayUnderMaxCacheSize(logLevel, maxCacheSize);
- await clearKeyframeBanksBeforeTime({
+ clearKeyframeBanksBeforeTime({
timestampInSeconds: timestamp,
src,
logLevel,
});
const keyframeBank = await getKeyframeBankOrRefetch({
- packetSink,
timestamp,
videoSampleSink,
src,
@@ -331,14 +305,12 @@ export const makeKeyframeManager = () => {
return {
requestKeyframeBank: ({
- packetSink,
timestamp,
videoSampleSink,
src,
logLevel,
maxCacheSize,
}: {
- packetSink: EncodedPacketSink;
timestamp: number;
videoSampleSink: VideoSampleSink;
src: string;
@@ -347,7 +319,6 @@ export const makeKeyframeManager = () => {
}) => {
queue = queue.then(() =>
requestKeyframeBank({
- packetSink,
timestamp,
videoSampleSink,
src,
@@ -355,7 +326,7 @@ export const makeKeyframeManager = () => {
maxCacheSize,
}),
);
- return queue as Promise<KeyframeBank | 'has-alpha' | null>;
+ return queue as Promise<KeyframeBank>;
},
getCacheStats,
clearAll,