Skip to content

🎥 Make videos programmatically with React

update documentation, document audioBufferToDataUrl()

Jonny Burger5b21cd6f4cf4
diff --git a/packages/docs/docs/audiobuffertodataurl.md b/packages/docs/docs/audiobuffertodataurl.md
new file mode 100644
index 0000000000..a969467bfe
--- /dev/null
+++ b/packages/docs/docs/audiobuffertodataurl.md
@@ -0,0 +1,86 @@
+---
+id: audio-buffer-to-data-url
+title: audioBufferToDataUrl()
+---
+
+_Part of the `@remotion/media-utils`package of helper functions. Available from v2.5.6._
+
+This API takes an [`AudioBuffer`](https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer) instance and converts it to a Base 64 Data URL so it can be passed to an [`<Audio />`](/docs/audio) tag.
+
+## API Usage
+
+```tsx twoslash
+const audioBuffer = new AudioBuffer({ length: 0, sampleRate: 44100 });
+// ---cut---
+import { audioBufferToDataUrl } from "@remotion/media-utils";
+
+const str = audioBufferToDataUrl(audioBuffer);
+```
+
+## Example: Rendering a sine tone
+
+The following composition will render a sine tone with a C4 pitch.
+
+```tsx twoslash
+import { interpolate } from "remotion";
+import { useCallback, useEffect, useState } from "react";
+import { Audio, continueRender, delayRender, useVideoConfig } from "remotion";
+import { audioBufferToDataUrl } from "@remotion/media-utils";
+
+const C4_FREQUENCY = 261.63;
+const sampleRate = 44100;
+
+export const OfflineAudioBufferExample: React.FC = () => {
+ const [handle] = useState(() => delayRender());
+ const [audioBuffer, setAudioBuffer] = useState<string | null>(null);
+ const { fps, durationInFrames } = useVideoConfig();
+ const lengthInSeconds = durationInFrames / fps;
+
+ const renderAudio = useCallback(async () => {
+ const offlineContext = new OfflineAudioContext({
+ numberOfChannels: 2,
+ length: sampleRate * lengthInSeconds,
+ sampleRate,
+ });
+ const oscillatorNode = offlineContext.createOscillator();
+ const gainNode = offlineContext.createGain();
+ oscillatorNode.connect(gainNode);
+ gainNode.connect(offlineContext.destination);
+ gainNode.gain.setValueAtTime(0.5, offlineContext.currentTime);
+
+ oscillatorNode.type = "sine";
+ oscillatorNode.frequency.value = C4_FREQUENCY;
+
+ const { currentTime } = offlineContext;
+ oscillatorNode.start(currentTime);
+ oscillatorNode.stop(currentTime + lengthInSeconds);
+
+ const buffer = await offlineContext.startRendering();
+ setAudioBuffer(audioBufferToDataUrl(buffer));
+
+ continueRender(handle);
+ }, [handle, lengthInSeconds]);
+
+ useEffect(() => {
+ renderAudio();
+ }, [renderAudio]);
+
+ return audioBuffer ? (
+ <Audio
+ src={audioBuffer}
+ startFrom={0}
+ endAt={100}
+ volume={(f) =>
+ interpolate(f, [0, 50, 100], [0, 1, 0], {
+ extrapolateLeft: "clamp",
+ extrapolateRight: "clamp",
+ })
+ }
+ />
+ ) : null;
+};
+```
+
+## See also
+
+- [Rendering audio only](/docs/using-audio/#rendering-audio-only)
diff --git a/packages/docs/docs/get-audio-data.md b/packages/docs/docs/get-audio-data.md
index 965924a2e7..0a2963221b 100644
--- a/packages/docs/docs/get-audio-data.md
+++ b/packages/docs/docs/get-audio-data.md
@@ -3,7 +3,7 @@ title: getAudioData()
id: get-audio-data
---
-_Part of the `@remotion/media-utils`_ package of helper functions.
+_Part of the `@remotion/media-utils` package of helper functions._
Takes an audio `src`, loads it and returns data and metadata for the specified source.
diff --git a/packages/docs/docs/get-audio-duration.md b/packages/docs/docs/get-audio-duration.md
index 8c3c89aeae..aa0c2b1afc 100644
--- a/packages/docs/docs/get-audio-duration.md
+++ b/packages/docs/docs/get-audio-duration.md
@@ -3,7 +3,7 @@ title: getAudioDuration()
id: get-audio-duration
---
-_Part of the `@remotion/media-utils`_ package of helper functions.
+_Part of the `@remotion/media-utils` package of helper functions._
Gets the duration in seconds of an audio source. Remotion will create an invisible `<audio>` tag, load the audio and return the duration.
diff --git a/packages/docs/docs/get-video-metadata.md b/packages/docs/docs/get-video-metadata.md
index d2a7218005..03b0d72b0a 100644
--- a/packages/docs/docs/get-video-metadata.md
+++ b/packages/docs/docs/get-video-metadata.md
@@ -3,7 +3,7 @@ title: getVideoMetadata()
id: get-video-metadata
---
-_Part of the `@remotion/media-utils`_ package of helper functions.
+_Part of the `@remotion/media-utils` package of helper functions._
Takes a `src` to a video, loads it and returns metadata for the specified source.
diff --git a/packages/docs/docs/get-waveform-portion.md b/packages/docs/docs/get-waveform-portion.md
index beb23fa50f..05d56b4d51 100644
--- a/packages/docs/docs/get-waveform-portion.md
+++ b/packages/docs/docs/get-waveform-portion.md
@@ -3,7 +3,7 @@ title: getWaveformPortion()
id: get-waveform-portion
---
-_Part of the `@remotion/media-utils`_ package of helper functions.
+_Part of the `@remotion/media-utils` package of helper functions._
Takes bulky waveform data (for example fetched by [`getAudioData()`](/docs/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()`](/docs/visualize-audio).
diff --git a/packages/docs/docs/use-audio-data.md b/packages/docs/docs/use-audio-data.md
index 5272e525a1..27f1ae1c7f 100644
--- a/packages/docs/docs/use-audio-data.md
+++ b/packages/docs/docs/use-audio-data.md
@@ -3,7 +3,7 @@ title: useAudioData()
id: use-audio-data
---
-_Part of the `@remotion/media-utils`_ package of helper functions.
+_Part of the `@remotion/media-utils` package of helper functions._
This convienience function wraps the [`getAudioData()`](/docs/get-audio-data) function into a hook and does 3 things:
diff --git a/packages/docs/docs/using-audio.md b/packages/docs/docs/using-audio.md
index 99bcd6c447..bf5de9348b 100644
--- a/packages/docs/docs/using-audio.md
+++ b/packages/docs/docs/using-audio.md
@@ -180,6 +180,16 @@ While Remotion doesn't limit the range of possible playback speeds, in developme
You can obtain audio data and create visualizations based on it. See the page [Audio visualization](/docs/audio-visualization) for more info.
+## Rendering audio only
+
+Exporting as `mp3`, `aac` and `wav` is supported. To render only the audio, specify an extension when exporting via CLI:
+
+```
+npx remotion render src/index.tsx my-comp audio.mp3
+```
+
+To render audio using the Node.JS APIs, set the `codec` property to one of the supported audio codecs.
+
## See also
- [Importing assets](/docs/assets)
diff --git a/packages/docs/docs/visualize-audio.md b/packages/docs/docs/visualize-audio.md
index c8e1cddf5c..9bd31984a9 100644
--- a/packages/docs/docs/visualize-audio.md
+++ b/packages/docs/docs/visualize-audio.md
@@ -3,7 +3,7 @@ title: visualizeAudio()
id: visualize-audio
---
-_Part of the `@remotion/media-utils`_ package of helper functions.
+_Part of the `@remotion/media-utils` package of helper functions._
This function takes in `AudioData` (preferrably fetched by the [`useAudioData()`](/docs/use-audio-data) hook) and processes it in a way that makes visualizing the audio that is playing at the current frame easy.
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index ecfdecb405..76c1025da3 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -91,6 +91,7 @@ module.exports = {
type: "category",
label: "API - @remotion/media-utils",
items: [
+ "audio-buffer-to-data-url",
"get-audio-data",
"get-audio-duration",
"get-video-metadata",