Skip to content

πŸŽ₯ Make videos programmatically with React

`docs`: Add audio sample rate documentation page

Jonny Burgeradf652ef7724
diff --git a/packages/docs/docs/4-0-migration.mdx b/packages/docs/docs/4-0-migration.mdx
index 04ec6df525..b9ad508a7a 100644
--- a/packages/docs/docs/4-0-migration.mdx
+++ b/packages/docs/docs/4-0-migration.mdx
@@ -330,6 +330,7 @@ const composition = {
defaultVideoImageFormat: null,
defaultPixelFormat: null,
defaultProResProfile: null,
+ defaultSampleRate: null,
} as const;
// ---cut---
diff --git a/packages/docs/docs/artifacts.mdx b/packages/docs/docs/artifacts.mdx
index f4dc745218..0e8b7930da 100644
--- a/packages/docs/docs/artifacts.mdx
+++ b/packages/docs/docs/artifacts.mdx
@@ -91,6 +91,7 @@ const composition: VideoConfig = {
defaultVideoImageFormat: null,
defaultPixelFormat: null,
defaultProResProfile: null,
+ defaultSampleRate: null,
id: 'hi',
durationInFrames: 100,
};
diff --git a/packages/docs/docs/calculate-metadata.mdx b/packages/docs/docs/calculate-metadata.mdx
index 8157e5f718..4a91b12f19 100644
--- a/packages/docs/docs/calculate-metadata.mdx
+++ b/packages/docs/docs/calculate-metadata.mdx
@@ -25,7 +25,10 @@ Use it if you:
<Step>4</Step> want to add per-composition default video image format, pixel format, or ProRes profile
</div>
<div>
-<Step>5</Step> want to run code once, before the actual render starts.
+<Step>5</Step> want to add a per-composition default <a href="/docs/sample-rate">audio sample rate</a>
+</div>
+<div>
+<Step>6</Step> want to run code once, before the actual render starts.
</div>
The `calculateMetadata()` function is called a single time, independently from the concurrency of the render.
@@ -67,6 +70,8 @@ const calculateMetadata: CalculateMetadataFunction<MyComponentProps> = ({props,
defaultPixelFormat: 'yuv420p',
// or add per-composition default ProRes profile
defaultProResProfile: 'hq',
+ // or add per-composition default audio sample rate
+ defaultSampleRate: 44100,
};
};
@@ -109,6 +114,7 @@ The function must return an pure JSON-serializable object, which can contain the
- `defaultVideoImageFormat`: The default video image format (`'png'`, `'jpeg'`, or `'none'`). Optional. <AvailableFrom v="4.0.316" />
- `defaultPixelFormat`: The default pixel format (`'yuv420p'`, `'yuva420p'`, `'yuv422p'`, `'yuv444p'`, `'yuv420p10le'`, `'yuv422p10le'`, `'yuv444p10le'`, or `'yuva444p10le'`). Optional. <AvailableFrom v="4.0.316" />
- `defaultProResProfile`: The default ProRes profile (`'4444-xq'`, `'4444'`, `'hq'`, `'standard'`, `'light'`, or `'proxy'`). Optional. <AvailableFrom v="4.0.367" />
+- `defaultSampleRate`: The default [audio sample rate](/docs/sample-rate) in Hz. Optional. <AvailableFrom v="4.0.448" />
If you return a field, it will take precendence over the props directly passed to the composition. The defaultCodec returned will have a higher priority than the config file, but less priority than explicitly passing the option to renderMedia() i.e. the composition will be rendered with this codec if nothing with higher priority was specified.
diff --git a/packages/docs/docs/renderer/make-cancel-signal.mdx b/packages/docs/docs/renderer/make-cancel-signal.mdx
index 8bee5e63dc..13585ab7bb 100644
--- a/packages/docs/docs/renderer/make-cancel-signal.mdx
+++ b/packages/docs/docs/renderer/make-cancel-signal.mdx
@@ -29,6 +29,7 @@ const composition: VideoConfig = {
defaultVideoImageFormat: null,
defaultPixelFormat: null,
defaultProResProfile: null,
+ defaultSampleRate: null,
};
// ---cut---
import {makeCancelSignal, renderMedia} from '@remotion/renderer';
diff --git a/packages/docs/docs/sample-rate.mdx b/packages/docs/docs/sample-rate.mdx
new file mode 100644
index 0000000000..9fad900e99
--- /dev/null
+++ b/packages/docs/docs/sample-rate.mdx
@@ -0,0 +1,197 @@
+---
+image: /generated/articles-docs-sample-rate.png
+title: Audio Sample Rate
+crumb: 'Rendering'
+---
+
+The audio sample rate determines how many audio samples per second are stored in the output file. A higher sample rate preserves more detail in high-frequency content.
+
+## Default behavior
+
+By default, Remotion renders all audio at **48000 Hz** (48 kHz). This is a broadcast standard and produces good results for most content.
+
+All audio sources β€” regardless of their original sample rate β€” are resampled to the output sample rate during rendering. This means a 44100 Hz source embedded in a Remotion composition will be upsampled to 48000 Hz.
+
+## Resampling artifacts
+
+Resampling is a lossy transformation. If your source audio has a different sample rate than the output, the conversion may introduce subtle quality degradation. This is most noticeable with:
+
+- Music mastered at 44100 Hz (CD quality) being upsampled to 48000 Hz
+- High-quality recordings at 96000 Hz being downsampled
+- Audio containing sharp transients or high-frequency detail
+
+To avoid these artifacts, match the output sample rate to the sample rate of your source audio.
+
+## Overriding the sample rate<AvailableFrom v="4.0.448" />
+
+You can change the output sample rate using one of three methods. They are listed in order of priority β€” the first one that is set takes precedence.
+
+### Via CLI flag
+
+```bash
+npx remotion render MyComp --sample-rate=44100
+```
+
+### Via config file
+
+```ts twoslash title="remotion.config.ts"
+import {Config} from '@remotion/cli/config';
+
+Config.setSampleRate(44100);
+```
+
+### Via `renderMedia()`
+
+```ts twoslash title="render.ts"
+// @module: es2022
+// @target: es2017
+import {bundle} from '@remotion/bundler';
+import {renderMedia, selectComposition} from '@remotion/renderer';
+
+const bundled = await bundle({entryPoint: './src/index.ts'});
+
+const composition = await selectComposition({
+ serveUrl: bundled,
+ id: 'MyComp',
+});
+
+await renderMedia({
+ composition,
+ serveUrl: bundled,
+ codec: 'h264',
+ sampleRate: 44100,
+});
+```
+
+## Per-composition sample rate
+
+You can set the sample rate per composition using [`calculateMetadata()`](/docs/calculate-metadata). This is useful when your composition is driven by a media file with a known sample rate.
+
+```ts twoslash title="src/Root.tsx"
+// @filename: MyComp.tsx
+import React from 'react';
+
+export type MyCompProps = {
+ src: string;
+};
+
+export const MyComp: React.FC<MyCompProps> = ({src}) => {
+ return <div>{src}</div>;
+};
+
+// @filename: get-media-metadata.ts
+import {Input, ALL_FORMATS, UrlSource} from 'mediabunny';
+
+export const getMediaMetadata = async (src: string) => {
+ const input = new Input({
+ formats: ALL_FORMATS,
+ source: new UrlSource(src, {
+ getRetryDelay: () => null,
+ }),
+ });
+
+ const durationInSeconds = await input.computeDuration();
+ const videoTrack = await input.getPrimaryVideoTrack();
+ const audioTrack = await input.getPrimaryAudioTrack();
+
+ const dimensions = videoTrack
+ ? {
+ width: videoTrack.displayWidth,
+ height: videoTrack.displayHeight,
+ }
+ : null;
+ const packetStats = await videoTrack?.computePacketStats(50);
+ const fps = packetStats?.averagePacketRate ?? null;
+ const sampleRate = audioTrack?.sampleRate ?? null;
+
+ return {
+ durationInSeconds,
+ dimensions,
+ fps,
+ sampleRate,
+ };
+};
+
+// @filename: Root.tsx
+// ---cut---
+import React from 'react';
+import {CalculateMetadataFunction, Composition} from 'remotion';
+import {MyComp, MyCompProps} from './MyComp';
+import {getMediaMetadata} from './get-media-metadata';
+
+const calculateMetadata: CalculateMetadataFunction<
+ MyCompProps
+> = async ({props}) => {
+ const metadata = await getMediaMetadata(props.src);
+
+ return {
+ props,
+ durationInFrames: Math.ceil(
+ (metadata.durationInSeconds ?? 1) * 30,
+ ),
+ fps: 30,
+ width: metadata.dimensions?.width ?? 1920,
+ height: metadata.dimensions?.height ?? 1080,
+ defaultSampleRate: metadata.sampleRate ?? undefined,
+ };
+};
+
+export const Root: React.FC = () => {
+ return (
+ <Composition
+ id="MyComp"
+ component={MyComp}
+ durationInFrames={1}
+ fps={30}
+ width={1920}
+ height={1080}
+ defaultProps={{src: 'https://remotion.media/video.mp4'}}
+ calculateMetadata={calculateMetadata}
+ />
+ );
+};
+```
+
+If the media file has an audio track, the render sample rate will match the source. No CLI flag or config is needed.
+
+The `defaultSampleRate` returned by `calculateMetadata()` has lower priority than the CLI flag and config file. See [Priority](#priority) for the full order.
+
+## Mixing audio with different sample rates
+
+When a composition uses multiple audio sources with different sample rates, all of them are resampled to the single output sample rate. There is no way to preserve multiple sample rates in a single output file β€” this is a fundamental limitation of audio containers.
+
+Choose the sample rate that matches the majority of your sources, or the highest sample rate among them to minimize quality loss.
+
+## Priority
+
+When multiple sources define a sample rate, the highest-priority value wins:
+
+- <Step>1</Step> `--sample-rate` CLI flag / `sampleRate` option in `renderMedia()`
+- <Step>2</Step> `Config.setSampleRate()` in the config file
+- <Step>3</Step> `defaultSampleRate` returned by [`calculateMetadata()`](/docs/calculate-metadata)
+- <Step>4</Step> Default: `48000`
+
+## Sample rate during preview
+
+During preview in the [Remotion Studio](/docs/studio), audio is always played back at 48000 Hz. This cannot currently be changed.
+
+This means previewed audio may sound slightly different from the final render if you use a custom sample rate. The rendered output will use the correct sample rate.
+
+## Common sample rates
+
+| Sample Rate | Use Case |
+| --- | --- |
+| 8000 Hz | Telephone, low-quality voice |
+| 16000 Hz | Wideband speech, voice assistants |
+| 22050 Hz | AM radio quality |
+| 44100 Hz | CD audio, music production |
+| 48000 Hz | Broadcast video, Remotion default |
+| 96000 Hz | High-resolution audio |
+
+## See also
+
+- [Encoding guide](/docs/encoding)
+- [`calculateMetadata()`](/docs/calculate-metadata)
+- [`Config.setSampleRate()`](/docs/config#setsamplerate)
+- [`renderMedia()`](/docs/renderer/render-media#samplerate)
+- [Getting metadata from a video](/docs/mediabunny/metadata)
diff --git a/packages/docs/docs/use-video-config.mdx b/packages/docs/docs/use-video-config.mdx
index b1de8ce776..1bfa83152d 100644
--- a/packages/docs/docs/use-video-config.mdx
+++ b/packages/docs/docs/use-video-config.mdx
@@ -61,6 +61,10 @@ The props that were passed to the composition, after [all transformations](/docs
The default codec that is used for rendering this composition. Use [`calculateMetadata()`](/docs/calculate-metadata) to modify it.
+### `defaultSampleRate`<AvailableFrom v="4.0.448" />
+
+The default [audio sample rate](/docs/sample-rate) that is used for rendering this composition. Use [`calculateMetadata()`](/docs/calculate-metadata) to modify it.
+
These properties are controlled by passing them as props to [`<Composition>`](/docs/composition). Read the page about [the fundamentals](/docs/the-fundamentals) to read how to setup a Remotion project.
## Compatibility
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 01aee05994..40c0872a24 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -1015,6 +1015,7 @@ const sidebars: SidebarsConfig = {
},
items: [
'encoding',
+ 'sample-rate',
'stills',
'scaling',
'transparent-videos',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 4157ae9e12..f3ce0c0894 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -5223,6 +5223,15 @@ export const articles = [
noAi: false,
slug: 'rounded-text-box/index',
},
+ {
+ id: 'sample-rate',
+ title: 'Audio Sample Rate',
+ relativePath: 'docs/sample-rate.mdx',
+ compId: 'articles-docs-sample-rate',
+ crumb: 'Rendering',
+ noAi: false,
+ slug: 'sample-rate',
+ },
{
id: 'scaling',
title: 'Output scaling',
diff --git a/packages/docs/static/generated/articles-docs-sample-rate.png b/packages/docs/static/generated/articles-docs-sample-rate.png
new file mode 100644
index 0000000000..1e3bf82c6f
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-sample-rate.png differ