🎥 Make videos programmatically with React
`@remotion/media`: Pre-schedule audio nodes before premounting ends
Jonny Burgerefe6e605db73
diff --git a/packages/core/src/PremountContext.tsx b/packages/core/src/PremountContext.tsxnew file mode 100644index 0000000000..2e45d63513--- /dev/null+++ b/packages/core/src/PremountContext.tsx@@ -0,0 +1,17 @@+import {createContext} from 'react';++export type PremountContextValue = {+ premountFramesRemaining: number;+ playing: boolean;+};++/**+ * @internal+ * Provides premounting state to internal components.+ * - premountFramesRemaining: accumulated frames remaining until premounting ends (0 if not premounting).+ * - playing: whether the player is currently playing. Sticky true: if any parent is playing, this is true.+ */+export const PremountContext = createContext<PremountContextValue>({+ premountFramesRemaining: 0,+ playing: false,+});diff --git a/packages/core/src/Sequence.tsx b/packages/core/src/Sequence.tsxindex f55b1ab152..a5dbc06206 100644--- a/packages/core/src/Sequence.tsx+++ b/packages/core/src/Sequence.tsx@@ -9,6 +9,7 @@ import React, {import {AbsoluteFill} from './AbsoluteFill.js';import type {LoopDisplay, SequenceControls} from './CompositionManager.js';import {Freeze} from './freeze.js';+import {PremountContext} from './PremountContext.js';import {useNonce} from './nonce.js';import type {SequenceContextType} from './SequenceContext.js';import {SequenceContext} from './SequenceContext.js';@@ -361,20 +362,35 @@ const PremountedPostmountedSequenceRefForwardingFunction: React.ForwardRefRenderstyleWhilePostmounted,]);+ const parentPremountContext = useContext(PremountContext);+ const {playing} = useContext(TimelineContext);+ const premountFramesRemaining =+ parentPremountContext.premountFramesRemaining ++ (premountingActive ? from - frame : 0);++ const premountContextValue = useMemo(() => {+ return {+ premountFramesRemaining,+ playing: parentPremountContext.playing || playing,+ };+ }, [premountFramesRemaining, parentPremountContext.playing, playing]);+return (- <Freeze frame={freezeFrame} active={isFreezingActive}>- <Sequence- ref={ref}- from={from}- durationInFrames={durationInFrames}- style={style}- _remotionInternalPremountDisplay={premountFor}- _remotionInternalPostmountDisplay={postmountFor}- _remotionInternalIsPremounting={premountingActive}- _remotionInternalIsPostmounting={postmountingActive}- {...otherProps}- />- </Freeze>+ <PremountContext.Provider value={premountContextValue}>+ <Freeze frame={freezeFrame} active={isFreezingActive}>+ <Sequence+ ref={ref}+ from={from}+ durationInFrames={durationInFrames}+ style={style}+ _remotionInternalPremountDisplay={premountFor}+ _remotionInternalPostmountDisplay={postmountFor}+ _remotionInternalIsPremounting={premountingActive}+ _remotionInternalIsPostmounting={postmountingActive}+ {...otherProps}+ />+ </Freeze>+ </PremountContext.Provider>);};diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex 8f9a24739e..0a8608972b 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -87,6 +87,7 @@ import type {SequenceFieldSchema,SequenceSchema,} from './sequence-field-schema.js';+import {PremountContext} from './PremountContext.js';import {SequenceContext} from './SequenceContext.js';import {SequenceControlOverrideContext,@@ -193,6 +194,7 @@ export const Internals = {useLazyComponent,truthy,SequenceContext,+ PremountContext,useRemotionContexts,RemotionContextProvider,CSSUtils,diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.tsindex 78aa19ce43..c30de3e36c 100644--- a/packages/media/src/media-player.ts+++ b/packages/media/src/media-player.ts@@ -410,11 +410,7 @@ export class MediaPlayer {]);}- public async play(time: number): Promise<void> {- if (this.playing) {- return;- }-+ public async playAudio(time: number): Promise<void> {const newTime = getTimeInSeconds({unloopedTimeInSeconds: time,playbackRate: this.playbackRate,@@ -434,7 +430,6 @@ export class MediaPlayer {newTime,this.playbackRate * this.globalPlaybackRate,);- this.playing = true;if (this.audioIteratorManager) {this.audioIteratorManager.resumeScheduledAudioChunks({@@ -449,7 +444,16 @@ export class MediaPlayer {) {await this.sharedAudioContext.resume();}+ }++ public async play(time: number): Promise<void> {+ if (this.playing) {+ return;+ }++ this.playing = true;+ await this.playAudio(time);this.drawDebugOverlay();}diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsxindex 9ead754712..c802003f4c 100644--- a/packages/media/src/video/video-for-preview.tsx+++ b/packages/media/src/video/video-for-preview.tsx@@ -136,6 +136,15 @@ const VideoForPreviewAssertedShowing: React.FC<const parentSequence = useContext(SequenceContext);const isPremounting = Boolean(parentSequence?.premounting);const isPostmounting = Boolean(parentSequence?.postmounting);+ const {premountFramesRemaining, playing: playingWhilePremounting} =+ useContext(Internals.PremountContext);++ // Allows for pre-scheduling audio nodes before the premounting ends,+ // since there is some latency.+ const isNextFrameGoingToPlay =+ playingWhilePremounting &&+ premountFramesRemaining > 0 &&+ premountFramesRemaining <= 1.000000001;const loopDisplay = useLoopDisplay({loop,@@ -357,12 +366,42 @@ const VideoForPreviewAssertedShowing: React.FC<const mediaPlayer = mediaPlayerRef.current;if (!mediaPlayer) return;- if (playing && !isPlayerBuffering) {+ if (isNextFrameGoingToPlay) {+ const currentTimeUntilZero =+ // Premounting does not consider the local playback rate, just the global one.+ premountFramesRemaining / videoConfig.fps / globalPlaybackRate;+ mediaPlayer.playAudio(currentTimeRef.current - currentTimeUntilZero);+ }+ }, [+ isNextFrameGoingToPlay,+ premountFramesRemaining,+ videoConfig.fps,+ globalPlaybackRate,+ playbackRate,+ ]);++ useEffect(() => {+ const mediaPlayer = mediaPlayerRef.current;+ if (!mediaPlayer) return;++ if (playing && !isPlayerBuffering && !isNextFrameGoingToPlay) {+ // Play does nothing if already playing, so it can be called multiple times.mediaPlayer.play(currentTimeRef.current);} else {+ // Pause will do the work all over again and check if there are scheduled nodes.+ // This is why isNextFrameGoingToPlay is the in the depeendency array.+ // We want to trigger another pause if due to being 1 frame before premounting ends,+ // audio is resumed and at the same time a pause is happening, we need to ensure+ // that the pause is triggered again even though officially "playing" never changed.mediaPlayer.pause();}- }, [isPlayerBuffering, playing, logLevel, mediaPlayerReady]);+ }, [+ isPlayerBuffering,+ playing,+ logLevel,+ mediaPlayerReady,+ isNextFrameGoingToPlay,+ ]);useEffect(() => {const mediaPlayer = mediaPlayerRef.current;