Skip to main content

getWaveformPortion()

@remotion/media-utils 工具函数包的一部分。

🌐 Part of the @remotion/media-utils package of helper functions.

处理庞大的波形数据(例如通过 getAudioData() 获取的)并返回经过修剪和简化的版本,以便更简单地可视化。如果你只需要音量数据,这个函数是合适的;如果你需要关于每个频率范围的更详细数据,请使用 visualizeAudio()

🌐 Takes bulky waveform data (for example fetched by getAudioData()) and returns a trimmed and simplified version of it, for simpler visualization. This function is suitable if you only need volume data, if you need more detailed data about each frequency range, use visualizeAudio().

参数

🌐 Arguments

一个具有以下参数的对象:

🌐 An object with the following arguments:

audioData

音频数据

🌐 AudioData

关于音频的信息。使用 getAudioData() 来获取它。

🌐 Information about the audio. Use getAudioData() to fetch it.

startTimeInSeconds

number

修剪波形以排除 startTimeInSeconds 之前的所有数据。

🌐 Trim the waveform to exclude all data before startTimeInSeconds.

durationInSeconds

number

修剪波形以排除 startTimeInSeconds + durationInSeconds 之后的所有数据。

🌐 trim the waveform to exclude all data after startTimeInSeconds + durationInSeconds.

numberOfSamples

number

你希望结果数组有多大。该函数将压缩波形以适应 numberOfSamples 个数据点。

🌐 How big you want the result array to be. The function will compress the waveform to fit in numberOfSamples data points.

channel

number

使用哪个通道。默认值为0。

🌐 Which channel to use. Defaults to 0.

outputRange

number

输出值的范围。可以是 minus-one-to-onezero-to-one。默认值为 zero-to-one

🌐 The range of the output values. Either minus-one-to-one or zero-to-one. Defaults to zero-to-one.

normalize?v4.0.280

boolean

默认 true。如果设置为 true,则数据会按比例缩放,使最大值为 1

🌐 Default true. If set to true, then the data gets scaled so that the biggest value is 1.

note

从 v4.0.267 到 v4.0.279,normalize 被错误地更改为 false。我们已从 v4.0.280 恢复了原始行为。

返回值

🌐 Return value

Bar[] - 具有以下属性的对象数组:

index

number

数据点的索引,从0开始。对于作为React key 属性指定而不产生警告非常有用。

🌐 The index of the datapoint, starting at 0. Useful for specifying as React key attribute without getting a warning.

amplitude

number

描述音频振幅/音量/响度的数值。数值范围在0到1之间。

🌐 A value describing the amplitude / volume / loudness of the audio. Values range between 0 and 1.

示例

🌐 Example

import {getAudioData, getWaveformPortion} from '@remotion/media-utils';
import {staticFile} from 'remotion';

const audioData = await getAudioData(staticFile('music.mp3')); /* {
  channelWaveforms: [Float32Array(4410000), Float32Array(4410000)],
  sampleRate: 44100,
  durationInSeconds: 100.0000,
  numberOfChannels: 2,
  resultId: "0.432878981",
  isRemote: false
} */

const waveformPortion = await getWaveformPortion({
  audioData,
  // Will select time range of 20-40 seconds
  startTimeInSeconds: 20,
  durationInSeconds: 20,
  numberOfSamples: 10,
}); // [{index: 0, amplitude: 0.14}, ... {index: 9, amplitude: 0.79}]

console.log(waveformPortion.length); // 10

替代方案

🌐 Alternatives

visualizeAudio() 函数更适合基于音频的频率特性(低音、中音、高音等)进行音频可视化。

🌐 The visualizeAudio() function is more suitable for visualizing audio based on frequency properties of the audio (bass, mids, highs, etc).

另请参阅

🌐 See also