import type { ColorSpace, Codec, PixelFormat, X264Preset, } from '@remotion/renderer'; import type {HardwareAccelerationOption} from '@remotion/renderer/client'; import {BrowserSafeApis} from '@remotion/renderer/client'; import type {ChangeEvent} from 'react'; import React, {useCallback, useMemo} from 'react'; import {labelx264Preset} from '../../helpers/presets-labels'; import {Checkmark} from '../../icons/Checkmark'; import {Checkbox} from '../Checkbox'; import {Spacing} from '../layout'; import {VERTICAL_SCROLLBAR_CLASSNAME} from '../Menu/is-menu-item'; import type {ComboboxValue, SelectionItem} from '../NewComposition/ComboBox'; import {Combobox} from '../NewComposition/ComboBox'; import {RemotionInput} from '../NewComposition/RemInput'; import type {SegmentedControlItem} from '../SegmentedControl'; import {SegmentedControl} from '../SegmentedControl'; import {CrfSetting} from './CrfSetting'; import {input, label, optionRow, rightRow} from './layout'; import {NumberSetting} from './NumberSetting'; import {OptionExplainerBubble} from './OptionExplainerBubble'; import type {RenderType} from './RenderModalAdvanced'; import {RenderModalHr} from './RenderModalHr'; const qualityControlModes = ['crf', 'bitrate'] as const; export type QualityControl = (typeof qualityControlModes)[number]; const container: React.CSSProperties = { flex: 1, overflowY: 'auto', }; export const RenderModalEncoding: React.FC<{ readonly renderMode: RenderType; readonly codec: Codec; readonly qualityControlType: QualityControl | null; readonly setQualityControl: React.Dispatch< React.SetStateAction >; readonly shouldDisplayQualityControlPicker: boolean; readonly crf: number | null; readonly minCrf: number; readonly maxCrf: number; readonly setCrf: React.Dispatch>; readonly customTargetVideoBitrate: string; readonly setCustomTargetVideoBitrateValue: React.Dispatch< React.SetStateAction >; readonly encodingBufferSize: string | null; readonly setEncodingBufferSize: React.Dispatch< React.SetStateAction >; readonly encodingMaxRate: string | null; readonly setEncodingMaxRate: React.Dispatch< React.SetStateAction >; readonly pixelFormat: PixelFormat; readonly pixelFormatOptions: ComboboxValue[]; readonly colorSpace: ColorSpace; readonly setColorSpace: React.Dispatch>; readonly x264Preset: X264Preset | null; readonly setx264Preset: React.Dispatch>; readonly gopSize: number | null; readonly setGopSize: React.Dispatch>; readonly hardwareAcceleration: HardwareAccelerationOption; readonly setHardwareAcceleration: React.Dispatch< React.SetStateAction >; readonly disallowParallelEncoding: boolean; readonly setDisallowParallelEncoding: React.Dispatch< React.SetStateAction >; }> = ({ renderMode, codec, qualityControlType, setQualityControl, shouldDisplayQualityControlPicker, crf, minCrf, maxCrf, setCrf, customTargetVideoBitrate, setCustomTargetVideoBitrateValue, encodingBufferSize, setEncodingBufferSize, encodingMaxRate, setEncodingMaxRate, pixelFormat, pixelFormatOptions, colorSpace, setColorSpace, x264Preset, setx264Preset, gopSize, setGopSize, hardwareAcceleration, setHardwareAcceleration, disallowParallelEncoding, setDisallowParallelEncoding, }) => { const colorSpaceOptions = useMemo((): ComboboxValue[] => { return BrowserSafeApis.validColorSpaces.map((option) => { return { label: option, onClick: () => setColorSpace(option), key: option, id: option, keyHint: null, leftItem: colorSpace === option ? : null, quickSwitcherLabel: null, subMenu: null, type: 'item', value: option, }; }); }, [colorSpace, setColorSpace]); const qualityControlOptions = useMemo((): SegmentedControlItem[] => { return qualityControlModes.map((option) => { return { label: option === 'crf' ? 'CRF' : 'Bitrate', onClick: () => setQualityControl(option), key: option, selected: qualityControlType === option, }; }); }, [qualityControlType, setQualityControl]); const x264PresetOptions = useMemo((): ComboboxValue[] => { return BrowserSafeApis.x264PresetOptions.map((option) => { return { label: labelx264Preset(option), onClick: () => setx264Preset(option), key: option, type: 'item', id: option, keyHint: null, leftItem: x264Preset === option ? : null, quickSwitcherLabel: null, subMenu: null, value: option, }; }); }, [setx264Preset, x264Preset]); const hardwareAccelerationValues = useMemo((): ComboboxValue[] => { return BrowserSafeApis.hardwareAccelerationOptions.map( (option): SelectionItem => { return { label: option, onClick: () => setHardwareAcceleration(option), leftItem: hardwareAcceleration === option ? : null, subMenu: null, quickSwitcherLabel: null, type: 'item', id: option, keyHint: null, value: option, }; }, ); }, [hardwareAcceleration, setHardwareAcceleration]); const toggleCustomGopSize = useCallback(() => { setGopSize((previous) => { if (previous === null) { return 120; } return null; }); }, [setGopSize]); const changeGopSize: React.Dispatch> = useCallback( (cb) => { setGopSize((prev) => { if (prev === null) { throw new TypeError('Expected previous value'); } if (typeof cb === 'function') { return cb(prev); } return cb; }); }, [setGopSize], ); const onTargetVideoBitrateChanged: React.ChangeEventHandler = useCallback( (e) => { setCustomTargetVideoBitrateValue(e.target.value); }, [setCustomTargetVideoBitrateValue], ); const onEncodingBufferSizeToggled = useCallback( (e: ChangeEvent) => { setEncodingBufferSize(e.target.checked ? '10000k' : null); }, [setEncodingBufferSize], ); const onEncodingMaxRateToggled = useCallback( (e: ChangeEvent) => { setEncodingMaxRate(e.target.checked ? '5000k' : null); }, [setEncodingMaxRate], ); const onEncodingBufferSizeChanged: React.ChangeEventHandler = useCallback( (e) => { setEncodingBufferSize(e.target.value); }, [setEncodingBufferSize], ); const onEncodingMaxRateChanged: React.ChangeEventHandler = useCallback( (e) => { setEncodingMaxRate(e.target.value); }, [setEncodingMaxRate], ); const onDisallowParallelEncodingChanged = useCallback( (e: ChangeEvent) => { setDisallowParallelEncoding(e.target.checked); }, [setDisallowParallelEncoding], ); return (
{shouldDisplayQualityControlPicker && renderMode === 'video' ? (
Quality control
) : null} {qualityControlType === 'crf' && renderMode === 'video' && crf !== null ? ( ) : null} {qualityControlType === 'bitrate' && renderMode === 'video' ? (
Target video bitrate
) : null} {renderMode === 'video' ? ( <>
{encodingBufferSize === null ? null : (
-bufsize value
)}
Custom FFmpeg -maxrate
{encodingMaxRate === null ? null : (
-maxrate value
)}
Pixel format
Color space
{codec === 'h264' ? (
x264 Preset
) : null} {codec !== 'gif' ? (
Custom GOP size
) : null} {codec !== 'gif' && gopSize !== null ? ( `${value} frames`} onValueChanged={changeGopSize} value={gopSize} /> ) : null}
Hardware acceleration
) : null} {renderMode === 'still' ? null : ( <> {renderMode === 'video' ? : null}
No parallel encoding
)}
); };