Skip to main content
warning
This package is experimental.
We might change the API at any time, until we remove this notice.

获取部分音频数据()v4.0.328

🌐 getPartialAudioData()v4.0.328

属于 @remotion/webcodecs 软件包的一部分.

🌐 Part of the @remotion/webcodecs package.

从媒体文件的特定时间窗口中提取音频数据,并将其作为 Float32Array 返回。

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

Extract audio data from 10-20 seconds
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

指向媒体文件的 URL,或一个 File/Blob 对象。

🌐 A URL pointing to a media file, or a File/Blob object.

如果它是远程 URL,则必须支持 CORS,并且服务器必须支持字节范围请求以实现高效查找。

🌐 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 = 左声道
  • 1 = 右声道

对于单声道音频,使用 0

🌐 For mono audio, use 0.

signal

中止信号

一个AbortSignal来取消操作。

🌐 An AbortSignal to cancel the operation.

Using AbortController
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

返回一个包含所请求时间窗口和通道的音频样本的 Promise<Float32Array>

🌐 Returns a Promise<Float32Array> containing the audio samples for the requested time window and channel.

样本值是归一化的浮点数,通常在 -1.0 到 1.0 的范围内。

🌐 The sample values are normalized floating-point numbers typically in the range of -1.0 to 1.0.

注意

🌐 Notes

  • 该函数在请求的时间窗口周围使用一个小缓冲区(0.1 秒),以确保准确提取跨越边界的片段
  • 对于多通道音频,样本被解交错以提取特定通道
  • 该功能利用 @remotion/media-parser 进行解析,并使用 WebCodecs 进行高效音频解码
  • 采样率和其他音频特性取决于源媒体文件

另请参阅

🌐 See also