🎥 Make videos programmatically with React
`@remotion/media`: Preserve silent tails when looping videos (#10657)
Jonny Burger3b5a69617471
diff --git a/packages/example/src/AudioSmoothness/ShortAudioLoop.tsx b/packages/example/src/AudioSmoothness/ShortAudioLoop.tsxnew file mode 100644index 0000000000..328c53d780--- /dev/null+++ b/packages/example/src/AudioSmoothness/ShortAudioLoop.tsx@@ -0,0 +1,26 @@+import {Video} from '@remotion/media';+import {Composition, Sequence} from 'remotion';++const fps = 30;+const src = 'https://remotion.media/audio-shorter-than-video.mp4';++const ShortAudioLoop = () => {+ return (+ <Sequence from={-8 * fps} layout="none">+ <Video src={src} loop debugOverlay logLevel="verbose" />+ </Sequence>+ );+};++export const AudioSmoothnessShortAudioLoopComp = () => {+ return (+ <Composition+ id="audio-smoothness-short-audio-loop"+ component={ShortAudioLoop}+ width={320}+ height={180}+ fps={fps}+ durationInFrames={12 * fps}+ />+ );+};diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsxindex db0d8dc4c8..6b4603dde0 100644--- a/packages/example/src/Root.tsx+++ b/packages/example/src/Root.tsx@@ -208,6 +208,7 @@ import {AnimatedImageEffects} from './AnimatedImage/Effects';import {AudioSmoothnessBufferInterruptionComp} from './AudioSmoothness/BufferInterruption';import {AudioSmoothnessLoopedAudioComp} from './AudioSmoothness/LoopedAudio';import {AudioSmoothnessNewVideoComp} from './AudioSmoothness/NewVideo';+import {AudioSmoothnessShortAudioLoopComp} from './AudioSmoothness/ShortAudioLoop';import {AudioSmoothnessSlicedVideoComp} from './AudioSmoothness/SlicedVideo';import {AudioSmoothnessTrimAfterLoopComp} from './AudioSmoothness/TrimAfterLoop';import {AudioSmoothnessTrimButtonComp} from './AudioSmoothness/TrimButton';@@ -2233,6 +2234,7 @@ export const Index: React.FC = () => {<AudioSmoothnessTrimButtonComp /><AudioSmoothnessTrimAfterLoopComp /><AudioSmoothnessLoopedAudioComp />+ <AudioSmoothnessShortAudioLoopComp /></Folder><Folder name="Postmount"><Compositiondiff --git a/packages/media/src/make-iterator-with-priming.ts b/packages/media/src/make-iterator-with-priming.tsindex d51d4ffae1..b54df4a569 100644--- a/packages/media/src/make-iterator-with-priming.ts+++ b/packages/media/src/make-iterator-with-priming.ts@@ -108,10 +108,8 @@ export async function* makeLoopingIterator({}}- if (!yieldedInPass) {- throw new Error(- `Audio loop pass from ${passStartInSeconds} to ${segmentEndInSeconds} yielded no buffers`,- );+ if (!yieldedInPass && passStartInSeconds === loopStartInSeconds) {+ return;}passBaseTimestamp += segmentEndInSeconds - passStartInSeconds;diff --git a/packages/media/src/test/looping-audio-timestamps.test.ts b/packages/media/src/test/looping-audio-timestamps.test.tsindex 0015d24abd..6ebc60a727 100644--- a/packages/media/src/test/looping-audio-timestamps.test.ts+++ b/packages/media/src/test/looping-audio-timestamps.test.ts@@ -96,6 +96,38 @@ test('seeking into the middle of a loop must replay the full segment on later it);});+// https://github.com/remotion-dev/remotion/issues/10655+test('seeking into a silent audio tail must preserve the silence before looping', async () => {+ const iterator = makeLoopingIterator({+ audioSink: makeMockAudioSink(7),+ seekTimeInSeconds: 8,+ segmentEndInSeconds: 10,+ loopStartInSeconds: 0,+ maximumContinuousTimestamp: 22,+ });++ const chunks = await collect(iterator, 1000);++ expect(chunks.map((c) => c.rawTimestamp)).toEqual([+ 0, 1, 2, 3, 4, 5, 6, 0, 1,+ ]);+ expect(chunks.map((c) => c.timestamp)).toEqual([+ 10, 11, 12, 13, 14, 15, 16, 20, 21,+ ]);+});++test('a full loop pass without audio buffers must stop the iterator', async () => {+ const iterator = makeLoopingIterator({+ audioSink: makeMockAudioSink(0),+ seekTimeInSeconds: 8,+ segmentEndInSeconds: 10,+ loopStartInSeconds: 0,+ maximumContinuousTimestamp: 100,+ });++ expect(await collect(iterator, 1000)).toEqual([]);+});+test('looped audio with trimBefore must replay the trimmed segment', async () => {const iterator = makeLoopingIterator({audioSink: makeMockAudioSink(10),diff --git a/packages/remotion-media/audio-shorter-than-video.mp4 b/packages/remotion-media/audio-shorter-than-video.mp4new file mode 100644index 0000000000..7536a53fd9Binary files /dev/null and b/packages/remotion-media/audio-shorter-than-video.mp4 differdiff --git a/packages/remotion-media/generate.ts b/packages/remotion-media/generate.tsindex 223a9d80d4..6a01dfe0c4 100644--- a/packages/remotion-media/generate.ts+++ b/packages/remotion-media/generate.ts@@ -606,6 +606,20 @@ variants.push({category: 'edge-cases',});+const shortAudioFileName = 'audio-shorter-than-video.mp4';+copyFileSync(shortAudioFileName, path.join(outDir, shortAudioFileName));+const shortAudioStat = await Bun.file(+ path.join(outDir, shortAudioFileName),+).stat();+variants.push({+ videoCodec: 'h264',+ audioCodec: 'aac',+ container: 'mp4',+ fileNames: [shortAudioFileName],+ size: shortAudioStat.size,+ category: 'edge-cases',+});+// Sound effects (pre-existing files, CC0 licensed)const soundEffects = [{diff --git a/packages/remotion-media/variants.json b/packages/remotion-media/variants.jsonindex e700043833..6d7c8925f7 100644--- a/packages/remotion-media/variants.json+++ b/packages/remotion-media/variants.json@@ -782,6 +782,14 @@"size": 911894,"category": "edge-cases"},+ {+ "videoCodec": "h264",+ "audioCodec": "aac",+ "container": "mp4",+ "fileNames": ["audio-shorter-than-video.mp4"],+ "size": 318935,+ "category": "edge-cases"+ },{"videoCodec": "none","audioCodec": "pcm",