Skip to content

🎥 Make videos programmatically with React

better seeking

JonnyBurger8dc38e0fd369
diff --git a/packages/media/src/helpers/round-to-4-digits.ts b/packages/media/src/helpers/round-to-4-digits.ts
new file mode 100644
index 0000000000..08425f2923
--- /dev/null
+++ b/packages/media/src/helpers/round-to-4-digits.ts
@@ -0,0 +1,4 @@
+// Round to only 4 digits, because WebM has a timescale of 1_000, e.g. framer.webm
+export const roundTo4Digits = (timestamp: number) => {
+ return Math.round(timestamp * 1_000) / 1_000;
+};
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index 739f132d83..65b974bb99 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -15,6 +15,7 @@ import {
} from './audio/audio-preview-iterator';
import {drawPreviewOverlay} from './debug-overlay/preview-overlay';
import {getTimeInSeconds} from './get-time-in-seconds';
+import {roundTo4Digits} from './helpers/round-to-4-digits';
import {isNetworkError} from './is-network-error';
import {sleep, TimeoutError, withTimeout} from './video/timeout-utils';
import {
@@ -350,8 +351,9 @@ export class MediaPlayer {
currentPlaybackTime === null ||
Math.abs(newTime - currentPlaybackTime) > SEEK_THRESHOLD;
+ this.audioSyncAnchor = this.sharedAudioContext.currentTime - newTime;
+
if (isSignificantSeek) {
- this.audioSyncAnchor = this.sharedAudioContext.currentTime - newTime;
this.mediaEnded = false;
await Promise.all([
@@ -359,7 +361,7 @@ export class MediaPlayer {
this.startVideoIterator(newTime),
]);
} else if (!this.playing) {
- await this.render();
+ this.render();
}
}
@@ -520,7 +522,7 @@ export class MediaPlayer {
}
const nextFrame =
- await this.videoFrameIterator?.getNextOrNullIfNotAvailable();
+ await this.videoFrameIterator.getNextOrNullIfNotAvailable();
const frame = await (nextFrame.type === 'got-frame-or-end'
? nextFrame.frame
: nextFrame.waitPromise());
@@ -565,7 +567,10 @@ export class MediaPlayer {
return true;
}
- return this.drawnFrame.endTimestamp >= playbackTime;
+ return (
+ roundTo4Digits(this.drawnFrame.endTimestamp) <=
+ roundTo4Digits(playbackTime)
+ );
}
private startAudioIterator = (startFromSecond: number): void => {
diff --git a/packages/media/src/video-extraction/keyframe-bank.ts b/packages/media/src/video-extraction/keyframe-bank.ts
index dd9ef61116..7a0365b792 100644
--- a/packages/media/src/video-extraction/keyframe-bank.ts
+++ b/packages/media/src/video-extraction/keyframe-bank.ts
@@ -1,6 +1,7 @@
import type {VideoSample} from 'mediabunny';
import {Internals, type LogLevel} from 'remotion';
import {SAFE_BACK_WINDOW_IN_SECONDS} from '../caches';
+import {roundTo4Digits} from '../helpers/round-to-4-digits';
import {renderTimestampRange} from '../render-timestamp-range';
export type KeyframeBank = {
@@ -25,11 +26,6 @@ export type KeyframeBank = {
getLastUsed: () => number;
};
-// Round to only 4 digits, because WebM has a timescale of 1_000, e.g. framer.webm
-const roundTo4Digits = (timestamp: number) => {
- return Math.round(timestamp * 1_000) / 1_000;
-};
-
export const makeKeyframeBank = ({
startTimestampInSeconds,
endTimestampInSeconds,