🎥 Make videos programmatically with React
further work on prores
Jonny Burger218274f0c569
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.tsindex 56ec6c232d..cd27269deb 100644--- a/packages/cli/src/get-cli-options.ts+++ b/packages/cli/src/get-cli-options.ts@@ -120,6 +120,14 @@ const getAndValidatePixelFormat = (codec: Codec) => {return pixelFormat;};+const getAndValidateProResProfile = (actualCodec: Codec) => {+ const proResProfile = Internals.getProResProfile();+ Internals.validateSelectedCodecAndProResCombination(+ actualCodec,+ proResProfile+ );+};+const getAndValidateImageFormat = ({shouldOutputImageSequence,codec,@@ -167,7 +175,7 @@ export const getCliOptions = async () => {codec,pixelFormat,});- const profile = getProfile();+ const proResProfile = getAndValidateProResProfile(codec);return {parallelism: Internals.getConcurrency(),@@ -183,6 +191,6 @@ export const getCliOptions = async () => {crf,pixelFormat,imageFormat,- profile+ proResProfile,};};diff --git a/packages/cli/src/get-filename.ts b/packages/cli/src/get-filename.tsindex ff43c4e4bc..53eb18b281 100644--- a/packages/cli/src/get-filename.ts+++ b/packages/cli/src/get-filename.ts@@ -5,6 +5,9 @@ import {getUserPassedOutputLocation,} from './user-passed-output-location';+// TODO: Validate ProRes++// eslint-disable-next-line complexityexport const getOutputFilename = (codec: Codec,imageSequence: booleandiff --git a/packages/core/src/config/codec.ts b/packages/core/src/config/codec.tsindex 481577675c..05e61ac606 100644--- a/packages/core/src/config/codec.ts+++ b/packages/core/src/config/codec.ts@@ -6,24 +6,19 @@ const validCodecs = ['mp3','aac','wav',- 'prores'+ 'prores',] as const;export type Codec = typeof validCodecs[number];export type CodecOrUndefined = Codec | undefined;-export type ProfileOrUndefined = string | undefined;const validLegacyFormats = ['mp4', 'png-sequence'] as const;type LegacyFormat = typeof validLegacyFormats[number];let codec: CodecOrUndefined;-let profile: ProfileOrUndefined;export const getOutputCodecOrUndefined = (): CodecOrUndefined => {return codec;};-export const getOutputProfileOrUndefined = (): ProfileOrUndefined => {- return profile;-}export const DEFAULT_CODEC: Codec = 'h264';@@ -142,7 +137,3 @@ export const setCodec = (newCodec: CodecOrUndefined) => {codec = newCodec;};--export const setProfile = (newProfile: ProfileOrUndefined) => {- profile = newProfile;-}diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.tsindex f0c2370189..5cf52d5aa3 100644--- a/packages/core/src/config/index.ts+++ b/packages/core/src/config/index.ts@@ -16,6 +16,7 @@ import {import {setOverwriteOutput} from './overwrite';import {PixelFormat, setPixelFormat} from './pixel-format';import {setPort} from './preview-server';+import {setProResProfile} from './prores-profile';import {setQuality} from './quality';import {setWebpackCaching} from './webpack-caching';@@ -118,6 +119,12 @@ export const Config = {* Set to true if don't want a video but an image sequence as the output.*/setImageSequence,+ /**+ * Set the ProRes profile.+ * This method is only valid if the codec has been set to 'ProRes'.+ * Possible values: 4444-xq, 4444, hq, standard, light, proxy+ */+ setProResProfile,},} as const;diff --git a/packages/core/src/config/prores-profile.ts b/packages/core/src/config/prores-profile.tsnew file mode 100644index 0000000000..bd19309dd3--- /dev/null+++ b/packages/core/src/config/prores-profile.ts@@ -0,0 +1,44 @@+import {Codec} from './codec';++const proResProfileOptions = [+ '4444-xq',+ '4444',+ 'hq',+ 'standard',+ 'light',+ 'proxy',+] as const;++export type ProResProfile = typeof proResProfileOptions[number];++let proResProfile: ProResProfile | undefined;++export const getProResProfile = (): ProResProfile | undefined => {+ return proResProfile;+};++export const setProResProfile = (profile: ProResProfile | undefined) => {+ proResProfile = profile;+};++export const validateSelectedCodecAndProResCombination = (+ actualCodec: Codec,+ actualProResProfile: ProResProfile | undefined+) => {+ if (typeof actualProResProfile !== 'undefined' && actualCodec !== 'prores') {+ throw new TypeError(+ 'You have set a ProRes profile but the codec is not "prores". Set the codec to "prores" or remove the ProRes profile.'+ );+ }++ if (+ actualProResProfile !== undefined &&+ !proResProfileOptions.includes(actualProResProfile)+ ) {+ throw new TypeError(+ `The ProRes profile "${actualProResProfile}" is not valid. Valid options are ${proResProfileOptions+ .map((p) => `"${p}"`)+ .join(', ')}`+ );+ }+};diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex 66f2c7d5f4..be4def0451 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -46,6 +46,12 @@ import {validateSelectedPixelFormatAndCodecCombination,} from './config/pixel-format';import {getServerPort} from './config/preview-server';+import {+ getProResProfile,+ ProResProfile,+ setProResProfile,+ validateSelectedCodecAndProResCombination,+} from './config/prores-profile';import {getQuality} from './config/quality';import {DEFAULT_WEBPACK_CACHE_ENABLED,@@ -154,6 +160,9 @@ export const Internals = {validateFps,validateDimension,getRemotionEnvironment,+ getProResProfile,+ setProResProfile,+ validateSelectedCodecAndProResCombination,};export type {@@ -172,4 +181,5 @@ export type {SetMediaVolumeContextValue,LooseAnyComponent,RemotionEnvironment,+ ProResProfile,};diff --git a/packages/core/src/test/pro-res-profile.test.ts b/packages/core/src/test/pro-res-profile.test.tsnew file mode 100644index 0000000000..2238299dee--- /dev/null+++ b/packages/core/src/test/pro-res-profile.test.ts@@ -0,0 +1,37 @@+import {+ getProResProfile,+ ProResProfile,+ setProResProfile,+ validateSelectedCodecAndProResCombination,+} from '../config/prores-profile';+import {expectToThrow} from './expect-to-throw';++describe('Pro Res Profile', () => {+ const validProResProfile: ProResProfile = 'standard';++ test('Should be able to set a ProRes profile', () => {+ setProResProfile(validProResProfile);+ expect(getProResProfile()).toBe(validProResProfile);+ });++ test("Just a ProRes profile is not enough, because codec doesn't default to ProRes", () => {+ expectToThrow(() => {+ validateSelectedCodecAndProResCombination('aac', '4444-xq');+ }, /You have set a ProRes profile but the codec is not "prores". Set the codec to "prores" or remove the ProRes profile./);+ });++ test('Should accept a valid codec and ProRes combination', () => {+ expect(validateSelectedCodecAndProResCombination('prores', '4444')).toBe(+ undefined+ );+ });++ test('Should throw on invalid ProRes name', () => {+ expectToThrow(+ () =>+ // @ts-expect-error+ validateSelectedCodecAndProResCombination('prores', 'typoed'),+ /The ProRes profile "typoed" is not valid. Valid options are "4444-xq", "4444", "hq", "standard", "light", "proxy"/+ );+ });+});diff --git a/packages/renderer/src/ffmpeg-flags.ts b/packages/renderer/src/ffmpeg-flags.tsindex e8d516b989..bae9b719af 100644--- a/packages/renderer/src/ffmpeg-flags.ts+++ b/packages/renderer/src/ffmpeg-flags.ts@@ -15,6 +15,7 @@ export const getFfmpegBuildInfo = async () => {return buildConfig;};+// TODO: Check for ProResexport const ffmpegHasFeature = async (feature: 'enable-gpl' | 'enable-libx265' | 'enable-libvpx') => {diff --git a/packages/renderer/src/get-audio-codec-name.ts b/packages/renderer/src/get-audio-codec-name.tsnew file mode 100644index 0000000000..14aa7d44f5--- /dev/null+++ b/packages/renderer/src/get-audio-codec-name.ts@@ -0,0 +1,17 @@+import {Codec, Internals} from 'remotion';++export const getAudioCodecName = (codec: Codec): string | null => {+ if (!Internals.isAudioCodec(codec)) {+ return 'aac';+ }++ if (codec === 'aac') {+ return 'aac';+ }++ if (codec === 'mp3') {+ return 'libmp3lame';+ }++ return null;+};diff --git a/packages/renderer/src/get-codec-name.ts b/packages/renderer/src/get-codec-name.tsnew file mode 100644index 0000000000..d6b1da92b3--- /dev/null+++ b/packages/renderer/src/get-codec-name.ts@@ -0,0 +1,29 @@+import {Codec, Internals} from 'remotion';++export const getCodecName = (codec: Codec): string | null => {+ if (Internals.isAudioCodec(codec)) {+ return null;+ }++ if (codec === 'h264') {+ return 'libx264';+ }++ if (codec === 'h265') {+ return 'libx265';+ }++ if (codec === 'vp8') {+ return 'libvpx';+ }++ if (codec === 'vp9') {+ return 'libvpx-vp9';+ }++ if (codec === 'prores') {+ return 'prores_ks';+ }++ throw new TypeError(`Cannot find FFMPEG codec for ${codec}`);+};diff --git a/packages/renderer/src/get-pro-res-profile-name.ts b/packages/renderer/src/get-pro-res-profile-name.tsnew file mode 100644index 0000000000..6801fa0a96--- /dev/null+++ b/packages/renderer/src/get-pro-res-profile-name.ts@@ -0,0 +1,22 @@+import {ProResProfile} from 'remotion';++export const getProResProfileName = (+ proResProfile: ProResProfile | undefined+): string | null => {+ switch (proResProfile) {+ case '4444-xq':+ return '5';+ case '4444':+ return '4';+ case 'hq':+ return '3';+ case 'standard':+ return '2';+ case 'light':+ return '1';+ case 'proxy':+ return '0';+ default:+ return null;+ }+};diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.tsindex 17485b6252..a925c9bd28 100644--- a/packages/renderer/src/stitcher.ts+++ b/packages/renderer/src/stitcher.ts@@ -4,6 +4,7 @@ import {ImageFormat,Internals,PixelFormat,+ ProResProfile,RenderAssetInfo,} from 'remotion';import {calculateAssetPositions} from './assets/calculate-asset-positions';@@ -12,56 +13,15 @@ import {markAllAssetsAsDownloaded} from './assets/download-and-map-assets-to-filimport {getAssetAudioDetails} from './assets/get-asset-audio-details';import {calculateFfmpegFilters} from './calculate-ffmpeg-filters';import {createFfmpegComplexFilter} from './create-ffmpeg-complex-filter';+import {getAudioCodecName} from './get-audio-codec-name';+import {getCodecName} from './get-codec-name';import {getFrameInfo} from './get-frame-number-length';+import {getProResProfileName} from './get-pro-res-profile-name';import {DEFAULT_IMAGE_FORMAT} from './image-format';import {parseFfmpegProgress} from './parse-ffmpeg-progress';import {resolveAssetSrc} from './resolve-asset-src';import {validateFfmpeg} from './validate-ffmpeg';-const getCodecName = (codec: Codec): string | null => {- if (Internals.isAudioCodec(codec)) {- return null;- }-- if (codec === 'h264') {- return 'libx264';- }-- if (codec === 'h265') {- return 'libx265';- }-- if (codec === 'vp8') {- return 'libvpx';- }-- if (codec === 'vp9') {- return 'libvpx-vp9';- }-- if (codec === 'prores') {- return 'prores_ks'- }-- throw new TypeError(`Cannot find FFMPEG codec for ${codec}`);-};--const getAudioCodecName = (codec: Codec): string | null => {- if (!Internals.isAudioCodec(codec)) {- return 'aac';- }-- if (codec === 'aac') {- return 'aac';- }-- if (codec === 'mp3') {- return 'libmp3lame';- }-- return null;-};-// eslint-disable-next-line complexityexport const stitchFramesToVideo = async (options: {dir: string;@@ -75,12 +35,12 @@ export const stitchFramesToVideo = async (options: {imageFormat?: ImageFormat;pixelFormat?: PixelFormat;codec?: Codec;- profile?: string;crf?: number;// TODO: Do we want a parallelism flag for stitcher?parallelism?: number | null;onProgress?: (progress: number) => void;onDownload?: (src: string) => void;+ proResProfile?: ProResProfile;verbose?: boolean;}): Promise<void> => {const codec = options.codec ?? Internals.DEFAULT_CODEC;@@ -91,6 +51,7 @@ export const stitchFramesToVideo = async (options: {const encoderName = getCodecName(codec);const audioCodecName = getAudioCodecName(codec);+ const proResProfileName = getProResProfileName(options.proResProfile);const isAudioOnly = encoderName === null;if (options.verbose) {@@ -167,6 +128,7 @@ export const stitchFramesToVideo = async (options: {['-c:v', encoderName]: // If only exporting audio, we drop the video explicitly['-vn'],+ proResProfileName ? ['-profile:v', proResProfileName] : null,isAudioOnly ? null : ['-crf', String(crf)],isAudioOnly ? null : ['-pix_fmt', pixelFormat],@@ -179,7 +141,6 @@ export const stitchFramesToVideo = async (options: {// Ignore audio from image sequenceisAudioOnly ? null : ['-map', '0:v'],options.force ? '-y' : null,- options.profile ? ['-profile:v', options.profile] : null,options.outputLocation,];diff --git a/tests/rendering.test.ts b/tests/rendering.test.tsindex 5079dc64a2..849421a2fc 100644--- a/tests/rendering.test.ts+++ b/tests/rendering.test.ts@@ -283,3 +283,5 @@ test("Dynamic duration should work", async () => {fs.unlinkSync(outputPath);}});++test.todo("ProRes rendering");