getAudioData()
@remotion/media-utils 工具函数包的一部分。
🌐 Part of the @remotion/media-utils package of helper functions.
获取一个音频或视频 src,加载它并返回指定源的数据和元数据。
🌐 Takes an audio or video src, loads it and returns data and metadata for the specified source.
远程音频文件需要支持 CORS。 Remotion 的来源通常是 你可以使用 无需音频的 CORS。 你可以在渲染过程中 禁用 CORS 。更多信息
http://localhost:3000,但如果在 Lambda 上渲染或端口被占用,则可能不同。getAudioDurationInSeconds()
参数
🌐 Arguments
src
指向音频资源的字符串。
🌐 A string pointing to an audio asset.
options?v4.0.121
sampleRate?v4.0.121
应传递给AudioContext构造函数的sampleRate。如果未提供,默认值为48000。
🌐 The sampleRate that should be passed into the AudioContext constructor. If not provided, the default value is 48000.
在 4.0.121 之前的版本中,默认值是 undefined,导致各设备渲染时行为不确定。
🌐 In versions before 4.0.121, the default value was undefined, leading to undeterministic behavior across devices rendering.
返回值
🌐 Return value
Promise<AudioData>
关于音频数据的信息的对象:
🌐 An object with information about the audio data:
channelWaveforms
Float32Array[]
每个通道的波形信息数组。
🌐 An array with waveform information for each channel.
sampleRate
number
生成的 AudioContext 的采样率。如果传入 sampleRate 输入选项,将与其相同,否则为 48000,在 4.0.121 之前的版本中,则为设备首选输出设备的采样率。
🌐 The sample rate of the generated AudioContext. This will be the same as the sampleRate input option if passed, 48000 otherwise, and in version previous to 4.0.121, the sample rate of the device's preferred output device.
以前,这份文档说明这是音频文件的采样率。这个说法是不正确的。音频文件的采样率不会暴露给浏览器的 JavaScript 环境。
durationInSeconds
number
音频的持续时间,以秒为单位。
🌐 The duration of the audio in seconds.
numberOfChannels
number
音频文件中包含的通道数量。这对应于 channelWaveforms 数组的长度。
🌐 The number of channels contained in the audio file. This corresponds to the length of the channelWaveforms array.
resultId
string 此音频数据获取调用的唯一标识符。如果其他函数多次使用相同的 resultId 调用,它们可以缓存昂贵的操作。
🌐 string Unique identifier of this audio data fetching call. Other functions can cache expensive operations if they get called with the same resultId multiple times.
isRemote
boolean
音频是本地导入的还是来自其他来源的。
🌐 Whether the audio was imported locally or from a different origin.
示例
🌐 Example
import {getAudioData } from '@remotion/media-utils';
import music from './music.mp3';
await getAudioData (music ); /* {
channelWaveforms: [Float32Array(4410000), Float32Array(4410000)],
sampleRate: 44100,
durationInSeconds: 100.0000,
numberOfChannels: 2,
resultId: "0.432878981",
isRemote: false
} */
await getAudioData ('https://example.com/remote-audio.aac'); /* {
channelWaveforms: [Float32Array(4800000)],
sampleRate: 48000,
durationInSeconds: 100.0000,
numberOfChannels: 1,
resultId: "0.432324444",
isRemote: true
} */
await getAudioData (staticFile ('my-file.wav')); /* {
channelWaveforms: [Float32Array(4800000)],
sampleRate: 48000,
durationInSeconds: 100.0000,
numberOfChannels: 1,
resultId: "0.6891332223",
isRemote: false
} */错误
🌐 Errors
如果你传入一个没有音轨的文件,这个函数会抛出一个你需要处理的错误。
🌐 If you pass in a file that has no audio track, this function will throw an error you need to handle.
要确定文件是否有音频轨道,可以在服务器上使用 getVideoMetadata() 函数,如果文件没有音频轨道则拒绝该文件。为此,请检查 audioCodec 字段是否为 null。
🌐 To determine if a file has an audio track, you may use the getVideoMetadata() function on the server to reject a file if it has no audio track. To do so, check if the audioCodec field is null.
缓存行为
🌐 Caching behavior
这个函数正在缓存它返回的结果。
🌐 This function is memoizing the results it returns.
如果你多次传入相同的参数给 src,从第二次开始它将返回缓存的版本,无论文件是否已更改。
要清除缓存,你必须重新加载页面。
🌐 If you pass in the same argument to src multiple times, it will return a cached version from the second time on, regardless of if the file has changed.
To clear the cache, you have to reload the page.
替代方案
🌐 Alternatives
如果你只需要时长,优先使用getAudioDurationInSeconds(),因为它更快,不需要读取波形数据。
🌐 If you need only the duration, prefer getAudioDurationInSeconds() which is faster because it doesn't need to read waveform data.
使用 useAudioData() 辅助钩子,这样就不必自己进行状态管理,并且可以将调用封装在 delayRender() 中。
🌐 Use the useAudioData() helper hook to not have to do state management yourself and to wrap the call in delayRender().
另请参阅
🌐 See also