Skip to content

🎥 Make videos programmatically with React

show on remotion.dev/convert

Jonny Burgerda9dc704ea62
diff --git a/packages/convert/app/components/ContainerOverview.tsx b/packages/convert/app/components/ContainerOverview.tsx
index 79c1c34cb7..ed98c5ebe5 100644
--- a/packages/convert/app/components/ContainerOverview.tsx
+++ b/packages/convert/app/components/ContainerOverview.tsx
@@ -30,6 +30,7 @@ export const ContainerOverview: React.FC<{
readonly metadata: MediaParserMetadataEntry[] | null;
readonly location: MediaParserLocation | null;
readonly isAudioOnly: boolean;
+ readonly sampleRate: number | null;
}> = ({
container,
dimensions,
@@ -42,6 +43,7 @@ export const ContainerOverview: React.FC<{
metadata,
location,
isAudioOnly,
+ sampleRate,
}) => {
return (
<Table className="table-fixed">
@@ -134,6 +136,12 @@ export const ContainerOverview: React.FC<{
)}
</TableCell>
</TableRow>
+ {sampleRate !== null ? (
+ <TableRow>
+ <TableCell className="font-brand">Sample Rate</TableCell>
+ <TableCell className="text-right">{sampleRate} Hz</TableCell>
+ </TableRow>
+ ) : null}
{isAudioOnly ? null : (
<TableRow>
<TableCell className="font-brand">HDR</TableCell>
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index 1fde11bbd6..b35192feeb 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -48,6 +48,7 @@ import {getDefaultContainerForConversion} from './guess-codec-from-source';
import {M3uStreamSelector} from './M3uStreamSelector';
import {MirrorComponents} from './MirrorComponents';
import {PauseResumeAndCancel} from './PauseResumeAndCancel';
+import {ResampleUi} from './ResampleUi';
import {ResizeUi} from './ResizeUi';
import {RotateComponents} from './RotateComponents';
import {useSupportedConfigs} from './use-supported-configs';
@@ -138,6 +139,26 @@ const ConvertUI = ({
.map(([section]) => section as ConvertSections);
}, [action]);
+ const [resampleUserPreferenceActive, setResampleUserPreferenceActive] =
+ useState(false);
+ const [resampleRate, setResampleRate] = useState<number>(16000);
+
+ const canResample = useMemo(() => {
+ return outputContainer === 'wav';
+ }, [outputContainer]);
+
+ const actualResampleRate = useMemo(() => {
+ if (!canResample) {
+ return null;
+ }
+
+ if (!resampleUserPreferenceActive) {
+ return null;
+ }
+
+ return resampleRate;
+ }, [resampleRate, canResample, resampleUserPreferenceActive]);
+
const supportedConfigs = useSupportedConfigs({
outputContainer,
tracks,
@@ -145,6 +166,7 @@ const ConvertUI = ({
userRotation,
inputContainer,
resizeOperation,
+ sampleRate: actualResampleRate,
});
const isH264Reencode = supportedConfigs?.videoTrackOptions.some((o) => {
@@ -578,6 +600,32 @@ const ConvertUI = ({
);
}
+ if (section === 'resample') {
+ if (!canResample) {
+ return null;
+ }
+
+ return (
+ <div key="resample">
+ <ConvertUiSection
+ active={resampleUserPreferenceActive}
+ setActive={setResampleUserPreferenceActive}
+ >
+ Resample
+ </ConvertUiSection>
+ {resampleUserPreferenceActive ? (
+ <ResampleUi
+ sampleRate={resampleRate}
+ setSampleRate={setResampleRate}
+ currentSampleRate={
+ tracks?.audioTracks[0]?.sampleRate ?? null
+ }
+ />
+ ) : null}
+ </div>
+ );
+ }
+
if (section === 'resize') {
if (isAudioExclusively) {
return null;
diff --git a/packages/convert/app/components/Probe.tsx b/packages/convert/app/components/Probe.tsx
index c843c88236..908c953c3f 100644
--- a/packages/convert/app/components/Probe.tsx
+++ b/packages/convert/app/components/Probe.tsx
@@ -100,6 +100,7 @@ export const Probe: React.FC<{
location,
keyframes,
images,
+ sampleRate,
} = probeResult;
const onClick = useCallback(() => {
@@ -204,6 +205,7 @@ export const Probe: React.FC<{
metadata={metadata}
isHdr={isHdr}
location={location}
+ sampleRate={sampleRate}
/>
) : selectedTrack.type === 'video' ? (
<VideoTrackOverview
diff --git a/packages/convert/app/components/ResampleUi.tsx b/packages/convert/app/components/ResampleUi.tsx
new file mode 100644
index 0000000000..7321d69e23
--- /dev/null
+++ b/packages/convert/app/components/ResampleUi.tsx
@@ -0,0 +1,40 @@
+import {Label} from './ui/label';
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from './ui/select';
+
+export const ResampleUi: React.FC<{
+ sampleRate: number;
+ setSampleRate: (sampleRate: number) => void;
+ currentSampleRate: number | null;
+}> = ({sampleRate, setSampleRate, currentSampleRate}) => {
+ return (
+ <div>
+ <div className="h-4" />
+ <Label htmlFor="sample-rate">Sample rate</Label>
+ <Select
+ value={String(sampleRate)}
+ onValueChange={(val) => setSampleRate(Number(val))}
+ >
+ <SelectTrigger>
+ <SelectValue placeholder="Select a sample rate" />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="16000">16.000 Hz</SelectItem>
+ <SelectItem value="22050">22.050 Hz</SelectItem>
+ <SelectItem value="24000">24.000 Hz</SelectItem>
+ <SelectItem value="32000">32.000 Hz</SelectItem>
+ <SelectItem value="44100">44.100 Hz</SelectItem>
+ <SelectItem value="48000">48 Hz</SelectItem>
+ </SelectContent>
+ </Select>
+ <div className="text-sm mt-2 text-muted-foreground">
+ The current sample rate is {currentSampleRate} Hz.
+ </div>
+ </div>
+ );
+};
diff --git a/packages/convert/app/components/get-supported-configs.tsx b/packages/convert/app/components/get-supported-configs.tsx
index fe2de4fd05..bb38aad32b 100644
--- a/packages/convert/app/components/get-supported-configs.tsx
+++ b/packages/convert/app/components/get-supported-configs.tsx
@@ -87,6 +87,7 @@ export const getSupportedConfigs = async ({
userRotation,
inputContainer,
resizeOperation,
+ sampleRate,
}: {
tracks: MediaParserTracks;
container: ConvertMediaContainer;
@@ -95,6 +96,7 @@ export const getSupportedConfigs = async ({
userRotation: number;
inputContainer: MediaParserContainer;
resizeOperation: ResizeOperation | null;
+ sampleRate: number | null;
}): Promise<SupportedConfigs> => {
const availableVideoCodecs = getAvailableVideoCodecs({container});
@@ -170,6 +172,7 @@ export const getSupportedConfigs = async ({
audioCodec,
track,
bitrate,
+ sampleRate,
});
if (canReencode) {
@@ -177,7 +180,7 @@ export const getSupportedConfigs = async ({
type: 'reencode',
audioCodec,
bitrate,
- sampleRate: null,
+ sampleRate,
});
}
}
diff --git a/packages/convert/app/components/use-probe.ts b/packages/convert/app/components/use-probe.ts
index 95676a5052..98e64a57a0 100644
--- a/packages/convert/app/components/use-probe.ts
+++ b/packages/convert/app/components/use-probe.ts
@@ -57,6 +57,7 @@ export const useProbe = ({
const [keyframes, setKeyframes] = useState<MediaParserKeyframe[] | null>(
null,
);
+ const [sampleRate, setSampleRate] = useState<number | null>(null);
const [images, setImages] = useState<MediaParserEmbeddedImage[] | null>(null);
const [m3u, setM3u] = useState<M3uStream[] | null>(null);
const [done, setDone] = useState(false);
@@ -120,6 +121,9 @@ export const useProbe = ({
onKeyframes: (k) => {
setKeyframes(k);
},
+ onSampleRate(s) {
+ setSampleRate(s);
+ },
onImages: (i) => {
setImages(i);
},
@@ -180,6 +184,7 @@ export const useProbe = ({
images,
m3u,
controller,
+ sampleRate,
};
}, [
tracks,
@@ -202,5 +207,6 @@ export const useProbe = ({
images,
m3u,
controller,
+ sampleRate,
]);
};
diff --git a/packages/convert/app/components/use-supported-configs.ts b/packages/convert/app/components/use-supported-configs.ts
index b08b7fa1ba..5f87562aa2 100644
--- a/packages/convert/app/components/use-supported-configs.ts
+++ b/packages/convert/app/components/use-supported-configs.ts
@@ -15,6 +15,7 @@ export const useSupportedConfigs = ({
userRotation,
inputContainer,
resizeOperation,
+ sampleRate,
}: {
outputContainer: ConvertMediaContainer;
tracks: MediaParserTracks | null;
@@ -22,6 +23,7 @@ export const useSupportedConfigs = ({
userRotation: number;
resizeOperation: ResizeOperation | null;
inputContainer: MediaParserContainer | null;
+ sampleRate: number | null;
}) => {
const [state, setState] = useState<
Record<ConvertMediaContainer, SupportedConfigs | null>
@@ -40,6 +42,7 @@ export const useSupportedConfigs = ({
userRotation,
inputContainer,
resizeOperation,
+ sampleRate,
}).then((supportedConfigs) => {
setState((prev) => ({
...prev,
@@ -53,6 +56,7 @@ export const useSupportedConfigs = ({
tracks,
userRotation,
resizeOperation,
+ sampleRate,
]);
return state[outputContainer];
diff --git a/packages/convert/app/lib/default-ui.ts b/packages/convert/app/lib/default-ui.ts
index db9ddece77..deb7fb4fc6 100644
--- a/packages/convert/app/lib/default-ui.ts
+++ b/packages/convert/app/lib/default-ui.ts
@@ -96,7 +96,12 @@ export const isConvertEnabledByDefault = (action: RouteAction) => {
);
};
-export type ConvertSections = 'convert' | 'rotate' | 'mirror' | 'resize';
+export type ConvertSections =
+ | 'convert'
+ | 'rotate'
+ | 'mirror'
+ | 'resize'
+ | 'resample';
export const getOrderOfSections = (
action: RouteAction,
@@ -107,6 +112,7 @@ export const getOrderOfSections = (
resize: 1,
mirror: 2,
convert: 3,
+ resample: 4,
};
}
@@ -116,6 +122,7 @@ export const getOrderOfSections = (
resize: 1,
rotate: 2,
mirror: 3,
+ resample: 4,
};
}
@@ -125,6 +132,7 @@ export const getOrderOfSections = (
resize: 1,
rotate: 2,
mirror: 3,
+ resample: 4,
};
}
@@ -134,6 +142,7 @@ export const getOrderOfSections = (
resize: 1,
rotate: 2,
convert: 3,
+ resample: 4,
};
}
@@ -147,6 +156,7 @@ export const getOrderOfSections = (
rotate: 1,
mirror: 2,
convert: 3,
+ resample: 4,
};
}
diff --git a/packages/convert/app/tests/test-list.tsx b/packages/convert/app/tests/test-list.tsx
index be75b45330..a8b0d459fb 100644
--- a/packages/convert/app/tests/test-list.tsx
+++ b/packages/convert/app/tests/test-list.tsx
@@ -128,7 +128,12 @@ const aviToMp4ReEncode = (): TestStructure => {
};
}
- return {type: 'reencode', audioCodec: 'aac', bitrate: 128000};
+ return {
+ type: 'reencode',
+ audioCodec: 'aac',
+ bitrate: 128000,
+ sampleRate: null,
+ };
},
});
},
diff --git a/packages/webcodecs/src/default-on-audio-track-handler.ts b/packages/webcodecs/src/default-on-audio-track-handler.ts
index 05b344d938..35ca5bbb5a 100644
--- a/packages/webcodecs/src/default-on-audio-track-handler.ts
+++ b/packages/webcodecs/src/default-on-audio-track-handler.ts
@@ -39,6 +39,7 @@ export const defaultOnAudioTrackHandler: ConvertMediaOnAudioTrackHandler =
audioCodec: defaultAudioCodec,
track,
bitrate,
+ sampleRate: null,
});
if (canReencode) {
diff --git a/packages/webcodecs/src/wav-audio-encoder.ts b/packages/webcodecs/src/wav-audio-encoder.ts
index 6a25348d7d..bf04421696 100644
--- a/packages/webcodecs/src/wav-audio-encoder.ts
+++ b/packages/webcodecs/src/wav-audio-encoder.ts
@@ -22,15 +22,16 @@ export const getWaveAudioEncoder = ({
const audioData = convertAudioData({
audioData: unconvertedAudioData,
newSampleRate: config.sampleRate,
+ format: 's16',
});
+ unconvertedAudioData.close();
const chunk: EncodedAudioChunk = {
timestamp: audioData.timestamp,
duration: audioData.duration,
type: 'key',
- copyTo: (destination) =>
- audioData.copyTo(destination, {planeIndex: 0, format: 's16'}),
- byteLength: audioData.allocationSize({planeIndex: 0, format: 's16'}),
+ copyTo: (destination) => audioData.copyTo(destination, {planeIndex: 0}),
+ byteLength: audioData.allocationSize({planeIndex: 0}),
};
return onChunk(chunk);