π₯ Make videos programmatically with React
`docs`: Simplify sample rate docs example and fix docLink
Jonny Burgercab78b2e4fde
diff --git a/packages/docs/docs/sample-rate.mdx b/packages/docs/docs/sample-rate.mdxindex c6ff51cd73..139c6f15a7 100644--- a/packages/docs/docs/sample-rate.mdx+++ b/packages/docs/docs/sample-rate.mdx@@ -4,41 +4,21 @@ title: Audio Sample Ratecrumb: '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.+Match the sample rate of your audio input to the output sample rate to get the best audio quality.## 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.+By default, Remotion renders all audio at 48000 Hz (48 kHz).-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.+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.+This is because when the Remotion renderer starts, it doesn't know which assets will be added.+There can also be multiple audios with multiple sample rates in a single composition, which is why Remotion resamples all audio to 48000 Hz by default.## 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);-```+You can change the output sample rate:### Via `renderMedia()`@@ -84,13 +64,29 @@ await renderMediaOnWeb({});```+### Via CLI flag++```bash+npx remotion render MyComp --sample-rate=44100+```++### Via config file++This only affects the default in the Studio and CLI commands.++```ts twoslash title="remotion.config.ts"+import {Config} from '@remotion/cli/config';++Config.setSampleRate(44100);+```+### Via the Studio render dialogIn the [Remotion Studio](/docs/studio) render dialog, you can select the sample rate from a dropdown in the **Audio** tab.-## Per-composition sample rate+## Matching the sample rate of your source audio-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.+You can set the sample rate based on input props using [`calculateMetadata()`](/docs/calculate-metadata).```ts twoslash title="src/Root.tsx"// @filename: MyComp.tsx@@ -104,60 +100,28 @@ 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 {Input, ALL_FORMATS, UrlSource} from 'mediabunny';import {MyComp, MyCompProps} from './MyComp';-import {getMediaMetadata} from './get-media-metadata';const calculateMetadata: CalculateMetadataFunction<MyCompProps> = async ({props}) => {- const metadata = await getMediaMetadata(props.src);+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(props.src, {+ getRetryDelay: () => null,+ }),+ });++ const audioTrack = await input.getPrimaryAudioTrack();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,+ defaultSampleRate: audioTrack?.sampleRate ?? undefined,};};@@ -177,9 +141,7 @@ export const Root: React.FC = () => {};```-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.+The `defaultSampleRate` returned by `calculateMetadata()` has lower priority than the CLI flag or settings in the Studio dialog.## Mixing audio with different sample rates@@ -187,32 +149,12 @@ When a composition uses multiple audio sources with different sample rates, allChoose 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 previewDuring 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)diff --git a/packages/renderer/src/options/sample-rate.tsx b/packages/renderer/src/options/sample-rate.tsxindex ff8e7b211b..f73586654c 100644--- a/packages/renderer/src/options/sample-rate.tsx+++ b/packages/renderer/src/options/sample-rate.tsx@@ -15,7 +15,7 @@ export const sampleRateOption = {</>),ssrName: 'sampleRate' as const,- docLink: 'https://www.remotion.dev/docs/config#setsamplerate',+ docLink: 'https://www.remotion.dev/docs/sample-rate',type: 48000 as number,getValue: ({commandLine}: {commandLine: Record<string, unknown>},