Skip to content

🎥 Make videos programmatically with React

`remotion`: Improve sound of loop of short audios

Jonny Burgeree88f106a2f7
diff --git a/packages/core/src/audio/Audio.tsx b/packages/core/src/audio/Audio.tsx
index 537f63ae66..717e828a89 100644
--- a/packages/core/src/audio/Audio.tsx
+++ b/packages/core/src/audio/Audio.tsx
@@ -15,6 +15,7 @@ import {AudioForDevelopment} from './AudioForDevelopment.js';
import {AudioForRendering} from './AudioForRendering.js';
import type {RemotionAudioProps, RemotionMainAudioProps} from './props.js';
import {SharedAudioContext} from './shared-audio-tags.js';
+
const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
HTMLAudioElement,
RemotionAudioProps & RemotionMainAudioProps
@@ -74,7 +75,11 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
startFrom,
})}
>
- <Audio {...propsOtherThanLoop} ref={ref} />
+ <Audio
+ {...propsOtherThanLoop}
+ ref={ref}
+ _remotionInternalNativeLoopPassed
+ />
</Loop>
);
}
@@ -117,6 +122,7 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
return (
<AudioForDevelopment
+ nativeLoopPassed={props._remotionInternalNativeLoopPassed ?? false}
shouldPreMountAudioTags={
audioContext !== null && audioContext.numberOfAudioTags > 0
}
diff --git a/packages/core/src/audio/AudioForDevelopment.tsx b/packages/core/src/audio/AudioForDevelopment.tsx
index e1286670df..dd79429e30 100644
--- a/packages/core/src/audio/AudioForDevelopment.tsx
+++ b/packages/core/src/audio/AudioForDevelopment.tsx
@@ -29,6 +29,7 @@ import {useFrameForVolumeProp} from './use-audio-frame.js';
type AudioForDevelopmentProps = RemotionAudioProps & {
shouldPreMountAudioTags: boolean;
onDuration: (src: string, durationInSeconds: number) => void;
+ nativeLoopPassed: boolean;
};
const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
@@ -58,8 +59,10 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
onDuration,
acceptableTimeShiftInSeconds,
_remotionInternalNeedsDurationCalculation,
+ _remotionInternalNativeLoopPassed,
allowAmplificationDuringRender,
name,
+ nativeLoopPassed,
...nativeProps
} = props;
@@ -73,9 +76,10 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
return {
muted: muted || mediaMuted,
src: preloadedSrc,
+ loop: nativeLoopPassed,
...nativeProps,
};
- }, [mediaMuted, muted, nativeProps, preloadedSrc]);
+ }, [mediaMuted, muted, nativeLoopPassed, nativeProps, preloadedSrc]);
const sequenceContext = useContext(SequenceContext);
diff --git a/packages/core/src/audio/AudioForRendering.tsx b/packages/core/src/audio/AudioForRendering.tsx
index 482f47a515..362659014e 100644
--- a/packages/core/src/audio/AudioForRendering.tsx
+++ b/packages/core/src/audio/AudioForRendering.tsx
@@ -58,6 +58,7 @@ const AudioForRenderingRefForwardingFunction: React.ForwardRefRenderFunction<
onDuration,
toneFrequency,
_remotionInternalNeedsDurationCalculation,
+ _remotionInternalNativeLoopPassed,
acceptableTimeShiftInSeconds,
name,
...nativeProps
diff --git a/packages/core/src/audio/props.ts b/packages/core/src/audio/props.ts
index 77dbb18945..1eea7b328b 100644
--- a/packages/core/src/audio/props.ts
+++ b/packages/core/src/audio/props.ts
@@ -18,5 +18,6 @@ export type RemotionAudioProps = Omit<
acceptableTimeShiftInSeconds?: number;
allowAmplificationDuringRender?: boolean;
_remotionInternalNeedsDurationCalculation?: boolean;
+ _remotionInternalNativeLoopPassed?: boolean;
toneFrequency?: number;
};
diff --git a/packages/core/src/use-media-playback.ts b/packages/core/src/use-media-playback.ts
index 6081bc0f27..f326d53a9a 100644
--- a/packages/core/src/use-media-playback.ts
+++ b/packages/core/src/use-media-playback.ts
@@ -56,6 +56,17 @@ export const useMediaPlayback = ({
const playbackRate = localPlaybackRate * globalPlaybackRate;
+ const acceptableTimeShiftButLessThanDuration = (() => {
+ if (mediaRef.current?.duration) {
+ return Math.min(
+ mediaRef.current.duration,
+ acceptableTimeshift ?? DEFAULT_ACCEPTABLE_TIMESHIFT,
+ );
+ }
+
+ return acceptableTimeshift;
+ })();
+
useEffect(() => {
if (!playing) {
mediaRef.current?.pause();
@@ -96,7 +107,7 @@ export const useMediaPlayback = ({
const isTime = mediaRef.current.currentTime;
const timeShift = Math.abs(shouldBeTime - isTime);
- if (timeShift > acceptableTimeshift) {
+ if (timeShift > acceptableTimeShiftButLessThanDuration) {
// If scrubbing around, adjust timing
// or if time shift is bigger than 0.45sec
@@ -145,5 +156,6 @@ export const useMediaPlayback = ({
localPlaybackRate,
onlyWarnForMediaSeekingError,
acceptableTimeshift,
+ acceptableTimeShiftButLessThanDuration,
]);
};
diff --git a/packages/core/src/video/OffthreadVideo.tsx b/packages/core/src/video/OffthreadVideo.tsx
index 1da3beec59..1e1e6b52be 100644
--- a/packages/core/src/video/OffthreadVideo.tsx
+++ b/packages/core/src/video/OffthreadVideo.tsx
@@ -63,6 +63,7 @@ export const OffthreadVideo: React.FC<Omit<OffthreadVideoProps, 'loop'>> = (
return (
<VideoForDevelopment
+ nativeLoopPassed={false}
onDuration={onDuration}
onlyWarnForMediaSeekingError
{...withoutTransparent}
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index 82dec0e37f..67d04d4518 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -60,7 +60,11 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
layout="none"
name={name}
>
- <Video {...propsOtherThanLoop} ref={ref} />
+ <Video
+ {...propsOtherThanLoop}
+ ref={ref}
+ _remotionInternalNativeLoopPassed
+ />
</Loop>
);
}
@@ -97,6 +101,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
{...otherProps}
ref={ref}
onDuration={onDuration}
+ nativeLoopPassed={props._remotionInternalNativeLoopPassed ?? false}
/>
);
};
diff --git a/packages/core/src/video/VideoForDevelopment.tsx b/packages/core/src/video/VideoForDevelopment.tsx
index da45c907e2..a4ab05d29a 100644
--- a/packages/core/src/video/VideoForDevelopment.tsx
+++ b/packages/core/src/video/VideoForDevelopment.tsx
@@ -27,6 +27,7 @@ import {isIosSafari, useAppendVideoFragment} from './video-fragment.js';
type VideoForDevelopmentProps = RemotionVideoProps & {
onlyWarnForMediaSeekingError: boolean;
onDuration: (src: string, durationInSeconds: number) => void;
+ nativeLoopPassed: boolean;
};
const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
@@ -51,6 +52,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
acceptableTimeShiftInSeconds,
toneFrequency,
name,
+ nativeLoopPassed,
...nativeProps
} = props;
if (typeof acceptableTimeShift !== 'undefined') {
@@ -194,6 +196,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
muted={muted || mediaMuted}
playsInline
src={actualSrc}
+ loop={nativeLoopPassed}
{...nativeProps}
/>
);
diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsx
index 6490d9a520..ad9fcd2454 100644
--- a/packages/core/src/video/VideoForRendering.tsx
+++ b/packages/core/src/video/VideoForRendering.tsx
@@ -43,6 +43,7 @@ const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<
onDuration,
toneFrequency,
name,
+ acceptableTimeShiftInSeconds,
...props
},
ref,
diff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.ts
index 5a4441e7b5..8ea4f4ece8 100644
--- a/packages/core/src/video/props.ts
+++ b/packages/core/src/video/props.ts
@@ -4,6 +4,10 @@ import type {VolumeProp} from '../volume-prop.js';
export type RemotionMainVideoProps = {
startFrom?: number;
endAt?: number;
+ /**
+ * @deprecated Only for internal `transparent` use
+ */
+ _remotionInternalNativeLoopPassed?: boolean;
};
export type RemotionVideoProps = Omit<
diff --git a/packages/example/src/AudioTesting/ShortLoop.tsx b/packages/example/src/AudioTesting/ShortLoop.tsx
new file mode 100644
index 0000000000..0472828426
--- /dev/null
+++ b/packages/example/src/AudioTesting/ShortLoop.tsx
@@ -0,0 +1,13 @@
+import {Audio} from 'remotion';
+
+const ShortAudioLoop: React.FC = () => {
+ return (
+ <Audio
+ loop
+ volume={0.1}
+ src="https://res.cloudinary.com/thatwas-pro/video/upload/v1700393642/test/8bit_j7ffha.wav"
+ />
+ );
+};
+
+export default ShortAudioLoop;
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 05a687f477..2dbb4eccd4 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -702,6 +702,14 @@ export const Index: React.FC = () => {
fps={30}
durationInFrames={300}
/>
+ <Composition
+ id="audio-testing-short-loop"
+ lazyComponent={() => import('./AudioTesting/ShortLoop')}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={300}
+ />
<Composition
id="audio-testing-amplify"
lazyComponent={() => import('./AudioTesting/Amplify')}