Skip to content

🎥 Make videos programmatically with React

allow to customize the acceptable time shift before a video triggers a seek

Jonny Burgerf4ac5af8e4cb
diff --git a/packages/core/src/audio/AudioForDevelopment.tsx b/packages/core/src/audio/AudioForDevelopment.tsx
index 6b423f31bb..71eb30d535 100644
--- a/packages/core/src/audio/AudioForDevelopment.tsx
+++ b/packages/core/src/audio/AudioForDevelopment.tsx
@@ -12,7 +12,10 @@ import {usePreload} from '../prefetch';
import {random} from '../random';
import {SequenceContext} from '../Sequence';
import {useMediaInTimeline} from '../use-media-in-timeline';
-import {useMediaPlayback} from '../use-media-playback';
+import {
+ DEFAULT_ACCEPTABLE_TIMESHIFT,
+ useMediaPlayback,
+} from '../use-media-playback';
import {useMediaTagVolume} from '../use-media-tag-volume';
import {useSyncVolumeWithMediaTag} from '../use-sync-volume-with-media-tag';
import {
@@ -53,6 +56,7 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
shouldPreMountAudioTags,
src,
onDuration,
+ acceptableTimeShiftInSeconds,
...nativeProps
} = props;
@@ -108,6 +112,8 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
mediaType: 'audio',
playbackRate: playbackRate ?? 1,
onlyWarnForMediaSeekingError: false,
+ acceptableTimeshift:
+ acceptableTimeShiftInSeconds ?? DEFAULT_ACCEPTABLE_TIMESHIFT,
});
useImperativeHandle(
diff --git a/packages/core/src/audio/props.ts b/packages/core/src/audio/props.ts
index e122b7d8f3..097889ebd8 100644
--- a/packages/core/src/audio/props.ts
+++ b/packages/core/src/audio/props.ts
@@ -14,4 +14,5 @@ export type RemotionAudioProps = Omit<
> & {
volume?: VolumeProp;
playbackRate?: number;
+ acceptableTimeShiftInSeconds?: number;
};
diff --git a/packages/core/src/use-media-playback.ts b/packages/core/src/use-media-playback.ts
index 48e0ad908f..679f00fa02 100644
--- a/packages/core/src/use-media-playback.ts
+++ b/packages/core/src/use-media-playback.ts
@@ -12,18 +12,22 @@ import {useVideoConfig} from './use-video-config';
import {getMediaTime} from './video/get-current-time';
import {warnAboutNonSeekableMedia} from './warn-about-non-seekable-media';
+export const DEFAULT_ACCEPTABLE_TIMESHIFT = 0.45;
+
export const useMediaPlayback = ({
mediaRef,
src,
mediaType,
playbackRate: localPlaybackRate,
onlyWarnForMediaSeekingError,
+ acceptableTimeshift,
}: {
mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement>;
src: string | undefined;
mediaType: 'audio' | 'video';
playbackRate: number;
onlyWarnForMediaSeekingError: boolean;
+ acceptableTimeshift: number;
}) => {
const {playbackRate: globalPlaybackRate} = useContext(TimelineContext);
const frame = useCurrentFrame();
@@ -65,7 +69,7 @@ export const useMediaPlayback = ({
const isTime = mediaRef.current.currentTime;
const timeShift = Math.abs(shouldBeTime - isTime);
- if (timeShift > 0.45 && !mediaRef.current.ended) {
+ if (timeShift > acceptableTimeshift && !mediaRef.current.ended) {
// If scrubbing around, adjust timing
// or if time shift is bigger than 0.2sec
mediaRef.current.currentTime = shouldBeTime;
@@ -98,5 +102,6 @@ export const useMediaPlayback = ({
mediaStartsAt,
localPlaybackRate,
onlyWarnForMediaSeekingError,
+ acceptableTimeshift,
]);
};
diff --git a/packages/core/src/video/VideoForDevelopment.tsx b/packages/core/src/video/VideoForDevelopment.tsx
index 1fc40a4786..92286895e7 100644
--- a/packages/core/src/video/VideoForDevelopment.tsx
+++ b/packages/core/src/video/VideoForDevelopment.tsx
@@ -3,7 +3,10 @@ import React, {forwardRef, useEffect, useImperativeHandle, useRef} from 'react';
import {useFrameForVolumeProp} from '../audio/use-audio-frame';
import {usePreload} from '../prefetch';
import {useMediaInTimeline} from '../use-media-in-timeline';
-import {useMediaPlayback} from '../use-media-playback';
+import {
+ DEFAULT_ACCEPTABLE_TIMESHIFT,
+ useMediaPlayback,
+} from '../use-media-playback';
import {useMediaTagVolume} from '../use-media-tag-volume';
import {useSyncVolumeWithMediaTag} from '../use-sync-volume-with-media-tag';
import {
@@ -32,6 +35,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
onlyWarnForMediaSeekingError,
src,
onDuration,
+ acceptableTimeshift,
...nativeProps
} = props;
@@ -62,6 +66,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
mediaType: 'video',
playbackRate: props.playbackRate ?? 1,
onlyWarnForMediaSeekingError,
+ acceptableTimeshift: acceptableTimeshift ?? DEFAULT_ACCEPTABLE_TIMESHIFT,
});
const actualSrc = usePreload(src as string);
diff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.ts
index 7cef4ba8cd..2387dcf44f 100644
--- a/packages/core/src/video/props.ts
+++ b/packages/core/src/video/props.ts
@@ -15,6 +15,7 @@ export type RemotionVideoProps = Omit<
> & {
volume?: VolumeProp;
playbackRate?: number;
+ acceptableTimeshift?: number;
};
export type OffthreadVideoImageFormat = 'png' | 'jpeg';
@@ -28,4 +29,5 @@ export type OffthreadVideoProps = {
muted?: boolean;
onError?: React.ReactEventHandler<HTMLVideoElement | HTMLImageElement>;
imageFormat?: OffthreadVideoImageFormat;
+ acceptableTimeShiftInSeconds?: number;
};
diff --git a/packages/docs/docs/audio.md b/packages/docs/docs/audio.md
index 360133d885..6085b220e1 100644
--- a/packages/docs/docs/audio.md
+++ b/packages/docs/docs/audio.md
@@ -129,6 +129,12 @@ export const MyVideo = () => {
};
```
+## `acceptableTimeShiftInSeconds`
+
+_Available from v3.2.41_
+
+During [Preview](/docs/terminology#remotion-preview) or in the [Remotion Player](/docs/player), Remotion will seek the audio if it gets too much out of sync with Remotion's internal time - be it due to the audio loading or the page being too slow to keep up in real-time. By default, a seek is triggered if `0.45` seconds of time shift is encountered. Using this prop, you can customize the threshold.
+
## See also
- [Source code for this component](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/audio/Audio.tsx)
diff --git a/packages/docs/docs/offthreadvideo.md b/packages/docs/docs/offthreadvideo.md
index ea9804ca6f..5e41e080b2 100644
--- a/packages/docs/docs/offthreadvideo.md
+++ b/packages/docs/docs/offthreadvideo.md
@@ -39,7 +39,7 @@ export const MyComposition = () => {
## Props
-The props [`volume`](/docs/video#volume), [`playbackRate`](/docs/video#playbackrate) and [`muted`](/docs/video#muted) are supported and work the same as in [`<Video>`](/docs/video).
+The props [`volume`](/docs/video#volume), [`playbackRate`](/docs/video#playbackrate), [`muted`](/docs/video#muted) and [`acceptableTimeShiftInSeconds`](/docs/video#acceptabletimeshiftinseconds) are supported and work the same as in [`<Video>`](/docs/video).
The props [`onError`](/docs/img#onerror), `className` and `style` are supported and get passed to the underlying HTML element. Remember that during render, this is a `<img>` element, and during preview, this is a `<video>` element.
diff --git a/packages/docs/docs/video.md b/packages/docs/docs/video.md
index 7964b0aafc..472ff9a7d8 100644
--- a/packages/docs/docs/video.md
+++ b/packages/docs/docs/video.md
@@ -180,6 +180,14 @@ export const MyComposition = () => {
};
```
+## `acceptableTimeShiftInSeconds`
+
+_Available from v3.2.41_
+
+During [Preview](/docs/terminology#remotion-preview) or in the [Remotion Player](/docs/player), Remotion will seek the video if it gets too much out of sync with Remotion's internal time - be it due to the video loading or the page being too slow to keep up in real-time. By default, a seek is triggered if `0.45` seconds of time shift is encountered. Using this prop, you can customize the threshold.
+
+## Speed up renders for video with silent audio
+
Remotion will download the whole video during render in order to mix its audio. If the video contains a silent audio track, you can add the muted property to signal to Remotion that it does not need to download the video and make the render more efficient.
## Codec support