Skip to content

🎥 Make videos programmatically with React

Docs: Add docs for `getPartialAudioData()`

JonnyBurger90ad58e11062
diff --git a/packages/docs/docs/webcodecs/TableOfContents.tsx b/packages/docs/docs/webcodecs/TableOfContents.tsx
index d5a4632331..fffb67ab33 100644
--- a/packages/docs/docs/webcodecs/TableOfContents.tsx
+++ b/packages/docs/docs/webcodecs/TableOfContents.tsx
@@ -126,6 +126,12 @@ export const TableOfContents: React.FC = () => {
<strong>{'extractFrames()'}</strong>
<div>Extract frames from a video at specific timestamps.</div>
</TOCItem>
+ <TOCItem link="/docs/webcodecs/get-partial-audio-data">
+ <strong>{'getPartialAudioData()'}</strong>
+ <div>
+ Extract audio data from a specific time window of a media file.
+ </div>
+ </TOCItem>
<TOCItem link="/docs/webcodecs/rotate-and-resize-video-frame">
<strong>{'rotateAndResizeVideoFrame()'}</strong>
<div>Rotate and resize a video frame.</div>
diff --git a/packages/docs/docs/webcodecs/get-partial-audio-data.mdx b/packages/docs/docs/webcodecs/get-partial-audio-data.mdx
new file mode 100644
index 0000000000..86301473b7
--- /dev/null
+++ b/packages/docs/docs/webcodecs/get-partial-audio-data.mdx
@@ -0,0 +1,117 @@
+---
+image: /generated/articles-docs-webcodecs-get-partial-audio-data.png
+sidebar_label: getPartialAudioData()
+title: getPartialAudioData()
+slug: /webcodecs/get-partial-audio-data
+crumb: '@remotion/webcodecs'
+---
+
+import {UnstableDisclaimer} from './UnstableDisclaimer';
+
+:::warning
+
+<UnstableDisclaimer />
+:::
+
+# getPartialAudioData()<AvailableFrom v="4.0.328" />
+
+_Part of the [`@remotion/webcodecs`](/docs/webcodecs) package._
+
+Extracts audio data from a specific time window of a media file and returns it as a Float32Array.
+
+```tsx twoslash title="Extract audio data from 10-20 seconds"
+import {getPartialAudioData} from '@remotion/webcodecs';
+
+const audioData = await getPartialAudioData({
+ src: 'https://parser.media/audio.wav',
+ fromSeconds: 10,
+ toSeconds: 20,
+ channelIndex: 0, // Left channel for stereo audio
+ signal: new AbortController().signal,
+});
+
+console.log('Audio samples:', audioData.length);
+console.log('First sample value:', audioData[0]);
+```
+
+## API
+
+### `src`
+
+**string**
+
+A URL pointing to a media file, or a `File`/`Blob` object.
+
+If it is a remote URL, it must support CORS and the server must support byte-range requests for efficient seeking.
+
+### `fromSeconds`
+
+**number**
+
+The start time in seconds from which to extract audio data.
+
+### `toSeconds`
+
+**number**
+
+The end time in seconds until which to extract audio data.
+
+### `channelIndex`
+
+**number**
+
+The audio channel index to extract. For stereo audio:
+
+- `0` = Left channel
+- `1` = Right channel
+
+For mono audio, use `0`.
+
+### `signal`
+
+**AbortSignal**
+
+An [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the operation.
+
+```tsx twoslash title="Using AbortController"
+import {getPartialAudioData} from '@remotion/webcodecs';
+
+const controller = new AbortController();
+
+// Cancel after 5 seconds
+setTimeout(() => controller.abort(), 5000);
+
+try {
+ const audioData = await getPartialAudioData({
+ src: 'https://parser.media/audio.wav',
+ fromSeconds: 0,
+ toSeconds: 30,
+ channelIndex: 0,
+ signal: controller.signal,
+ });
+} catch (err) {
+ if (err.name === 'AbortError') {
+ console.log('Operation was cancelled');
+ }
+}
+```
+
+## Return value
+
+Returns a `Promise<Float32Array>` containing the audio samples for the requested time window and channel.
+
+The sample values are normalized floating-point numbers typically in the range of -1.0 to 1.0.
+
+## Notes
+
+- The function uses a small buffer (0.1 seconds) around the requested time window to ensure accurate extraction of chunks that span across boundaries
+- For multi-channel audio, samples are de-interleaved to extract the specific channel
+- The function leverages [`@remotion/media-parser`](/docs/media-parser) for parsing and WebCodecs for efficient audio decoding
+- Sample rate and other audio characteristics depend on the source media file
+
+## See also
+
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/webcodecs/src/get-partial-audio-data.ts)
+- [`createAudioDecoder()`](/docs/webcodecs/create-audio-decoder)
+- [`@remotion/media-parser`](/docs/media-parser)
+- [`@remotion/webcodecs`](/docs/webcodecs)
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 66dd0da998..42d4a9564d 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -6225,6 +6225,15 @@ export const articles = [
noAi: false,
slug: 'webcodecs/get-default-video-codec',
},
+ {
+ id: 'webcodecs/get-partial-audio-data',
+ title: 'getPartialAudioData()',
+ relativePath: 'docs/webcodecs/get-partial-audio-data.mdx',
+ compId: 'articles-docs-webcodecs-get-partial-audio-data',
+ crumb: '@remotion/webcodecs',
+ noAi: false,
+ slug: 'webcodecs/get-partial-audio-data',
+ },
{
id: 'webcodecs/index',
title: '@remotion/webcodecs',
diff --git a/packages/docs/static/generated/articles-docs-webcodecs-get-partial-audio-data.png b/packages/docs/static/generated/articles-docs-webcodecs-get-partial-audio-data.png
new file mode 100644
index 0000000000..99e81afacd
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-webcodecs-get-partial-audio-data.png differ