Skip to content

🎥 Make videos programmatically with React

awesome

Jonny Burgera14139d855ed
diff --git a/packages/docs/docs/webcodecs/convert-media.mdx b/packages/docs/docs/webcodecs/convert-media.mdx
index ebb6390a54..7ab34d46ff 100644
--- a/packages/docs/docs/webcodecs/convert-media.mdx
+++ b/packages/docs/docs/webcodecs/convert-media.mdx
@@ -105,21 +105,9 @@ _Function_ <TsType type="ConvertMediaOnProgress" source="@remotion/media-parser"
Allows receiving progress updates. The following fields are available:
```tsx twoslash
-import type {
- ConvertMediaOnProgress,
- ConvertMediaProgress,
-} from '@remotion/webcodecs';
-
-export const onProgress: ConvertMediaOnProgress = ({
- decodedVideoFrames,
- decodedAudioFrames,
- encodedVideoFrames,
- encodedAudioFrames,
- bytesWritten,
- millisecondsWritten,
- expectedOutputDurationInMs,
- overallProgress,
-}: ConvertMediaProgress) => {
+import type {ConvertMediaOnProgress, ConvertMediaProgress} from '@remotion/webcodecs';
+
+export const onProgress: ConvertMediaOnProgress = ({decodedVideoFrames, decodedAudioFrames, encodedVideoFrames, encodedAudioFrames, bytesWritten, millisecondsWritten, expectedOutputDurationInMs, overallProgress}: ConvertMediaProgress) => {
console.log(decodedVideoFrames);
// ^?
console.log(decodedAudioFrames);
@@ -168,11 +156,43 @@ You may return the same frame or replace it with a new `VideoFrame` object.
After the function returns, `convertMedia()` will call [`.close()`](https://developer.mozilla.org/en-US/docs/Web/API/VideoFrame/close) on the input and output frames.
This will destroy the frame and free up memory.
-
If you need a reference to the frame that lasts longer than the lifetime of this function, you must call [`.clone()`](https://developer.mozilla.org/en-US/docs/Web/API/VideoFrame/clone) on it.
If you return a different frame than the one you received, it must have the same values for `codedWidth`, `codedHeight`, `displayWidth` and `displayHeight`, `timestamp` and `duration` as the input frame.
+### `onAudioData?`
+
+_Function_ <TsType type="ConvertMediaOnAudioData" source="@remotion/media-parser"/>
+
+Allows you to hook into the audio data. The items are native [`AudioData`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData) objects.
+
+```tsx twoslash
+import type {ConvertMediaOnAudioData} from '@remotion/webcodecs';
+
+export const onAudioData: ConvertMediaOnAudioData = ({audioData}) => {
+ console.log(audioData);
+ // ^?
+
+ // Do something with the audiodata, for example:
+ // - Change the pitch
+ // - Lower the volume
+
+ // Then return the frame to be used for encoding.
+ return audioData;
+};
+```
+
+The callback function may be async.
+
+When the function returns, the returned audio data is used for audio encoding.
+You may return the same audio data or replace it with a new [`AudioData`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData) object.
+
+After the function returns, `convertMedia()` will call [`.close()`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData/close) on the input and output audio data.
+This will destroy the audio data and free up memory.
+If you need a reference to the audio data that lasts longer than the lifetime of this function, you must call [`.clone()`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData/clone) on it.
+
+If you return a different audio data than the one you received, it should have the same [`duration`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData/duration), [`numberOfChannels`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData/numberOfChannels), [`sampleRate`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData/sampleRate), [`timestamp`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData/format), [`numberOfChannels`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData/timestamp), [`format`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData/format) and [`numberOfChannels`](https://developer.mozilla.org/en-US/docs/Web/API/AudioData/numberOfChannels) as the input audio data.
+
### `signal?`
An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the conversion process whenever the signal is aborted.
diff --git a/packages/webcodecs/src/index.ts b/packages/webcodecs/src/index.ts
index 0ff6f2bbaf..268406079b 100644
--- a/packages/webcodecs/src/index.ts
+++ b/packages/webcodecs/src/index.ts
@@ -13,6 +13,7 @@ export {canReencodeAudioTrack} from './can-reencode-audio-track';
export {canReencodeVideoTrack} from './can-reencode-video-track';
export {
convertMedia,
+ ConvertMediaOnAudioData,
ConvertMediaOnProgress,
ConvertMediaOnVideoFrame,
ConvertMediaProgress,
diff --git a/packages/webcodecs/src/on-audio-track.ts b/packages/webcodecs/src/on-audio-track.ts
index 3bce384ad6..d503d43240 100644
--- a/packages/webcodecs/src/on-audio-track.ts
+++ b/packages/webcodecs/src/on-audio-track.ts
@@ -199,6 +199,35 @@ export const makeAudioTrackHandler =
? await onAudioData?.({audioData, track})
: audioData;
if (newAudioData !== audioData) {
+ if (newAudioData.duration !== audioData.duration) {
+ throw new Error(
+ `onAudioData returned a different duration than the input audio data. Original duration: ${audioData.duration}, new duration: ${newAudioData.duration}`,
+ );
+ }
+
+ if (newAudioData.numberOfChannels !== audioData.numberOfChannels) {
+ throw new Error(
+ `onAudioData returned a different number of channels than the input audio data. Original channels: ${audioData.numberOfChannels}, new channels: ${newAudioData.numberOfChannels}`,
+ );
+ }
+
+ if (newAudioData.sampleRate !== audioData.sampleRate) {
+ throw new Error(
+ `onAudioData returned a different sample rate than the input audio data. Original sample rate: ${audioData.sampleRate}, new sample rate: ${newAudioData.sampleRate}`,
+ );
+ }
+
+ if (newAudioData.format !== audioData.format) {
+ throw new Error(
+ `onAudioData returned a different format than the input audio data. Original format: ${audioData.format}, new format: ${newAudioData.format}`,
+ );
+ }
+ if (newAudioData.timestamp !== audioData.timestamp) {
+ throw new Error(
+ `onAudioData returned a different timestamp than the input audio data. Original timestamp: ${audioData.timestamp}, new timestamp: ${newAudioData.timestamp
+ );
+ }
+
audioData.close();
}