Skip to content

🎥 Make videos programmatically with React

audiowaveform data

Jonny Burgere5ade7915405
diff --git a/packages/cli/src/editor/components/AudioWaveform.tsx b/packages/cli/src/editor/components/AudioWaveform.tsx
index 709a8346f4..fdfc5fcaf3 100644
--- a/packages/cli/src/editor/components/AudioWaveform.tsx
+++ b/packages/cli/src/editor/components/AudioWaveform.tsx
@@ -110,10 +110,9 @@ export const AudioWaveform: React.FC<{
);
return getWaveformPortion({
- metadata,
- startFrom,
- fps,
- durationInFrames,
+ audioData: metadata,
+ startTimeInSeconds: startFrom * fps,
+ durationInSeconds: durationInFrames * fps,
numberOfSamples,
});
}, [durationInFrames, fps, metadata, startFrom, visualizationWidth]);
diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json
index 41439c000c..a050a0c7d7 100644
--- a/packages/cli/tsconfig.json
+++ b/packages/cli/tsconfig.json
@@ -7,8 +7,9 @@
"skipLibCheck": true
},
"references": [
- { "path": "../core" },
- { "path": "../bundler" },
- { "path": "../renderer" }
+ {"path": "../core"},
+ {"path": "../bundler"},
+ {"path": "../media-utils"},
+ {"path": "../renderer"}
]
}
diff --git a/packages/docs/docs/get-video-metadata.md b/packages/docs/docs/get-video-metadata.md
index 18e81be134..0ad874d01b 100644
--- a/packages/docs/docs/get-video-metadata.md
+++ b/packages/docs/docs/get-video-metadata.md
@@ -49,6 +49,11 @@ await getVideoMetadata('https://example.com/remote-audio.webm'); /* {
} */
```
+## Caching behavior
+
+This function is memoizing the results it returns.
+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.
+
## See also
- [Using videos](/docs/assets#using-videos)
diff --git a/packages/docs/docs/get-waveform-portion.md b/packages/docs/docs/get-waveform-portion.md
index 853e3fa497..9101ea84ef 100644
--- a/packages/docs/docs/get-waveform-portion.md
+++ b/packages/docs/docs/get-waveform-portion.md
@@ -9,4 +9,55 @@ This function is part of Remotion 2.0 which is not yet released. The information
_Part of the `@remotion/media-utils`_ package of helper functions.
-TODO
+Takes bulky waveform data (for example fetched by [`getAudioData()`](get-audio-data)) 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()`](visualize-audio).
+
+## Arguments
+
+### `options`
+
+An object with the following arguments:
+
+- `audioData`: `AudioData` - information about the audio. Use [`getAudioData()`](get-audio-data) to fetch it.
+- `startTimeInSeconds`: `number` - trim the waveform to exclude all data before `startTimeInSeconds`.
+- `durationInSeconds`: `number` - trim the waveform to exclude all data after `startTimeInSeconds + durationInSeconds`.
+- `numberOfSamples`: `number` - how big you want the result array to be. The function will compress the waveform to fit in `numberOfSamples` data points.
+
+## Return value
+
+`Bar[]` - An array of objects with the following properties:
+
+- `index`: `number` - the index of the datapoint, starting at 0. Useful for specifying as React `key` attribute without getting a warning.
+- `amplitude`: `number` - a value describing the amplitude / volume / loudness of the audio.
+
+## Example
+
+```tsx
+import {getAudioData} from '@remotion/media-utils';
+import music from './music.mp3';
+
+const audioData = await getAudioData(music); /* {
+ channelWaveforms: [Float32Array(4410000), Float32Array(4410000)],
+ sampleRate: 44100,
+ durationInSeconds: 100.0000,
+ numberOfChannels: 2,
+ resultId: "0.432878981",
+ isRemote: false
+} */
+
+await waveformPortion = getWaveformPortion({
+ audioData,
+ // Will select time range of 20-40 seconds
+ startTimeInSeconds: 20,
+ durationInSeconds: 20,
+ numberOfSamples: 10
+}) // [{index: 0, amplitude: 1.2203}, ... {index: 9, amplitude: 3.2211}]
+
+console.log(waveformPortion.length) // 10
+```
+
+## Alternatives
+
+The [`visualizeAudio()`](visualize-audio) function is more suitable for visualizing audio based on frequency properties of the audio (bass, mids, highs, etc).
+
+- [Using audio](/docs/using-audio)
+- [`<Audio/>`](/docs/audio)
diff --git a/packages/media-utils/src/get-waveform-portion.ts b/packages/media-utils/src/get-waveform-portion.ts
index 667531f727..05d3fc3259 100644
--- a/packages/media-utils/src/get-waveform-portion.ts
+++ b/packages/media-utils/src/get-waveform-portion.ts
@@ -7,29 +7,27 @@ type Bar = {
};
export const getWaveformPortion = ({
- metadata,
- startFrom,
- fps,
- durationInFrames,
+ audioData,
+ startTimeInSeconds,
+ durationInSeconds,
numberOfSamples,
}: {
- metadata: AudioData;
- startFrom: number;
- fps: number;
- durationInFrames: number;
+ audioData: AudioData;
+ startTimeInSeconds: number;
+ durationInSeconds: number;
numberOfSamples: number;
}): Bar[] => {
const startSample = Math.floor(
- (startFrom / (metadata.durationInSeconds / fps)) *
- metadata.channelWaveforms[0].length
+ (startTimeInSeconds / audioData.durationInSeconds) *
+ audioData.channelWaveforms[0].length
);
const endSample = Math.floor(
- ((startFrom + durationInFrames) / (metadata.durationInSeconds * fps)) *
- metadata.channelWaveforms[0].length
+ ((startTimeInSeconds + durationInSeconds) / audioData.durationInSeconds) *
+ audioData.channelWaveforms[0].length
);
return getWaveformSamples(
- metadata.channelWaveforms[0].slice(startSample, endSample),
+ audioData.channelWaveforms[0].slice(startSample, endSample),
numberOfSamples
).map((w, i) => {
return {