🎥 Make videos programmatically with React
`@remotion/web-renderer`: Fix audio positioning
Jonny Burger2a331b6d2fb4
diff --git a/packages/template-recorder/remotion/audio/AudioTrack.tsx b/packages/template-recorder/remotion/audio/AudioTrack.tsxindex 10312df3fe..a27d5620f0 100644--- a/packages/template-recorder/remotion/audio/AudioTrack.tsx+++ b/packages/template-recorder/remotion/audio/AudioTrack.tsx@@ -1,5 +1,5 @@import React, { useMemo } from "react";-import { Html5Audio, interpolate, Sequence, useVideoConfig } from "remotion";+import { interpolate, Sequence, useVideoConfig } from "remotion";import type { CanvasLayout } from "../../config/layout";import type { SceneAndMetadata } from "../../config/scenes";import {@@ -10,6 +10,7 @@ import {} from "../../config/sounds";import { SCENE_TRANSITION_DURATION } from "../../config/transitions";import { getShouldTransitionOut } from "../animations/transitions";+import { Audio } from "@remotion/media";type LoudPart = [number, number];@@ -83,9 +84,8 @@ const AudioClip: React.FC<{}, [durationInFrames, loudParts]);return (- <Html5Audio+ <Audio// Lint false positive: https://github.com/remotion-dev/remotion/issues/2706-volume={volumeFunction}loopVolumeCurveBehavior="extend"src={src}diff --git a/packages/template-recorder/remotion/scenes/VideoScene/SoundEffects.tsx b/packages/template-recorder/remotion/scenes/VideoScene/SoundEffects.tsxindex 8940f21883..8797223f64 100644--- a/packages/template-recorder/remotion/scenes/VideoScene/SoundEffects.tsx+++ b/packages/template-recorder/remotion/scenes/VideoScene/SoundEffects.tsx@@ -1,9 +1,10 @@-import { Html5Audio, staticFile } from "remotion";+import { staticFile } from "remotion";import type { SceneAndMetadata } from "../../../config/scenes";import {isGrowingFromMiniature,isShrinkingToMiniature,} from "../../animations/webcam-transitions/helpers";+import { Audio } from "@remotion/media";export const SoundEffects: React.FC<{previousScene: SceneAndMetadata | null;@@ -19,7 +20,7 @@ export const SoundEffects: React.FC<{secondScene: sceneAndMetadata,})) {- return <Html5Audio src={staticFile("sounds/shrink.m4a")} volume={0.2} />;+ return <Audio src={staticFile("sounds/shrink.m4a")} volume={0.2} />;}if (@@ -28,8 +29,8 @@ export const SoundEffects: React.FC<{secondScene: sceneAndMetadata,})) {- return <Html5Audio src={staticFile("sounds/grow.m4a")} volume={0.2} />;+ return <Audio src={staticFile("sounds/grow.m4a")} volume={0.2} />;}- return <Html5Audio src={staticFile("sounds/whip.wav")} volume={0.1} />;+ return <Audio src={staticFile("sounds/whip.wav")} volume={0.1} />;};diff --git a/packages/web-renderer/src/audio.ts b/packages/web-renderer/src/audio.tsindex 58dceec833..5ebd72f064 100644--- a/packages/web-renderer/src/audio.ts+++ b/packages/web-renderer/src/audio.ts@@ -11,7 +11,9 @@ function mixAudio(waves: Int16Array[], length: number) {const mixed = new Int16Array(length);for (let i = 0; i < length; i++) {- const sum = waves.reduce((acc, wave) => acc + wave[i], 0);+ const sum = waves.reduce((acc, wave) => {+ return acc + (wave[i] ?? 0);+ }, 0);// Clamp to Int16 rangemixed[i] = Math.max(-32768, Math.min(32767, sum));}@@ -19,44 +21,39 @@ function mixAudio(waves: Int16Array[], length: number) {return mixed;}-export const onlyInlineAudio = (assets: TRenderAsset[]): AudioData | null => {+export const onlyInlineAudio = ({+ assets,+ fps,+ frame,+}: {+ assets: TRenderAsset[];+ fps: number;+ frame: number;+}): AudioData | null => {const inlineAudio = assets.filter((asset) => asset.type === 'inline-audio');+ const expectedLength = Math.round(+ (TARGET_NUMBER_OF_CHANNELS * TARGET_SAMPLE_RATE) / fps,+ );- let length: number | null = null;for (const asset of inlineAudio) {if (asset.toneFrequency !== 1) {throw new Error('Setting the toneFrequency is not supported yet in web rendering.',);}-- if (length === null) {- length = asset.audio.length;- // 1 frame offset may happen due to rounding, it is safe to truncate the last sample- } else if (- Math.abs(length - asset.audio.length) > TARGET_NUMBER_OF_CHANNELS- ) {- throw new Error('All inline audio must have the same length');- } else {- length = Math.min(length, asset.audio.length);- }- }-- if (length === null) {- return null;}const mixedAudio = mixAudio(inlineAudio.map((asset) => asset.audio as Int16Array),- length,+ expectedLength,) as Int16Array<ArrayBuffer>;return new AudioData({data: mixedAudio,format: 's16',numberOfChannels: TARGET_NUMBER_OF_CHANNELS,- numberOfFrames: length / TARGET_NUMBER_OF_CHANNELS,+ numberOfFrames: expectedLength / TARGET_NUMBER_OF_CHANNELS,sampleRate: TARGET_SAMPLE_RATE,- timestamp: inlineAudio[0].timestamp,+ timestamp: (frame / fps) * 1_000_000,});};diff --git a/packages/web-renderer/src/render-media-on-web.tsx b/packages/web-renderer/src/render-media-on-web.tsxindex d07c790024..a16ece9f0d 100644--- a/packages/web-renderer/src/render-media-on-web.tsx+++ b/packages/web-renderer/src/render-media-on-web.tsx@@ -274,8 +274,8 @@ const internalRenderMediaOnWeb = async <const throttledOnProgress = createThrottledProgressCallback(onProgress);- for (let i = realFrameRange[0]; i <= realFrameRange[1]; i++) {- timeUpdater.current?.update(i);+ for (let frame = realFrameRange[0]; frame <= realFrameRange[1]; frame++) {+ timeUpdater.current?.update(frame);await waitForReady({timeoutInMilliseconds: delayRenderTimeoutInMilliseconds,scope: delayRenderScope,@@ -297,20 +297,20 @@ const internalRenderMediaOnWeb = async <if (onArtifact) {await artifactsHandler.handle({imageData,- frame: i,+ frame,assets,onArtifact,});}- const audio = onlyInlineAudio(assets);+ const audio = onlyInlineAudio({assets, fps: resolved.fps, frame});if (signal?.aborted) {throw new Error('renderMediaOnWeb() was cancelled');}const timestamp = Math.round(- ((i - realFrameRange[0]) / resolved.fps) * 1_000_000,+ ((frame - realFrameRange[0]) / resolved.fps) * 1_000_000,);const videoFrame = new VideoFrame(imageData, {timestamp,