Back to Remotion

getPartialAudioData()

packages/docs/docs/webcodecs/get-partial-audio-data.mdx

4.0.4683.0 KB
Original Source

:::warning We are phasing out Remotion WebCodecs and are moving to Mediabunny! :::

import {UnstableDisclaimer} from './UnstableDisclaimer';

:::warning

<UnstableDisclaimer /> :::

getPartialAudioData()<AvailableFrom v="4.0.328" />

Part of the @remotion/webcodecs package.

Extracts audio data from a specific time window of a media file and returns it as a Float32Array.

tsx
import {getPartialAudioData} from '@remotion/webcodecs';

const audioData = await getPartialAudioData({
  src: 'https://remotion.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 to cancel the operation.

tsx
import {getPartialAudioData} from '@remotion/webcodecs';
import {hasBeenAborted} from '@remotion/media-parser';

const controller = new AbortController();

// Cancel after 5 seconds
setTimeout(() => controller.abort(), 5000);

try {
  const audioData = await getPartialAudioData({
    src: 'https://remotion.media/audio.wav',
    fromSeconds: 0,
    toSeconds: 30,
    channelIndex: 0,
    signal: controller.signal,
  });
} catch (err) {
  if (hasBeenAborted(err)) {
    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 for parsing and WebCodecs for efficient audio decoding
  • Sample rate and other audio characteristics depend on the source media file

See also