Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: Fix frames being rendered out of order in `<Video>` for rendering

Jonny Burger7a9ee998a315
diff --git a/packages/media/src/audio-extraction/audio-iterator.ts b/packages/media/src/audio-extraction/audio-iterator.ts
index 84dcb678d5..86d2ca8356 100644
--- a/packages/media/src/audio-extraction/audio-iterator.ts
+++ b/packages/media/src/audio-extraction/audio-iterator.ts
@@ -1,6 +1,6 @@
import type {AudioSample, AudioSampleSink} from 'mediabunny';
import {Internals, type LogLevel} from 'remotion';
-import {SAFE_BACK_WINDOW_IN_SECONDS} from '../caches';
+import {SAFE_WINDOW_OF_MONOTOCY} from '../caches';
import type {RememberActualMatroskaTimestamps} from '../video-extraction/remember-actual-matroska-timestamps';
import {makeAudioCache} from './audio-cache';
@@ -93,7 +93,7 @@ export const makeAudioIterator = ({
if (fullDuration !== null && timestamp > fullDuration) {
// Clear all samples before the timestamp
// Do this in the while loop because samples might start from 0
- cache.clearBeforeThreshold(fullDuration - SAFE_BACK_WINDOW_IN_SECONDS);
+ cache.clearBeforeThreshold(fullDuration - SAFE_WINDOW_OF_MONOTOCY);
return [];
}
@@ -121,7 +121,7 @@ export const makeAudioIterator = ({
const deleteBefore =
fullDuration === null ? timestamp : Math.min(timestamp, fullDuration);
- cache.clearBeforeThreshold(deleteBefore - SAFE_BACK_WINDOW_IN_SECONDS);
+ cache.clearBeforeThreshold(deleteBefore - SAFE_WINDOW_OF_MONOTOCY);
if (sample === null) {
break;
diff --git a/packages/media/src/caches.ts b/packages/media/src/caches.ts
index 1d37bb16d3..ab4636d841 100644
--- a/packages/media/src/caches.ts
+++ b/packages/media/src/caches.ts
@@ -3,8 +3,8 @@ import {cancelRender, Internals, type LogLevel} from 'remotion';
import {makeAudioManager} from './audio-extraction/audio-manager';
import {makeKeyframeManager} from './video-extraction/keyframe-manager';
-// TODO: make it dependent on the fps and concurrency
-export const SAFE_BACK_WINDOW_IN_SECONDS = 1;
+// Frames can be out of order, but we don't expect them to be more than 1 second out of order
+export const SAFE_WINDOW_OF_MONOTOCY = 1;
export const keyframeManager = makeKeyframeManager();
export const audioManager = makeAudioManager();
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 d148babb5b..592da44bba 100644
--- a/packages/media/src/video-extraction/get-frames-since-keyframe.ts
+++ b/packages/media/src/video-extraction/get-frames-since-keyframe.ts
@@ -10,6 +10,7 @@ import {
WEBM,
} from 'mediabunny';
import type {LogLevel} from 'remotion';
+import {roundTo4Digits} from '../helpers/round-to-4-digits';
import {isNetworkError} from '../is-type-of-error';
import {makeKeyframeBank} from './keyframe-bank';
import {rememberActualMatroskaTimestamps} from './remember-actual-matroska-timestamps';
@@ -162,20 +163,27 @@ export const getFramesSinceKeyframe = async ({
startPacket,
logLevel,
src,
+ requestedTimestamp,
}: {
packetSink: EncodedPacketSink;
videoSampleSink: VideoSampleSink;
startPacket: EncodedPacket;
logLevel: LogLevel;
src: string;
+ requestedTimestamp: number;
}) => {
const nextKeyPacket = await packetSink.getNextKeyPacket(startPacket, {
verifyKeyPackets: true,
});
const sampleIterator = videoSampleSink.samples(
- startPacket.timestamp,
- nextKeyPacket ? nextKeyPacket.timestamp : Infinity,
+ // The start packet timestamp can 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
+
+ // Rounding to 4 digits because of https://github.com/Vanilagy/mediabunny/issues/269
+ roundTo4Digits(requestedTimestamp),
+ nextKeyPacket ? roundTo4Digits(nextKeyPacket.timestamp) : Infinity,
);
const keyframeBank = makeKeyframeBank({
diff --git a/packages/media/src/video-extraction/keyframe-bank.ts b/packages/media/src/video-extraction/keyframe-bank.ts
index 6259a92c69..c99ac14080 100644
--- a/packages/media/src/video-extraction/keyframe-bank.ts
+++ b/packages/media/src/video-extraction/keyframe-bank.ts
@@ -1,6 +1,6 @@
import type {VideoSample} from 'mediabunny';
import {Internals, type LogLevel} from 'remotion';
-import {SAFE_BACK_WINDOW_IN_SECONDS} from '../caches';
+import {SAFE_WINDOW_OF_MONOTOCY} from '../caches';
import {roundTo4Digits} from '../helpers/round-to-4-digits';
import {renderTimestampRange} from '../render-timestamp-range';
import {getAllocationSize} from './get-allocation-size';
@@ -103,7 +103,7 @@ export const makeKeyframeBank = ({
return (
roundTo4Digits(lastFrame.timestamp + lastFrame.duration) >
- roundTo4Digits(timestamp) + 0.001
+ roundTo4Digits(timestamp) + SAFE_WINDOW_OF_MONOTOCY
);
};
@@ -136,7 +136,7 @@ export const makeKeyframeBank = ({
deleteFramesBeforeTimestamp({
logLevel: parentLogLevel,
- timestampInSeconds: timestampInSeconds - SAFE_BACK_WINDOW_IN_SECONDS,
+ timestampInSeconds: timestampInSeconds - SAFE_WINDOW_OF_MONOTOCY,
});
}
diff --git a/packages/media/src/video-extraction/keyframe-manager.ts b/packages/media/src/video-extraction/keyframe-manager.ts
index d08ee3e645..e86bc5cbc7 100644
--- a/packages/media/src/video-extraction/keyframe-manager.ts
+++ b/packages/media/src/video-extraction/keyframe-manager.ts
@@ -1,7 +1,7 @@
import type {EncodedPacketSink, VideoSampleSink} from 'mediabunny';
import {Internals, type LogLevel} from 'remotion';
import {canBrowserUseWebGl2} from '../browser-can-use-webgl2';
-import {getTotalCacheStats, SAFE_BACK_WINDOW_IN_SECONDS} from '../caches';
+import {getTotalCacheStats, SAFE_WINDOW_OF_MONOTOCY} from '../caches';
import {renderTimestampRange} from '../render-timestamp-range';
import {getFramesSinceKeyframe} from './get-frames-since-keyframe';
import {type KeyframeBank} from './keyframe-bank';
@@ -155,7 +155,7 @@ export const makeKeyframeManager = () => {
src: string;
logLevel: LogLevel;
}) => {
- const threshold = timestampInSeconds - SAFE_BACK_WINDOW_IN_SECONDS;
+ const threshold = timestampInSeconds - SAFE_WINDOW_OF_MONOTOCY;
if (!sources[src]) {
return;
@@ -218,7 +218,10 @@ export const makeKeyframeManager = () => {
return null;
}
- const startTimestampInSeconds = startPacket.timestamp;
+ // The start packet timestamp can 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
+ const startTimestampInSeconds = Math.min(timestamp, startPacket.timestamp);
const existingBank = sources[src]?.[startTimestampInSeconds];
// Bank does not yet exist, we need to fetch
@@ -229,6 +232,7 @@ export const makeKeyframeManager = () => {
startPacket,
logLevel,
src,
+ requestedTimestamp: startTimestampInSeconds,
});
addKeyframeBank({src, bank: newKeyframeBank, startTimestampInSeconds});
@@ -258,6 +262,7 @@ export const makeKeyframeManager = () => {
startPacket,
logLevel,
src,
+ requestedTimestamp: timestamp,
});
addKeyframeBank({src, bank: replacementKeybank, startTimestampInSeconds});