We might change the API at any time, until we remove this notice.
getPartialAudioData()v4.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.
Extract audio data from 10-20 secondstsximport {getPartialAudioData } from '@remotion/webcodecs';constaudioData = awaitgetPartialAudioData ({src : 'https://remotion.media/audio.wav',fromSeconds : 10,toSeconds : 20,channelIndex : 0, // Left channel for stereo audiosignal : newAbortController ().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 channel1= Right channel
For mono audio, use 0.
signal
AbortSignal
An AbortSignal to cancel the operation.
Using AbortControllertsximport {getPartialAudioData } from '@remotion/webcodecs';import {hasBeenAborted } from '@remotion/media-parser';constcontroller = newAbortController ();// Cancel after 5 secondssetTimeout (() =>controller .abort (), 5000);try {constaudioData = awaitgetPartialAudioData ({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-parserfor parsing and WebCodecs for efficient audio decoding - Sample rate and other audio characteristics depend on the source media file