Skip to content

🎥 Make videos programmatically with React

only show valid pixel formats for the codec in studio + validate

Jonny Burger3efa8bcd130a
diff --git a/packages/cli/src/editor/components/RenderModal/RenderModal.tsx b/packages/cli/src/editor/components/RenderModal/RenderModal.tsx
index 837838cfed..5dbac64727 100644
--- a/packages/cli/src/editor/components/RenderModal/RenderModal.tsx
+++ b/packages/cli/src/editor/components/RenderModal/RenderModal.tsx
@@ -44,6 +44,7 @@ import {GifIcon} from '../../icons/gif';
import {getDefaultOutLocation} from '../../../get-default-out-name';
import {getDefaultCodecs} from '../../../preview-server/render-queue/get-default-video-contexts';
import {BLUE, BLUE_DISABLED, LIGHT_TEXT} from '../../helpers/colors';
+import {Checkmark} from '../../icons/Checkmark';
import {ModalsContext} from '../../state/modals';
import {SidebarContext} from '../../state/sidebar';
import {Spacing} from '../layout';
@@ -55,6 +56,7 @@ import {
ModalContainer,
} from '../ModalContainer';
import {NewCompHeader} from '../ModalHeader';
+import type {ComboboxValue} from '../NewComposition/ComboBox';
import {
optionsSidebarTabs,
persistSelectedOptionsSidebarPanel,
@@ -403,7 +405,7 @@ const RenderModal: React.FC<
() => initialx264Preset,
);
- const [pixelFormat, setPixelFormat] = useState<PixelFormat>(
+ const [userPreferredPixelFormat, setPixelFormat] = useState<PixelFormat>(
() => initialPixelFormat,
);
const [preferredQualityControlType, setQualityControl] =
@@ -737,6 +739,18 @@ const RenderModal: React.FC<
const audioCodec = deriveFinalAudioCodec(codec, userSelectedAudioCodec);
+ const availablePixelFormats = useMemo(() => {
+ return BrowserSafeApis.validPixelFormatsForCodec(codec);
+ }, [codec]);
+
+ const pixelFormat = useMemo(() => {
+ if (availablePixelFormats.includes(userPreferredPixelFormat)) {
+ return userPreferredPixelFormat;
+ }
+
+ return availablePixelFormats[0];
+ }, [availablePixelFormats, userPreferredPixelFormat]);
+
const onClickVideo = useCallback(() => {
setSidebarCollapsedState({left: null, right: 'expanded'});
persistSelectedOptionsSidebarPanel('renders');
@@ -1082,6 +1096,23 @@ const RenderModal: React.FC<
};
}, [registerKeybinding, trigger]);
+ const pixelFormatOptions = useMemo((): ComboboxValue[] => {
+ return availablePixelFormats.map((option) => {
+ return {
+ label: option,
+ onClick: () => setPixelFormat(option),
+ key: option,
+ id: option,
+ keyHint: null,
+ leftItem: pixelFormat === option ? <Checkmark /> : null,
+ quickSwitcherLabel: null,
+ subMenu: null,
+ type: 'item',
+ value: option,
+ };
+ });
+ }, [availablePixelFormats, pixelFormat]);
+
return (
<div style={outer}>
<NewCompHeader title={`Render ${resolvedComposition.id}`} />
@@ -1204,7 +1235,7 @@ const RenderModal: React.FC<
scale={scale}
setScale={setScale}
pixelFormat={pixelFormat}
- setPixelFormat={setPixelFormat}
+ pixelFormatOptions={pixelFormatOptions}
imageFormatOptions={imageFormatOptions}
crf={crf}
setCrf={setCrf}
diff --git a/packages/cli/src/editor/components/RenderModal/RenderModalPicture.tsx b/packages/cli/src/editor/components/RenderModal/RenderModalPicture.tsx
index 186de9c74d..8b1512bf7c 100644
--- a/packages/cli/src/editor/components/RenderModal/RenderModalPicture.tsx
+++ b/packages/cli/src/editor/components/RenderModal/RenderModalPicture.tsx
@@ -34,7 +34,6 @@ export const RenderModalPicture: React.FC<{
scale: number;
setScale: React.Dispatch<React.SetStateAction<number>>;
pixelFormat: PixelFormat;
- setPixelFormat: React.Dispatch<React.SetStateAction<PixelFormat>>;
colorSpace: ColorSpace;
setColorSpace: React.Dispatch<React.SetStateAction<ColorSpace>>;
imageFormatOptions: SegmentedControlItem[];
@@ -53,12 +52,12 @@ export const RenderModalPicture: React.FC<{
crf: number;
customTargetVideoBitrate: string;
shouldDisplayQualityControlPicker: boolean;
+ pixelFormatOptions: ComboboxValue[];
}> = ({
renderMode,
scale,
setScale,
pixelFormat,
- setPixelFormat,
imageFormatOptions,
setQualityControl,
qualityControlType,
@@ -75,24 +74,8 @@ export const RenderModalPicture: React.FC<{
stillImageFormat,
colorSpace,
setColorSpace,
+ pixelFormatOptions,
}) => {
- const pixelFormatOptions = useMemo((): ComboboxValue[] => {
- return BrowserSafeApis.validPixelFormats.map((option) => {
- return {
- label: option,
- onClick: () => setPixelFormat(option),
- key: option,
- id: option,
- keyHint: null,
- leftItem: pixelFormat === option ? <Checkmark /> : null,
- quickSwitcherLabel: null,
- subMenu: null,
- type: 'item',
- value: option,
- };
- });
- }, [pixelFormat, setPixelFormat]);
-
const colorSpaceOptions = useMemo((): ComboboxValue[] => {
return BrowserSafeApis.validColorSpaces.map((option) => {
return {
diff --git a/packages/renderer/src/client.ts b/packages/renderer/src/client.ts
index 250dab6d77..262b6d5e0d 100644
--- a/packages/renderer/src/client.ts
+++ b/packages/renderer/src/client.ts
@@ -18,7 +18,11 @@ import {isAudioCodec} from './is-audio-codec';
import {allOptions} from './options';
import {validColorSpaces} from './options/color-space';
import {optionsMap} from './options/options-map';
-import {DEFAULT_PIXEL_FORMAT, validPixelFormats} from './pixel-format';
+import {
+ DEFAULT_PIXEL_FORMAT,
+ validPixelFormats,
+ validPixelFormatsForCodec,
+} from './pixel-format';
import {proResProfileOptions} from './prores-profile';
import {validateOutputFilename} from './validate-output-filename';
import {x264PresetOptions} from './x264-preset';
@@ -33,6 +37,7 @@ export const BrowserSafeApis = {
proResProfileOptions,
x264PresetOptions,
validPixelFormats,
+ validPixelFormatsForCodec,
DEFAULT_PIXEL_FORMAT,
supportedAudioCodecs,
defaultFileExtensionMap,
diff --git a/packages/renderer/src/pixel-format.ts b/packages/renderer/src/pixel-format.ts
index a4534a8c55..4d92489a28 100644
--- a/packages/renderer/src/pixel-format.ts
+++ b/packages/renderer/src/pixel-format.ts
@@ -15,6 +15,14 @@ export type PixelFormat = (typeof validPixelFormats)[number];
export const DEFAULT_PIXEL_FORMAT: PixelFormat = 'yuv420p';
+export const validPixelFormatsForCodec = (codec: Codec) => {
+ if (codec === 'vp8' || codec === 'vp9') {
+ return validPixelFormats;
+ }
+
+ return validPixelFormats.filter((format) => format !== 'yuva420p');
+};
+
export const validateSelectedPixelFormatAndCodecCombination = (
pixelFormat: PixelFormat | undefined,
codec: Codec,
@@ -31,9 +39,13 @@ export const validateSelectedPixelFormatAndCodecCombination = (
return;
}
- if (codec !== 'vp8' && codec !== 'vp9') {
+ const validFormats = validPixelFormatsForCodec(codec);
+
+ if (!validFormats.includes(pixelFormat)) {
throw new TypeError(
- "Pixel format was set to 'yuva420p' but codec is not 'vp8' or 'vp9'. To render videos with alpha channel, you must choose a codec that supports it.",
+ `Pixel format was set to 'yuva420p' but codec ${codec} does not support it. Valid pixel formats for codec ${codec} are: ${validFormats.join(
+ ', ',
+ )}.`,
);
}
};
diff --git a/packages/renderer/src/test/pixel-format.test.ts b/packages/renderer/src/test/pixel-format.test.ts
index fe58be1a51..693ada1f3f 100644
--- a/packages/renderer/src/test/pixel-format.test.ts
+++ b/packages/renderer/src/test/pixel-format.test.ts
@@ -25,15 +25,12 @@ describe('pixel-format tests validateSelectedPixelFormatAndCodecCombination', ()
}
}
- const invalidCodecs: Codec[] = ['h264', 'h265'];
- invalidCodecs.forEach((entry) =>
- test(`test for invalid combination yuva420p and ${entry}`, () =>
- expect(() =>
- validateSelectedPixelFormatAndCodecCombination('yuva420p', entry),
- ).toThrow(
- /Pixel format was set to 'yuva420p' but codec is not 'vp8' or 'vp9'. To render videos with alpha channel, you must choose a codec that supports it./,
- )),
- );
+ test(`test for invalid combination yuva420p and h264`, () =>
+ expect(() =>
+ validateSelectedPixelFormatAndCodecCombination('yuva420p', 'h264'),
+ ).toThrow(
+ /Pixel format was set to 'yuva420p' but codec h264 does not support it. Valid pixel formats for codec h264 are: yuv420p, yuv422p, yuv444p, yuv420p10le, yuv422p10le, yuv444p10le, yuva444p10le./g,
+ ));
const validCodecs: Codec[] = ['vp8', 'vp9'];
validCodecs.forEach((c) =>
test(`test for valid combination yuva420p and ${c}`, () =>