🎥 Make videos programmatically with React
volume, but doesn't work
Jonny Burgere77a0cef9434
diff --git a/packages/media/package.json b/packages/media/package.jsonindex b3731a95d4..6413ccd7d4 100644--- a/packages/media/package.json+++ b/packages/media/package.json@@ -16,6 +16,7 @@"formatting": "prettier --experimental-cli src --check","lint": "eslint src","watch": "tsc -w",+ "test": "vitest src/test --browser --run","make": "tsc -d && bun --env-file=../.env.bundle bundle.ts"},"dependencies": {diff --git a/packages/media/src/audio/audio-for-rendering.tsx b/packages/media/src/audio/audio-for-rendering.tsxindex 28323df668..800c0c6c94 100644--- a/packages/media/src/audio/audio-for-rendering.tsx+++ b/packages/media/src/audio/audio-for-rendering.tsx@@ -87,6 +87,7 @@ export const AudioForRendering: React.FC<AudioProps> = ({includeAudio: shouldRenderAudio,includeVideo: false,isClientSideRendering: environment.isClientSideRendering,+ volume,}).then(({audio}) => {if (audio) {@@ -128,6 +129,7 @@ export const AudioForRendering: React.FC<AudioProps> = ({shouldRenderAudio,src,unregisterRenderAsset,+ volume,]);return null;diff --git a/packages/media/src/convert-audiodata/convert-audiodata.ts b/packages/media/src/convert-audiodata/convert-audiodata.tsindex 935ca5f49e..55a0e35844 100644--- a/packages/media/src/convert-audiodata/convert-audiodata.ts+++ b/packages/media/src/convert-audiodata/convert-audiodata.ts@@ -6,6 +6,7 @@ export type ConvertAudioDataOptions = {trimStartInSeconds: number;trimEndInSeconds: number;targetNumberOfChannels: number;+ volume: number;};const FORMAT: AudioSampleFormat = 's16';@@ -24,6 +25,7 @@ export const convertAudioData = ({trimStartInSeconds,trimEndInSeconds,targetNumberOfChannels,+ volume,}: ConvertAudioDataOptions): PcmS16AudioData => {const {numberOfChannels: srcNumberOfChannels,@@ -63,7 +65,8 @@ export const convertAudioData = ({if (newNumberOfFrames === frameCount &&- targetNumberOfChannels === srcNumberOfChannels+ targetNumberOfChannels === srcNumberOfChannels &&+ volume === 1) {return {data: srcChannels,@@ -78,8 +81,9 @@ export const convertAudioData = ({srcNumberOfChannels,source: srcChannels,destination: data,- newNumberOfFrames,+ targetFrames: newNumberOfFrames,chunkSize,+ volume,});const newAudioData = {diff --git a/packages/media/src/convert-audiodata/resample-audiodata.ts b/packages/media/src/convert-audiodata/resample-audiodata.tsindex 656253f5a7..77fbffcfcf 100644--- a/packages/media/src/convert-audiodata/resample-audiodata.ts+++ b/packages/media/src/convert-audiodata/resample-audiodata.ts@@ -1,28 +1,29 @@// Remotion exports all videos with 2 channels.export const TARGET_NUMBER_OF_CHANNELS = 2;+// Remotion exports all videos with 48kHz sample rate.+export const TARGET_SAMPLE_RATE = 48000;+export const resampleAudioData = ({srcNumberOfChannels,source: srcChannels,destination,- newNumberOfFrames,+ targetFrames,chunkSize,+ volume,}: {srcNumberOfChannels: number;source: Int16Array;destination: Int16Array;- newNumberOfFrames: number;+ targetFrames: number;chunkSize: number;+ volume: number;}) => {- for (- let newFrameIndex = 0;- newFrameIndex < newNumberOfFrames;- newFrameIndex++- ) {+ for (let newFrameIndex = 0; newFrameIndex < targetFrames; newFrameIndex++) {const start = Math.floor(newFrameIndex * chunkSize);const end = Math.max(Math.floor(start + chunkSize), start + 1);- const sourceValues = new Array(srcNumberOfChannels).fill(0);+ const sourceValues: number[] = new Array(srcNumberOfChannels).fill(0);for (let channelIndex = 0;@@ -42,7 +43,7 @@ export const resampleAudioData = ({const average = itemSum / itemCount;- sourceValues[channelIndex] = average;+ sourceValues[channelIndex] = average * volume;}if (TARGET_NUMBER_OF_CHANNELS === srcNumberOfChannels) {@@ -57,11 +58,9 @@ export const resampleAudioData = ({// Mono to Stereo: M -> L, M -> Rif (srcNumberOfChannels === 1) {const m = sourceValues[0];- const l = m;- const r = m;- destination[newFrameIndex * 2 + 0] = l;- destination[newFrameIndex * 2 + 1] = r;+ destination[newFrameIndex * 2 + 0] = m;+ destination[newFrameIndex * 2 + 1] = m;}// Quad to Stereo: 0.5 * (L + SL), 0.5 * (R + SR)diff --git a/packages/media/src/extract-frame-via-broadcast-channel.ts b/packages/media/src/extract-frame-via-broadcast-channel.tsindex cbf421a0f7..19e1e53d12 100644--- a/packages/media/src/extract-frame-via-broadcast-channel.ts+++ b/packages/media/src/extract-frame-via-broadcast-channel.ts@@ -11,6 +11,7 @@ type ExtractFrameRequest = {logLevel: LogLevel;includeAudio: boolean;includeVideo: boolean;+ volume: number;};type ExtractFrameResponse =@@ -41,6 +42,7 @@ if (window.remotion_broadcastChannel && window.remotion_isMainTab) {durationInSeconds: data.durationInSeconds,includeAudio: data.includeAudio,includeVideo: data.includeVideo,+ volume: data.volume,});const videoFrame = frame;@@ -84,6 +86,7 @@ export const extractFrameViaBroadcastChannel = async ({includeAudio,includeVideo,isClientSideRendering,+ volume,}: {src: string;timeInSeconds: number;@@ -92,6 +95,7 @@ export const extractFrameViaBroadcastChannel = async ({includeAudio: boolean;includeVideo: boolean;isClientSideRendering: boolean;+ volume: number;}): Promise<{frame: ImageBitmap | VideoFrame | null;audio: PcmS16AudioData | null;@@ -104,6 +108,7 @@ export const extractFrameViaBroadcastChannel = async ({durationInSeconds,includeAudio,includeVideo,+ volume,});return {@@ -159,6 +164,7 @@ export const extractFrameViaBroadcastChannel = async ({durationInSeconds,includeAudio,includeVideo,+ volume,};window.remotion_broadcastChannel!.postMessage(request);diff --git a/packages/media/src/extract-frame.ts b/packages/media/src/extract-frame.tsindex c18fc07f92..b521b5ba63 100644--- a/packages/media/src/extract-frame.ts+++ b/packages/media/src/extract-frame.ts@@ -2,7 +2,10 @@ import type {AudioSample} from 'mediabunny';import {combineAudioDataAndClosePrevious} from './convert-audiodata/combine-audiodata';import type {PcmS16AudioData} from './convert-audiodata/convert-audiodata';import {convertAudioData} from './convert-audiodata/convert-audiodata';-import {TARGET_NUMBER_OF_CHANNELS} from './convert-audiodata/resample-audiodata';+import {+ TARGET_NUMBER_OF_CHANNELS,+ TARGET_SAMPLE_RATE,+} from './convert-audiodata/resample-audiodata';import {getSinks, type GetSink} from './get-frames-since-keyframe';import {makeKeyframeManager} from './keyframe-manager';import type {LogLevel} from './log';@@ -42,11 +45,12 @@ export const extractAudio = async ({src,timeInSeconds,durationInSeconds,+ volume,}: {src: string;timeInSeconds: number;- logLevel: LogLevel;durationInSeconds: number;+ volume: number;}): Promise<PcmS16AudioData | null> => {if (!sinkPromise[src]) {sinkPromise[src] = getSinks(src);@@ -150,10 +154,11 @@ export const extractAudio = async ({const audioData = convertAudioData({audioData: audioDataRaw,- newSampleRate: 48000,+ newSampleRate: TARGET_SAMPLE_RATE,trimStartInSeconds,trimEndInSeconds,targetNumberOfChannels: TARGET_NUMBER_OF_CHANNELS,+ volume,});audioDataRaw.close();@@ -184,6 +189,7 @@ export const extractFrameAndAudio = async ({durationInSeconds,includeAudio,includeVideo,+ volume,}: {src: string;timeInSeconds: number;@@ -191,6 +197,7 @@ export const extractFrameAndAudio = async ({durationInSeconds: number;includeAudio: boolean;includeVideo: boolean;+ volume: number;}): Promise<{frame: VideoFrame | null;audio: PcmS16AudioData | null;@@ -207,8 +214,8 @@ export const extractFrameAndAudio = async ({? extractAudio({src,timeInSeconds,- logLevel,durationInSeconds,+ volume,}): null,]);diff --git a/packages/media/src/test/browser.test.ts b/packages/media/src/test/browser.test.tsindex dd0c49c861..dd7df5681c 100644--- a/packages/media/src/test/browser.test.ts+++ b/packages/media/src/test/browser.test.ts@@ -11,6 +11,7 @@ test('Should be able to extract a frame', async () => {logLevel: 'info',includeAudio: true,includeVideo: true,+ volume: 1,});assert(frame);@@ -37,9 +38,10 @@ test('Should manage the cache', async () => {src: `/bigbuckbunny.mp4?i=${i}`,timeInSeconds: 1,durationInSeconds: 1 / 30,- logLevel: 'verbose',+ logLevel: 'info',includeAudio: true,includeVideo: true,+ volume: 1,});}@@ -47,3 +49,41 @@ test('Should manage the cache', async () => {expect(cacheStats.count).toBe(725);expect(cacheStats.totalSize).toBe(1002240000);});++test.only('Should be able to extract a frame', async () => {+ await keyframeManager.clearAll();++ const {audio: audioAtHalfVolume} = await extractFrameAndAudio({+ src: '/bigbuckbunny.mp4',+ timeInSeconds: 1,+ durationInSeconds: 1 / 30,+ logLevel: 'info',+ includeAudio: true,+ includeVideo: false,+ volume: 0.5,+ });++ const {audio: audioAtFullVolume, frame} = await extractFrameAndAudio({+ src: '/bigbuckbunny.mp4',+ timeInSeconds: 1,+ durationInSeconds: 1 / 30,+ logLevel: 'info',+ includeAudio: true,+ includeVideo: false,+ volume: 1,+ });++ assert(!frame);++ const totalAudioAtFullVolume = audioAtFullVolume?.data.reduce(+ (acc, curr) => acc + curr * 0.5,+ 0,+ );+ assert(audioAtHalfVolume);+ const totalAudioAtHalfVolume = audioAtHalfVolume?.data.reduce(+ (acc, curr) => acc + curr,+ 0,+ );+ assert(totalAudioAtFullVolume);+ expect(totalAudioAtHalfVolume).toBe(totalAudioAtFullVolume);+});diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsxindex f58592c206..54c3f6f48e 100644--- a/packages/media/src/video/video-for-rendering.tsx+++ b/packages/media/src/video/video-for-rendering.tsx@@ -99,6 +99,7 @@ export const VideoForRendering: React.FC<VideoProps> = ({includeAudio: shouldRenderAudio,includeVideo: true,isClientSideRendering: environment.isClientSideRendering,+ volume,}).then(({frame: imageBitmap, audio}) => {if (!imageBitmap) {@@ -149,6 +150,7 @@ export const VideoForRendering: React.FC<VideoProps> = ({shouldRenderAudio,src,unregisterRenderAsset,+ volume,]);return (