🎥 Make videos programmatically with React
primitive implementation
Jonny Burgerd37820fb5eda
diff --git a/packages/core/src/CompositionManager.tsx b/packages/core/src/CompositionManager.tsxindex d555621505..f32c6a4f99 100644--- a/packages/core/src/CompositionManager.tsx+++ b/packages/core/src/CompositionManager.tsx@@ -118,7 +118,7 @@ export type AudioOrVideoAsset = {export type InlineAudioAsset = {type: 'inline-audio';id: string;- audio: number[];+ audio: Int16Array | number[];frame: number;timestamp: number;duration: number;diff --git a/packages/example/src/OffthreadRemoteVideo/NewRemoteVideo.tsx b/packages/example/src/OffthreadRemoteVideo/NewRemoteVideo.tsxindex ccebb52704..b25673045a 100644--- a/packages/example/src/OffthreadRemoteVideo/NewRemoteVideo.tsx+++ b/packages/example/src/OffthreadRemoteVideo/NewRemoteVideo.tsx@@ -1,4 +1,4 @@-import {Video} from '@remotion/media';+import {Audio, Video} from '@remotion/media';import {CalculateMetadataFunction, Composition} from 'remotion';// https://www.remotion.dev/docs/mediabunny/metadataimport {getMediaMetadata} from '../get-media-metadata';@@ -19,7 +19,12 @@ export const calculateMetadataFn: CalculateMetadataFunction<};export const Component = () => {- return <Video src={src} />;+ return (+ <>+ <Video src={src} />+ <Audio src={'https://remotion.media/dialogue.wav'} />+ </>+ );};export const NewVideoComp = () => {diff --git a/packages/media/src/audio/audio-for-rendering.tsx b/packages/media/src/audio/audio-for-rendering.tsxindex e1da4b3171..6b863e3039 100644--- a/packages/media/src/audio/audio-for-rendering.tsx+++ b/packages/media/src/audio/audio-for-rendering.tsx@@ -214,7 +214,9 @@ export const AudioForRendering: React.FC<AudioProps> = ({registerRenderAsset({type: 'inline-audio',id,- audio: Array.from(audio.data),+ audio: environment.isClientSideRendering+ ? audio.data+ : Array.from(audio.data),frame: absoluteFrame,timestamp: audio.timestamp,duration: (audio.numberOfFrames / TARGET_SAMPLE_RATE) * 1_000_000,diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsxindex a323b3515a..213508a85f 100644--- a/packages/media/src/video/video-for-rendering.tsx+++ b/packages/media/src/video/video-for-rendering.tsx@@ -322,7 +322,9 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({registerRenderAsset({type: 'inline-audio',id,- audio: Array.from(audio.data),+ audio: environment.isClientSideRendering+ ? audio.data+ : Array.from(audio.data),frame: absoluteFrame,timestamp: audio.timestamp,duration: (audio.numberOfFrames / TARGET_SAMPLE_RATE) * 1_000_000,@@ -371,6 +373,7 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({videoEnabled,maxCacheSize,cancelRender,+ environment,]);const classNameValue = useMemo(() => {diff --git a/packages/web-renderer/src/audio.ts b/packages/web-renderer/src/audio.tsindex 28076f5537..7ece23515a 100644--- a/packages/web-renderer/src/audio.ts+++ b/packages/web-renderer/src/audio.ts@@ -1,6 +1,27 @@import type {TRenderAsset} from 'remotion';-export const onlyInlineAudio = (assets: TRenderAsset[]) => {+const TARGET_NUMBER_OF_CHANNELS = 2;+const TARGET_SAMPLE_RATE = 48000;++function mixAudio(waves: Int16Array[]) {+ if (waves.length === 1) {+ return waves[0];+ }++ const {length} = waves[0];++ const mixed = new Int16Array(length);++ for (let i = 0; i < length; i++) {+ const sum = waves.reduce((acc, wave) => acc + wave[i], 0);+ // Clamp to Int16 range+ mixed[i] = Math.max(-32768, Math.min(32767, sum));+ }++ return mixed;+}++export const onlyInlineAudio = (assets: TRenderAsset[]): AudioData | null => {const inlineAudio = assets.filter((asset) => asset.type === 'inline-audio');let duration: number | null = null;@@ -26,6 +47,19 @@ export const onlyInlineAudio = (assets: TRenderAsset[]) => {}if (duration === null || length === null) {- throw new Error('All inline audio must have a duration and length');+ return null;}++ const mixedAudio = mixAudio(+ inlineAudio.map((asset) => asset.audio as Int16Array),+ ) as Int16Array<ArrayBuffer>;++ return new AudioData({+ data: mixedAudio,+ format: 's16',+ numberOfChannels: TARGET_NUMBER_OF_CHANNELS,+ numberOfFrames: inlineAudio[0].audio.length / TARGET_NUMBER_OF_CHANNELS,+ sampleRate: TARGET_SAMPLE_RATE,+ timestamp: inlineAudio[0].timestamp,+ });};diff --git a/packages/web-renderer/src/render-media-on-web.tsx b/packages/web-renderer/src/render-media-on-web.tsxindex bc6c1e9433..f80a2e9b09 100644--- a/packages/web-renderer/src/render-media-on-web.tsx+++ b/packages/web-renderer/src/render-media-on-web.tsx@@ -1,4 +1,12 @@-import {BufferTarget, Output, VideoSample, VideoSampleSource} from 'mediabunny';+import {+ AudioSample,+ AudioSampleSource,+ BufferTarget,+ Output,+ QUALITY_MEDIUM,+ VideoSample,+ VideoSampleSource,+} from 'mediabunny';import type {CalculateMetadataFunction} from 'remotion';import {Internals, type LogLevel} from 'remotion';import type {AnyZodObject, z} from 'zod';@@ -237,6 +245,17 @@ const internalRenderMediaOnWeb = async <output.addVideoTrack(videoSampleSource);+ const audioSampleSource = new AudioSampleSource({+ // TODO: Hardcoded+ codec: 'aac',+ bitrate: QUALITY_MEDIUM,+ });+ cleanupFns.push(() => {+ audioSampleSource.close();+ });++ output.addAudioTrack(audioSampleSource);+await output.start();if (signal?.aborted) {@@ -279,7 +298,8 @@ const internalRenderMediaOnWeb = async <});}- onlyInlineAudio(assets);+ // TODO: What if there is no audio? Need to return a nulled audio sample.+ const audio = onlyInlineAudio(assets);if (signal?.aborted) {throw new Error('renderMediaOnWeb() was cancelled');@@ -307,7 +327,14 @@ const internalRenderMediaOnWeb = async <});}+ // TODO: Parallelize?await videoSampleSource.add(new VideoSample(frameToEncode));+ if (audio) {+ const audioSample = new AudioSample(audio);+ await audioSampleSource.add(audioSample);+ audioSample.close();+ }+progress.encodedFrames++;throttledOnProgress?.({...progress});@@ -322,6 +349,7 @@ const internalRenderMediaOnWeb = async <onProgress?.({...progress});videoSampleSource.close();+ audioSampleSource.close();await output.finalize();return output.target.buffer as ArrayBuffer;diff --git a/packages/web-renderer/src/test/audio.test.tsx b/packages/web-renderer/src/test/audio.test.tsxindex 5d4474f1c1..dd527fda23 100644--- a/packages/web-renderer/src/test/audio.test.tsx+++ b/packages/web-renderer/src/test/audio.test.tsx@@ -3,7 +3,7 @@ import {staticFile} from 'remotion';import {expect, test} from 'vitest';import {renderMediaOnWeb} from '../render-media-on-web';-test.only('should not be able to set toneFrequency on web rendering', async () => {+test('should not be able to set toneFrequency on web rendering', async () => {const Component: React.FC = () => {return <Audio src={staticFile('dialogue.wav')} toneFrequency={0.5} />;};@@ -25,7 +25,7 @@ test.only('should not be able to set toneFrequency on web rendering', async () =);});-test.only('should be able to render 2 audios', async () => {+test('should be able to render 2 audios', async () => {const Component: React.FC = () => {return (<>