Skip to content

🎥 Make videos programmatically with React

seeking

Jonny Burger5a5bf1749c05
diff --git a/packages/core/src/audio/Audio.tsx b/packages/core/src/audio/Audio.tsx
index 6223d5dee1..2eddb0674d 100644
--- a/packages/core/src/audio/Audio.tsx
+++ b/packages/core/src/audio/Audio.tsx
@@ -35,7 +35,6 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
stack,
pauseWhenBuffering,
showInTimeline,
- _remotionDebugSeeking,
...otherProps
} = props;
const {loop, ...propsOtherThanLoop} = props;
@@ -157,7 +156,6 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
_remotionInternalNativeLoopPassed={
props._remotionInternalNativeLoopPassed ?? false
}
- _remotionDebugSeeking={_remotionDebugSeeking ?? false}
_remotionInternalStack={stack ?? null}
shouldPreMountAudioTags={
audioContext !== null && audioContext.numberOfAudioTags > 0
diff --git a/packages/core/src/audio/AudioForPreview.tsx b/packages/core/src/audio/AudioForPreview.tsx
index de9de945f0..d7905e3cf4 100644
--- a/packages/core/src/audio/AudioForPreview.tsx
+++ b/packages/core/src/audio/AudioForPreview.tsx
@@ -32,7 +32,6 @@ type AudioForPreviewProps = RemotionAudioProps & {
readonly pauseWhenBuffering: boolean;
readonly _remotionInternalNativeLoopPassed: boolean;
readonly _remotionInternalStack: string | null;
- readonly _remotionDebugSeeking: boolean;
readonly showInTimeline: boolean;
readonly stack?: string | undefined;
};
@@ -61,7 +60,6 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
_remotionInternalNeedsDurationCalculation,
_remotionInternalNativeLoopPassed,
_remotionInternalStack,
- _remotionDebugSeeking,
allowAmplificationDuringRender,
name,
pauseWhenBuffering,
@@ -170,7 +168,6 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
acceptableTimeShiftInSeconds ?? DEFAULT_ACCEPTABLE_TIMESHIFT,
isPremounting: Boolean(sequenceContext?.premounting),
pauseWhenBuffering,
- debugSeeking: _remotionDebugSeeking,
onAutoPlayError: null,
});
diff --git a/packages/core/src/audio/props.ts b/packages/core/src/audio/props.ts
index c396b8eb92..3671e4c18a 100644
--- a/packages/core/src/audio/props.ts
+++ b/packages/core/src/audio/props.ts
@@ -20,7 +20,6 @@ export type RemotionAudioProps = Omit<
allowAmplificationDuringRender?: boolean;
_remotionInternalNeedsDurationCalculation?: boolean;
_remotionInternalNativeLoopPassed?: boolean;
- _remotionDebugSeeking?: boolean;
toneFrequency?: number;
pauseWhenBuffering?: boolean;
showInTimeline?: boolean;
diff --git a/packages/core/src/seek.ts b/packages/core/src/seek.ts
new file mode 100644
index 0000000000..e9c31649d7
--- /dev/null
+++ b/packages/core/src/seek.ts
@@ -0,0 +1,25 @@
+import type {LogLevel} from './log';
+import {Log} from './log';
+import {isIosSafari} from './video/video-fragment';
+
+export const seek = ({
+ mediaRef,
+ time,
+ logLevel,
+ why,
+}: {
+ mediaRef: HTMLVideoElement | HTMLAudioElement;
+ time: number;
+ logLevel: LogLevel;
+ why: string;
+}): number => {
+ // iOS seeking does not support multiple decimals
+ const timeToSet = isIosSafari() ? Number(time.toFixed(1)) : time;
+ Log.trace(
+ logLevel,
+ `[seek] from ${mediaRef.currentTime} to ${timeToSet}. Reason; ${why}`,
+ );
+
+ mediaRef.currentTime = timeToSet;
+ return timeToSet;
+};
diff --git a/packages/core/src/use-media-playback.ts b/packages/core/src/use-media-playback.ts
index 360d6bce53..0786d0e919 100644
--- a/packages/core/src/use-media-playback.ts
+++ b/packages/core/src/use-media-playback.ts
@@ -3,7 +3,10 @@ import {useCallback, useContext, useEffect, useRef} from 'react';
import {useMediaStartsAt} from './audio/use-audio-frame.js';
import {useBufferUntilFirstFrame} from './buffer-until-first-frame.js';
import {BufferingContextReact, useIsPlayerBuffering} from './buffering.js';
+import {useLogLevel} from './log-level-context.js';
+import {Log} from './log.js';
import {playAndHandleNotAllowedError} from './play-and-handle-not-allowed-error.js';
+import {seek} from './seek.js';
import {
TimelineContext,
usePlayingState,
@@ -14,24 +17,10 @@ import {useMediaBuffering} from './use-media-buffering.js';
import {useRequestVideoCallbackTime} from './use-request-video-callback-time.js';
import {useVideoConfig} from './use-video-config.js';
import {getMediaTime} from './video/get-current-time.js';
-import {isIosSafari} from './video/video-fragment.js';
import {warnAboutNonSeekableMedia} from './warn-about-non-seekable-media.js';
export const DEFAULT_ACCEPTABLE_TIMESHIFT = 0.45;
-const seek = (
- mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement | null>,
- time: number,
-): void => {
- if (!mediaRef.current) {
- return;
- }
-
- // iOS seeking does not support multiple decimals
- const timeToSet = isIosSafari() ? Number(time.toFixed(1)) : time;
- mediaRef.current.currentTime = timeToSet;
-};
-
export const useMediaPlayback = ({
mediaRef,
src,
@@ -41,7 +30,6 @@ export const useMediaPlayback = ({
acceptableTimeshift,
pauseWhenBuffering,
isPremounting,
- debugSeeking,
onAutoPlayError,
}: {
mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement | null>;
@@ -52,7 +40,6 @@ export const useMediaPlayback = ({
acceptableTimeshift: number;
pauseWhenBuffering: boolean;
isPremounting: boolean;
- debugSeeking: boolean;
onAutoPlayError: null | (() => void);
}) => {
const {playbackRate: globalPlaybackRate} = useContext(TimelineContext);
@@ -64,6 +51,7 @@ export const useMediaPlayback = ({
const mediaStartsAt = useMediaStartsAt();
const lastSeekDueToShift = useRef<number | null>(null);
const lastSeek = useRef<number | null>(null);
+ const logLevel = useLogLevel();
if (!buffering) {
throw new Error(
@@ -78,15 +66,13 @@ export const useMediaPlayback = ({
return;
}
- if (debugSeeking) {
- // eslint-disable-next-line no-console
- console.log(
- `Detected ${src} as a variable FPS video. Disabling buffering while seeking.`,
- );
- }
+ Log.verbose(
+ logLevel,
+ `Detected ${src} as a variable FPS video. Disabling buffering while seeking.`,
+ );
isVariableFpsVideoMap.current[src] = true;
- }, [debugSeeking, src]);
+ }, [logLevel, src]);
const currentTime = useRequestVideoCallbackTime({
mediaRef,
@@ -179,18 +165,6 @@ export const useMediaPlayback = ({
? timeShiftRvcTag
: timeShiftMediaTag;
- if (debugSeeking) {
- // eslint-disable-next-line no-console
- console.log({
- mediaTagTime,
- rvcTime,
- shouldBeTime,
- state: mediaRef.current.readyState,
- playing: !mediaRef.current.paused,
- isVariableFpsVideo,
- });
- }
-
if (
timeShift > acceptableTimeShiftButLessThanDuration &&
lastSeekDueToShift.current !== shouldBeTime
@@ -198,20 +172,13 @@ export const useMediaPlayback = ({
// If scrubbing around, adjust timing
// or if time shift is bigger than 0.45sec
- if (debugSeeking) {
- // eslint-disable-next-line no-console
- console.log('Seeking', {
- shouldBeTime,
- isTime: mediaTagTime,
- rvcTime,
- timeShift,
- isVariableFpsVideo,
- });
- }
-
- seek(mediaRef, shouldBeTime);
- lastSeek.current = shouldBeTime;
- lastSeekDueToShift.current = shouldBeTime;
+ lastSeek.current = seek({
+ mediaRef: mediaRef.current,
+ time: shouldBeTime,
+ logLevel,
+ why: `seeking because time shift is too big. shouldBeTime = ${shouldBeTime}, isTime = ${mediaTagTime}, requestVideoCallbackTime = ${rvcTime}, timeShift = ${timeShift}, isVariableFpsVideo = ${isVariableFpsVideo}`,
+ });
+ lastSeekDueToShift.current = lastSeek.current;
if (playing && !isVariableFpsVideo) {
if (playbackRate > 0) {
bufferUntilFirstFrame(shouldBeTime);
@@ -248,8 +215,12 @@ export const useMediaPlayback = ({
if (!playing || isSomethingElseBuffering) {
if (makesSenseToSeek) {
- seek(mediaRef, shouldBeTime);
- lastSeek.current = shouldBeTime;
+ lastSeek.current = seek({
+ mediaRef: mediaRef.current,
+ time: shouldBeTime,
+ logLevel,
+ why: `not playing or something else is buffering. time offset is over seek threshold (${seekThreshold})`,
+ });
}
return;
@@ -261,8 +232,12 @@ export const useMediaPlayback = ({
absoluteFrame === 0
) {
if (makesSenseToSeek) {
- seek(mediaRef, shouldBeTime);
- lastSeek.current = shouldBeTime;
+ lastSeek.current = seek({
+ mediaRef: mediaRef.current,
+ time: shouldBeTime,
+ logLevel,
+ why: `is over timeshift threshold (threshold = ${seekThreshold}) is paused OR has reached end of timeline and is starting over`,
+ });
}
playAndHandleNotAllowedError(mediaRef, mediaType, onAutoPlayError);
@@ -278,7 +253,7 @@ export const useMediaPlayback = ({
bufferUntilFirstFrame,
buffering.buffering,
currentTime,
- debugSeeking,
+ logLevel,
desiredUnclampedTime,
isBuffering,
isMediaTagBuffering,
diff --git a/packages/core/src/video/OffthreadVideo.tsx b/packages/core/src/video/OffthreadVideo.tsx
index 3b7db3b482..0d6bff911e 100644
--- a/packages/core/src/video/OffthreadVideo.tsx
+++ b/packages/core/src/video/OffthreadVideo.tsx
@@ -71,7 +71,6 @@ export const OffthreadVideo: React.FC<OffthreadVideoProps> = (props) => {
const {
transparent,
toneMapped,
- _remotionDebugSeeking,
onAutoPlayError,
onVideoFrame,
crossOrigin,
@@ -84,7 +83,6 @@ export const OffthreadVideo: React.FC<OffthreadVideoProps> = (props) => {
<VideoForPreview
_remotionInternalStack={stack ?? null}
_remotionInternalNativeLoopPassed={false}
- _remotionDebugSeeking={_remotionDebugSeeking ?? false}
onDuration={onDuration}
onlyWarnForMediaSeekingError
pauseWhenBuffering={pauseWhenBuffering ?? false}
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index a861102b1f..8027ef8243 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -36,7 +36,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
onAutoPlayError,
...otherProps
} = props;
- const {loop, _remotionDebugSeeking, ...propsOtherThanLoop} = props;
+ const {loop, ...propsOtherThanLoop} = props;
const {fps} = useVideoConfig();
const environment = getRemotionEnvironment();
@@ -150,7 +150,6 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
_remotionInternalNativeLoopPassed={
_remotionInternalNativeLoopPassed ?? false
}
- _remotionDebugSeeking={_remotionDebugSeeking ?? false}
showInTimeline={showInTimeline ?? true}
onAutoPlayError={onAutoPlayError ?? undefined}
/>
diff --git a/packages/core/src/video/VideoForPreview.tsx b/packages/core/src/video/VideoForPreview.tsx
index 5c52d1e669..0c57eeb6fe 100644
--- a/packages/core/src/video/VideoForPreview.tsx
+++ b/packages/core/src/video/VideoForPreview.tsx
@@ -32,7 +32,6 @@ type VideoForPreviewProps = RemotionVideoProps & {
readonly pauseWhenBuffering: boolean;
readonly _remotionInternalNativeLoopPassed: boolean;
readonly _remotionInternalStack: string | null;
- readonly _remotionDebugSeeking: boolean;
readonly showInTimeline: boolean;
readonly onVideoFrame: null | OnVideoFrame;
readonly crossOrigin?: '' | 'anonymous' | 'use-credentials';
@@ -58,7 +57,6 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
name,
_remotionInternalNativeLoopPassed,
_remotionInternalStack,
- _remotionDebugSeeking,
style,
pauseWhenBuffering,
showInTimeline,
@@ -122,7 +120,6 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
acceptableTimeShiftInSeconds ?? DEFAULT_ACCEPTABLE_TIMESHIFT,
isPremounting: Boolean(parentSequence?.premounting),
pauseWhenBuffering,
- debugSeeking: _remotionDebugSeeking,
onAutoPlayError: onAutoPlayError ?? null,
});
diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsx
index 342da2c845..8094f6827c 100644
--- a/packages/core/src/video/VideoForRendering.tsx
+++ b/packages/core/src/video/VideoForRendering.tsx
@@ -18,6 +18,7 @@ import {
import {continueRender, delayRender} from '../delay-render.js';
import {getRemotionEnvironment} from '../get-remotion-environment.js';
import {isApproximatelyTheSame} from '../is-approximately-the-same.js';
+import {useLogLevel} from '../log-level-context.js';
import {random} from '../random.js';
import {useTimelinePosition} from '../timeline-position-state.js';
import {useCurrentFrame} from '../use-current-frame.js';
@@ -63,6 +64,7 @@ const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<
const sequenceContext = useContext(SequenceContext);
const mediaStartsAt = useMediaStartsAt();
const environment = getRemotionEnvironment();
+ const logLevel = useLogLevel();
const {registerRenderAsset, unregisterRenderAsset} =
useContext(RenderAssetManager);
@@ -191,11 +193,12 @@ const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<
continueRender(handle);
};
- const seek = seekToTimeMultipleUntilRight(
- current,
- currentTime,
- videoConfig.fps,
- );
+ const seek = seekToTimeMultipleUntilRight({
+ element: current,
+ desiredTime: currentTime,
+ fps: videoConfig.fps,
+ logLevel,
+ });
seek.prom.then(() => {
continueRender(handle);
@@ -240,6 +243,7 @@ const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<
onError,
delayRenderRetries,
delayRenderTimeoutInMilliseconds,
+ logLevel,
]);
const {src} = props;
diff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.ts
index 672e7f4e0b..33868ae78f 100644
--- a/packages/core/src/video/props.ts
+++ b/packages/core/src/video/props.ts
@@ -9,7 +9,6 @@ export type RemotionMainVideoProps = {
* @deprecated Only for internal `transparent` use
*/
_remotionInternalNativeLoopPassed?: boolean;
- _remotionDebugSeeking?: boolean;
};
export type RemotionVideoProps = Omit<
diff --git a/packages/core/src/video/seek-until-right.ts b/packages/core/src/video/seek-until-right.ts
index e1bf8a2bda..7933600ecd 100644
--- a/packages/core/src/video/seek-until-right.ts
+++ b/packages/core/src/video/seek-until-right.ts
@@ -1,10 +1,20 @@
import {isApproximatelyTheSame} from '../is-approximately-the-same';
+import {type LogLevel} from '../log';
+import {seek} from '../seek';
const roundTo6Commas = (num: number) => {
return Math.round(num * 100_000) / 100_000;
};
-export const seekToTime = (element: HTMLVideoElement, desiredTime: number) => {
+export const seekToTime = ({
+ element,
+ desiredTime,
+ logLevel,
+}: {
+ element: HTMLVideoElement;
+ desiredTime: number;
+ logLevel: LogLevel;
+}) => {
if (isApproximatelyTheSame(element.currentTime, desiredTime)) {
return {
wait: Promise.resolve(desiredTime),
@@ -12,7 +22,12 @@ export const seekToTime = (element: HTMLVideoElement, desiredTime: number) => {
};
}
- element.currentTime = desiredTime;
+ seek({
+ logLevel,
+ mediaRef: element,
+ time: desiredTime,
+ why: 'Seeking during rendering',
+ });
let cancel: number;
let cancelSeeked: null | (() => void) = null;
@@ -55,11 +70,17 @@ export const seekToTime = (element: HTMLVideoElement, desiredTime: number) => {
};
};
-export const seekToTimeMultipleUntilRight = (
- element: HTMLVideoElement,
- desiredTime: number,
- fps: number,
-) => {
+export const seekToTimeMultipleUntilRight = ({
+ element,
+ desiredTime,
+ fps,
+ logLevel,
+}: {
+ element: HTMLVideoElement;
+ desiredTime: number;
+ fps: number;
+ logLevel: LogLevel;
+}) => {
const threshold = 1 / fps / 2;
let currentCancel: () => void = () => undefined;
@@ -75,7 +96,11 @@ export const seekToTimeMultipleUntilRight = (
}
const prom = new Promise<void>((resolve, reject) => {
- const firstSeek = seekToTime(element, desiredTime + threshold);
+ const firstSeek = seekToTime({
+ element,
+ desiredTime: desiredTime + threshold,
+ logLevel,
+ });
firstSeek.wait.then((seekedTo) => {
const difference = Math.abs(desiredTime - seekedTo);
@@ -85,7 +110,11 @@ export const seekToTimeMultipleUntilRight = (
const sign = desiredTime > seekedTo ? 1 : -1;
- const newSeek = seekToTime(element, seekedTo + threshold * sign);
+ const newSeek = seekToTime({
+ element,
+ desiredTime: seekedTo + threshold * sign,
+ logLevel,
+ });
currentCancel = newSeek.cancel;
newSeek.wait
.then((newTime) => {
@@ -95,7 +124,11 @@ export const seekToTimeMultipleUntilRight = (
return resolve();
}
- const thirdSeek = seekToTime(element, desiredTime + threshold);
+ const thirdSeek = seekToTime({
+ element,
+ desiredTime: desiredTime + threshold,
+ logLevel,
+ });
currentCancel = thirdSeek.cancel;
return thirdSeek.wait
.then(() => {