Skip to content

🎥 Make videos programmatically with React

track option selection by key, not index

Jonny Burgeread20de20f1e
diff --git a/packages/convert/app/components/AudioCodecSelection.tsx b/packages/convert/app/components/AudioCodecSelection.tsx
index ddd07c8118..dd6caaa3c8 100644
--- a/packages/convert/app/components/AudioCodecSelection.tsx
+++ b/packages/convert/app/components/AudioCodecSelection.tsx
@@ -1,6 +1,7 @@
import {MediaParserAudioCodec} from '@remotion/media-parser';
import {AudioOperation} from '@remotion/webcodecs';
import React from 'react';
+import {getAudioOperationId} from '~/lib/operation-key';
import {AudioCodecDropWarning} from './AudioCodecDropWarning';
import {AudioOperationOption} from './AudioOperationOption';
import {
@@ -14,8 +15,8 @@ import {
export const AudioCodecSelection: React.FC<{
readonly audioTrackOptions: AudioOperation[];
- readonly index: number;
- readonly setIndex: (v: number) => void;
+ readonly index: string;
+ readonly setIndex: (v: string) => void;
readonly currentAudioCodec: MediaParserAudioCodec | null;
}> = ({audioTrackOptions, index, setIndex, currentAudioCodec}) => {
if (!currentAudioCodec) {
@@ -28,8 +29,8 @@ export const AudioCodecSelection: React.FC<{
<>
<Select
disabled={disabled}
- value={String(index)}
- onValueChange={(v) => setIndex(Number(v))}
+ value={index}
+ onValueChange={(v) => setIndex(v)}
>
<SelectTrigger id="audioCodec">
<SelectValue placeholder="Select a audio codec" />
@@ -39,8 +40,8 @@ export const AudioCodecSelection: React.FC<{
return (
<SelectGroup key={i}>
<SelectItem
- // eslint-disable-next-line react/jsx-key
- value={String(i)}
+ key={getAudioOperationId(operation)}
+ value={getAudioOperationId(operation)}
>
<AudioOperationOption
currentAudioCodec={currentAudioCodec}
diff --git a/packages/convert/app/components/ConvertForm.tsx b/packages/convert/app/components/ConvertForm.tsx
index 39824187d6..a046f2c0ea 100644
--- a/packages/convert/app/components/ConvertForm.tsx
+++ b/packages/convert/app/components/ConvertForm.tsx
@@ -7,6 +7,7 @@ import {
getAvailableContainers,
} from '@remotion/webcodecs';
import React from 'react';
+import {getAudioOperationId, getVideoOperationId} from '~/lib/operation-key';
import {renderHumanReadableContainer} from '~/lib/render-codec-label';
import {AudioCodecSelection} from './AudioCodecSelection';
import {SupportedConfigs} from './get-supported-configs';
@@ -29,10 +30,10 @@ export const ConvertForm: React.FC<{
React.SetStateAction<ConvertMediaContainer>
>;
readonly supportedConfigs: SupportedConfigs | null;
- readonly videoConfigIndexSelection: Record<number, number>;
- readonly audioConfigIndexSelection: Record<number, number>;
- readonly setAudioConfigIndex: (trackId: number, i: number) => void;
- readonly setVideoConfigIndex: (trackId: number, i: number) => void;
+ readonly videoConfigIndexSelection: Record<number, string>;
+ readonly audioConfigIndexSelection: Record<number, string>;
+ readonly setAudioConfigIndex: (trackId: number, key: string) => void;
+ readonly setVideoConfigIndex: (trackId: number, key: string) => void;
readonly currentAudioCodec: MediaParserAudioCodec | null;
readonly currentVideoCodec: MediaParserVideoCodec | null;
}> = ({
@@ -79,7 +80,13 @@ export const ConvertForm: React.FC<{
totalVideoTracks={supportedConfigs.videoTrackOptions.length}
/>
<VideoCodecSelection
- index={videoConfigIndexSelection[track.trackId] ?? 0}
+ index={getVideoOperationId(
+ track.operations.find(
+ (o) =>
+ getVideoOperationId(o) ===
+ videoConfigIndexSelection[track.trackId],
+ ) ?? track.operations[0],
+ )}
setIndex={(i) => {
setVideoConfigIndex(track.trackId, i);
}}
@@ -101,7 +108,13 @@ export const ConvertForm: React.FC<{
totalAudioTracks={supportedConfigs.audioTrackOptions.length}
/>
<AudioCodecSelection
- index={audioConfigIndexSelection[track.trackId] ?? 0}
+ index={getAudioOperationId(
+ track.operations.find(
+ (o) =>
+ getAudioOperationId(o) ===
+ audioConfigIndexSelection[track.trackId],
+ ) ?? track.operations[0],
+ )}
setIndex={(i) => {
setAudioConfigIndex(track.trackId, i);
}}
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index be278de1f4..94c49717dc 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -27,7 +27,7 @@ import {
} from '~/lib/default-ui';
import {
getActualAudioConfigIndex,
- getActualVideoConfigIndex,
+ getActualVideoOperation,
} from '~/lib/get-audio-video-config-index';
import {getInitialResizeSuggestion} from '~/lib/get-initial-resize-suggestion';
import {isReencoding} from '~/lib/is-reencoding';
@@ -96,11 +96,11 @@ export default function ConvertUI({
const [outputContainer, setContainer] = useState<ConvertMediaContainer>(() =>
getDefaultContainerForConversion(src, action),
);
- const [videoConfigIndexSelection, _setVideoConfigIndex] = useState<
- Record<number, number>
+ const [videoOperationSelection, setVideoOperationKey] = useState<
+ Record<number, string>
>({});
- const [audioConfigIndexSelection, _setAudioConfigIndex] = useState<
- Record<number, number>
+ const [audioOperationSelection, setAudioOperationKey] = useState<
+ Record<number, string>
>({});
const [state, setState] = useState<ConvertState>({type: 'idle'});
const [name, setName] = useState<string | null>(null);
@@ -132,28 +132,26 @@ export default function ConvertUI({
});
const isH264Reencode = supportedConfigs?.videoTrackOptions.some((o) => {
- const index = getActualVideoConfigIndex({
+ const operation = getActualVideoOperation({
enableConvert,
trackNumber: o.trackId,
- videoConfigIndexSelection,
+ videoConfigIndexSelection: videoOperationSelection,
+ operations: o.operations,
});
- return (
- o.operations[index].type === 'reencode' &&
- o.operations[index].videoCodec === 'h264'
- );
+ return operation.type === 'reencode' && operation.videoCodec === 'h264';
});
- const setVideoConfigIndex = useCallback((trackId: number, i: number) => {
- _setVideoConfigIndex((prev) => ({
+ const setVideoConfigIndex = useCallback((trackId: number, key: string) => {
+ setVideoOperationKey((prev) => ({
...prev,
- [trackId]: i,
+ [trackId]: key,
}));
}, []);
- const setAudioConfigIndex = useCallback((trackId: number, i: number) => {
- _setAudioConfigIndex((prev) => ({
+ const setAudioConfigIndex = useCallback((trackId: number, key: string) => {
+ setAudioOperationKey((prev) => ({
...prev,
- [trackId]: i,
+ [trackId]: key,
}));
}, []);
@@ -209,17 +207,13 @@ export default function ConvertUI({
throw new Error('Found no options for audio track');
}
- const configIndex = getActualAudioConfigIndex({
+ const operation = getActualAudioConfigIndex({
enableConvert,
- audioConfigIndexSelection,
+ audioConfigIndexSelection: audioOperationSelection,
trackNumber: track.trackId,
+ operations: options.operations,
});
- const operation = options.operations[configIndex ?? 0];
- if (!operation) {
- throw new Error('Found no operation');
- }
-
MediaParserInternals.Log.info(
'info',
`Selected operation for audio track ${track.trackId}`,
@@ -236,16 +230,13 @@ export default function ConvertUI({
throw new Error('Found no options for video track');
}
- const configIndex = getActualVideoConfigIndex({
+ const operation = getActualVideoOperation({
enableConvert,
- videoConfigIndexSelection,
+ videoConfigIndexSelection: videoOperationSelection,
trackNumber: track.trackId,
+ operations: options.operations,
});
- const operation = options.operations[configIndex ?? 0];
- if (!operation) {
- throw new Error('Found no operation');
- }
MediaParserInternals.Log.info(
'info',
`Selected operation for video track ${track.trackId}`,
@@ -286,8 +277,8 @@ export default function ConvertUI({
flipVertical,
supportedConfigs,
enableConvert,
- audioConfigIndexSelection,
- videoConfigIndexSelection,
+ audioOperationSelection,
+ videoOperationSelection,
]);
const cancel = useCallback(() => {
@@ -383,7 +374,7 @@ export default function ConvertUI({
supportedConfigs !== null &&
isReencoding({
supportedConfigs,
- videoConfigIndexSelection,
+ videoConfigIndexSelection: videoOperationSelection,
enableConvert,
})
}
@@ -409,7 +400,7 @@ export default function ConvertUI({
supportedConfigs !== null &&
isReencoding({
supportedConfigs,
- videoConfigIndexSelection,
+ videoConfigIndexSelection: videoOperationSelection,
enableConvert,
})
}
@@ -423,16 +414,16 @@ export default function ConvertUI({
}
const disableSubmit = isSubmitDisabled({
- audioConfigIndexSelection,
+ audioConfigIndexSelection: audioOperationSelection,
supportedConfigs,
- videoConfigIndexSelection,
+ videoConfigIndexSelection: videoOperationSelection,
enableConvert,
enableRotateOrMirror,
});
const canPixelManipulate = canRotateOrMirror({
supportedConfigs,
- videoConfigIndexSelection,
+ videoConfigIndexSelection: videoOperationSelection,
enableConvert,
});
@@ -461,8 +452,8 @@ export default function ConvertUI({
setFlipHorizontal,
setFlipVertical,
supportedConfigs,
- audioConfigIndexSelection,
- videoConfigIndexSelection,
+ audioConfigIndexSelection: audioOperationSelection,
+ videoConfigIndexSelection: videoOperationSelection,
setAudioConfigIndex,
setVideoConfigIndex,
currentAudioCodec,
diff --git a/packages/convert/app/components/VideoCodecSelection.tsx b/packages/convert/app/components/VideoCodecSelection.tsx
index 1675158535..6873bcf720 100644
--- a/packages/convert/app/components/VideoCodecSelection.tsx
+++ b/packages/convert/app/components/VideoCodecSelection.tsx
@@ -1,6 +1,7 @@
import {MediaParserVideoCodec} from '@remotion/media-parser';
import {VideoOperation} from '@remotion/webcodecs';
import React from 'react';
+import {getVideoOperationId} from '~/lib/operation-key';
import {
Select,
SelectContent,
@@ -13,15 +14,15 @@ import {VideoOperationOption} from './VideoOperationOption';
export const VideoCodecSelection: React.FC<{
readonly videoOperations: VideoOperation[];
- readonly index: number;
- readonly setIndex: (v: number) => void;
+ readonly index: string;
+ readonly setIndex: (v: string) => void;
readonly currentVideoCodec: MediaParserVideoCodec | null;
}> = ({videoOperations, index, setIndex, currentVideoCodec}) => {
return (
<Select
disabled={videoOperations.length < 2}
- value={String(index)}
- onValueChange={(v) => setIndex(Number(v))}
+ value={index}
+ onValueChange={(v) => setIndex(v)}
>
<SelectTrigger id="videoCodec">
<SelectValue placeholder="Select a video codec" />
@@ -31,8 +32,8 @@ export const VideoCodecSelection: React.FC<{
return (
<SelectGroup key={i}>
<SelectItem
- // eslint-disable-next-line react/jsx-key
- value={String(i)}
+ key={getVideoOperationId(operation)}
+ value={getVideoOperationId(operation)}
>
<VideoOperationOption
currentVideoCodec={currentVideoCodec}
diff --git a/packages/convert/app/lib/can-rotate-or-mirror.ts b/packages/convert/app/lib/can-rotate-or-mirror.ts
index a5cc653ff6..b6f8b14546 100644
--- a/packages/convert/app/lib/can-rotate-or-mirror.ts
+++ b/packages/convert/app/lib/can-rotate-or-mirror.ts
@@ -7,7 +7,7 @@ export const canRotateOrMirror = ({
enableConvert,
}: {
supportedConfigs: SupportedConfigs | null;
- videoConfigIndexSelection: Record<number, number>;
+ videoConfigIndexSelection: Record<number, string>;
enableConvert: boolean;
}) => {
return (
diff --git a/packages/convert/app/lib/get-audio-video-config-index.ts b/packages/convert/app/lib/get-audio-video-config-index.ts
index 2b94257db6..b1348e9020 100644
--- a/packages/convert/app/lib/get-audio-video-config-index.ts
+++ b/packages/convert/app/lib/get-audio-video-config-index.ts
@@ -1,31 +1,46 @@
+import {AudioOperation, VideoOperation} from '@remotion/webcodecs';
+import {getAudioOperationId, getVideoOperationId} from './operation-key';
+
export const getActualAudioConfigIndex = ({
audioConfigIndexSelection,
enableConvert,
trackNumber,
+ operations,
}: {
- audioConfigIndexSelection: Record<number, number>;
+ audioConfigIndexSelection: Record<number, string>;
enableConvert: boolean;
trackNumber: number;
+ operations: AudioOperation[];
}) => {
if (!enableConvert) {
- return 0;
+ return operations[0];
}
- return audioConfigIndexSelection[trackNumber] ?? 0;
+ return (
+ operations.find(
+ (o) => getAudioOperationId(o) === audioConfigIndexSelection[trackNumber],
+ ) ?? operations[0]
+ );
};
-export const getActualVideoConfigIndex = ({
+export const getActualVideoOperation = ({
videoConfigIndexSelection,
enableConvert,
trackNumber,
+ operations,
}: {
- videoConfigIndexSelection: Record<number, number>;
+ videoConfigIndexSelection: Record<number, string>;
enableConvert: boolean;
trackNumber: number;
+ operations: VideoOperation[];
}) => {
if (!enableConvert) {
- return 0;
+ return operations[0];
}
- return videoConfigIndexSelection[trackNumber] ?? 0;
+ return (
+ operations.find(
+ (o) => getVideoOperationId(o) === videoConfigIndexSelection[trackNumber],
+ ) ?? operations[0]
+ );
};
diff --git a/packages/convert/app/lib/is-reencoding.ts b/packages/convert/app/lib/is-reencoding.ts
index e2c34b17fd..0672d2cdc0 100644
--- a/packages/convert/app/lib/is-reencoding.ts
+++ b/packages/convert/app/lib/is-reencoding.ts
@@ -1,7 +1,7 @@
import {SupportedConfigs} from '~/components/get-supported-configs';
import {
getActualAudioConfigIndex,
- getActualVideoConfigIndex,
+ getActualVideoOperation,
} from './get-audio-video-config-index';
export const isReencoding = ({
@@ -10,16 +10,17 @@ export const isReencoding = ({
enableConvert,
}: {
supportedConfigs: SupportedConfigs;
- videoConfigIndexSelection: Record<number, number>;
+ videoConfigIndexSelection: Record<number, string>;
enableConvert: boolean;
}) => {
return supportedConfigs.videoTrackOptions.every((o) => {
- const index = getActualVideoConfigIndex({
+ const operation = getActualVideoOperation({
enableConvert,
trackNumber: o.trackId,
videoConfigIndexSelection,
+ operations: o.operations,
});
- return o.operations[index].type === 'reencode';
+ return operation.type === 'reencode';
});
};
@@ -30,26 +31,28 @@ export const isDroppingEverything = ({
enableConvert,
}: {
supportedConfigs: SupportedConfigs;
- audioConfigIndexSelection: Record<number, number>;
- videoConfigIndexSelection: Record<number, number>;
+ audioConfigIndexSelection: Record<number, string>;
+ videoConfigIndexSelection: Record<number, string>;
enableConvert: boolean;
}) => {
return (
supportedConfigs.audioTrackOptions.every((o) => {
- const index = getActualAudioConfigIndex({
+ const operation = getActualAudioConfigIndex({
audioConfigIndexSelection,
enableConvert,
trackNumber: o.trackId,
+ operations: o.operations,
});
- return o.operations[index].type === 'drop';
+ return operation.type === 'drop';
}) &&
supportedConfigs.videoTrackOptions.every((o) => {
- const index = getActualVideoConfigIndex({
+ const operation = getActualVideoOperation({
enableConvert,
trackNumber: o.trackId,
videoConfigIndexSelection,
+ operations: o.operations,
});
- return o.operations[index].type === 'drop';
+ return operation.type === 'drop';
})
);
};
diff --git a/packages/convert/app/lib/is-submit-enabled.ts b/packages/convert/app/lib/is-submit-enabled.ts
index 460ce789c3..6a992367cf 100644
--- a/packages/convert/app/lib/is-submit-enabled.ts
+++ b/packages/convert/app/lib/is-submit-enabled.ts
@@ -11,8 +11,8 @@ export const isSubmitDisabled = ({
enableRotateOrMirror,
}: {
supportedConfigs: SupportedConfigs | null;
- audioConfigIndexSelection: Record<number, number>;
- videoConfigIndexSelection: Record<number, number>;
+ audioConfigIndexSelection: Record<number, string>;
+ videoConfigIndexSelection: Record<number, string>;
enableConvert: boolean;
enableRotateOrMirror: RotateOrMirrorState;
}) => {
diff --git a/packages/convert/app/lib/operation-key.ts b/packages/convert/app/lib/operation-key.ts
new file mode 100644
index 0000000000..b27a48bf76
--- /dev/null
+++ b/packages/convert/app/lib/operation-key.ts
@@ -0,0 +1,45 @@
+import {AudioOperation, VideoOperation} from '@remotion/webcodecs';
+
+export const getVideoOperationId = (operation: VideoOperation) => {
+ if (operation.type === 'copy') {
+ return 'copy';
+ }
+
+ if (operation.type === 'drop') {
+ return 'drop';
+ }
+
+ if (operation.type === 'reencode') {
+ return `reencode-${operation.videoCodec}-${operation.rotate}-${operation.resize}`;
+ }
+
+ if (operation.type === 'fail') {
+ return 'fail';
+ }
+
+ throw new Error(
+ 'Unknown operation type ' + JSON.stringify(operation satisfies never),
+ );
+};
+
+export const getAudioOperationId = (operation: AudioOperation) => {
+ if (operation.type === 'copy') {
+ return 'copy';
+ }
+
+ if (operation.type === 'drop') {
+ return 'drop';
+ }
+
+ if (operation.type === 'reencode') {
+ return `reencode-${operation.audioCodec}-${operation.bitrate}`;
+ }
+
+ if (operation.type === 'fail') {
+ return 'fail';
+ }
+
+ throw new Error(
+ 'Unknown operation type ' + JSON.stringify(operation satisfies never),
+ );
+};