Skip to content

🎥 Make videos programmatically with React

Fix problem 2: no image at frame 0 in the rendered video

Igor Samokhovetse5a64aaf67fb
diff --git a/packages/media/src/video-extraction/keyframe-bank.ts b/packages/media/src/video-extraction/keyframe-bank.ts
index 7a0365b792..1f2b1e1503 100644
--- a/packages/media/src/video-extraction/keyframe-bank.ts
+++ b/packages/media/src/video-extraction/keyframe-bank.ts
@@ -140,15 +140,14 @@ export const makeKeyframeBank = ({
): Promise<VideoSample | null> => {
lastUsed = Date.now();
- if (timestampInSeconds < startTimestampInSeconds) {
- return Promise.reject(
- new Error(
- `Timestamp is before start timestamp (requested: ${timestampInSeconds}sec, start: ${startTimestampInSeconds})`,
- ),
- );
- }
+ // If the requested timestamp is before the start of this bank, clamp it to the start. This handles videos that don't start at timestamp 0 (issue #5915)
+ // For example, requesting frame at 0sec when video starts at 0.04sec should return the frame at 0.04sec.
+ const adjustedTimestamp = Math.max(
+ timestampInSeconds,
+ startTimestampInSeconds,
+ );
- if (timestampInSeconds > endTimestampInSeconds) {
+ if (adjustedTimestamp > endTimestampInSeconds) {
return Promise.reject(
new Error(
`Timestamp is after end timestamp (requested: ${timestampInSeconds}sec, end: ${endTimestampInSeconds})`,
@@ -156,7 +155,7 @@ export const makeKeyframeBank = ({
);
}
- await ensureEnoughFramesForTimestamp(timestampInSeconds);
+ await ensureEnoughFramesForTimestamp(adjustedTimestamp);
for (let i = frameTimestamps.length - 1; i >= 0; i--) {
const sample = frames[frameTimestamps[i]];
@@ -165,11 +164,10 @@ export const makeKeyframeBank = ({
}
if (
- roundTo4Digits(sample.timestamp) <=
- roundTo4Digits(timestampInSeconds) ||
+ roundTo4Digits(sample.timestamp) <= roundTo4Digits(adjustedTimestamp) ||
// Match 0.3333333333 to 0.33355555
// this does not satisfy the previous condition, since one rounds up and one rounds down
- Math.abs(sample.timestamp - timestampInSeconds) <= 0.001
+ Math.abs(sample.timestamp - adjustedTimestamp) <= 0.001
) {
return sample;
}
diff --git a/packages/media/src/video-extraction/keyframe-manager.ts b/packages/media/src/video-extraction/keyframe-manager.ts
index 3ca2bb75d9..ffa3468f41 100644
--- a/packages/media/src/video-extraction/keyframe-manager.ts
+++ b/packages/media/src/video-extraction/keyframe-manager.ts
@@ -201,9 +201,14 @@ export const makeKeyframeManager = () => {
src: string;
logLevel: LogLevel;
}): Promise<KeyframeBank | 'has-alpha' | null> => {
- const startPacket = await packetSink.getKeyPacket(timestamp, {
- verifyKeyPackets: true,
- });
+ // 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.getKeyPacket(timestamp, {
+ verifyKeyPackets: true,
+ })) ?? (await packetSink.getFirstPacket({verifyKeyPackets: true}));
+
const hasAlpha = startPacket?.sideData.alpha;
if (hasAlpha && !canBrowserUseWebGl2()) {
return 'has-alpha';