🎥 Make videos programmatically with React
refactor ffmpeg options
Jonny Burger9e95040f61a6
diff --git a/packages/renderer/src/ffmpeg-args.ts b/packages/renderer/src/ffmpeg-args.tsnew file mode 100644index 0000000000..52219f3ec9--- /dev/null+++ b/packages/renderer/src/ffmpeg-args.ts@@ -0,0 +1,86 @@+import type {Codec} from './codec';+import {validateQualitySettings} from './crf';+import {getCodecName} from './get-codec-name';+import type {PixelFormat} from './pixel-format';+import {truthy} from './truthy';+import type {X264Preset} from './x264-preset';++const firstEncodingStepOnly = ({+ hasPreencoded,+ proResProfileName,+ pixelFormat,+ x264Preset,+ codec,+ crf,+ videoBitrate,+}: {+ hasPreencoded: boolean;+ proResProfileName: string | null;+ pixelFormat: PixelFormat;+ x264Preset: X264Preset | null;+ crf: unknown;+ codec: Codec;+ videoBitrate: string | null | undefined;+}): string[][] => {+ if (hasPreencoded) {+ return [];+ }++ return [+ proResProfileName ? ['-profile:v', proResProfileName] : null,+ ['-pix_fmt', pixelFormat],++ // Without explicitly disabling auto-alt-ref,+ // transparent WebM generation doesn't work+ pixelFormat === 'yuva420p' ? ['-auto-alt-ref', '0'] : null,+ x264Preset ? ['-preset', x264Preset] : null,+ validateQualitySettings({+ crf,+ videoBitrate,+ codec,+ }),+ ].filter(truthy);+};++export const generateFfmpegArgs = ({+ hasPreencoded,+ proResProfileName,+ pixelFormat,+ x264Preset,+ codec,+ crf,+ videoBitrate,+}: {+ hasPreencoded: boolean;+ proResProfileName: string | null;+ pixelFormat: PixelFormat;+ x264Preset: X264Preset | null;+ crf: unknown;+ codec: Codec;+ videoBitrate: string | null | undefined;+}): string[][] => {+ const encoderName = getCodecName(codec);++ if (encoderName === null) {+ throw new TypeError('encoderName is null: ' + JSON.stringify(codec));+ }++ return [+ // -c:v is the same as -vcodec as -codec:video+ // and specified the video codec.+ ['-c:v', encoderName],+ ['-colorspace:v', 'bt709'],+ ['-color_primaries:v', 'bt709'],+ ['-color_trc:v', 'bt709'],+ ['-color_range:v', 'tv'],+ ...firstEncodingStepOnly({+ codec,+ crf,+ hasPreencoded,+ pixelFormat,+ proResProfileName,+ videoBitrate,+ x264Preset,+ }),+ ].filter(truthy);+};diff --git a/packages/renderer/src/prespawn-ffmpeg.ts b/packages/renderer/src/prespawn-ffmpeg.tsindex bc365e7d51..778aaad1c5 100644--- a/packages/renderer/src/prespawn-ffmpeg.ts+++ b/packages/renderer/src/prespawn-ffmpeg.ts@@ -1,9 +1,8 @@import {callFf} from './call-ffmpeg';import type {Codec} from './codec';import {DEFAULT_CODEC} from './codec';-import {validateQualitySettings} from './crf';+import {generateFfmpegArgs} from './ffmpeg-args';import type {FfmpegOverrideFn} from './ffmpeg-override';-import {getCodecName} from './get-codec-name';import {getProResProfileName} from './get-prores-profile-name';import type {VideoImageFormat} from './image-format';import type {LogLevel} from './log-level';@@ -18,6 +17,7 @@ import {import type {ProResProfile} from './prores-profile';import {validateDimension, validateFps} from './validate';import {validateEvenDimensionsWithCodec} from './validate-even-dimensions-with-codec';+import type {X264Preset} from './x264-preset';type RunningStatus =| {@@ -41,6 +41,7 @@ type PreStitcherOptions = {pixelFormat: PixelFormat | undefined;codec: Codec | undefined;crf: number | null | undefined;+ x264Preset: X264Preset | null;onProgress: (progress: number) => void;proResProfile: ProResProfile | undefined;logLevel: LogLevel;@@ -72,13 +73,8 @@ export const prespawnFfmpeg = (options: PreStitcherOptions) => {});const pixelFormat = options.pixelFormat ?? DEFAULT_PIXEL_FORMAT;- const encoderName = getCodecName(codec);const proResProfileName = getProResProfileName(codec, options.proResProfile);- if (encoderName === null) {- throw new TypeError('encoderName is null: ' + JSON.stringify(options));- }-validateSelectedPixelFormatAndCodecCombination(pixelFormat, codec);const ffmpegArgs = [@@ -91,19 +87,14 @@ export const prespawnFfmpeg = (options: PreStitcherOptions) => {['-vcodec', options.imageFormat === 'jpeg' ? 'mjpeg' : 'png'],['-i', '-'],],- // -c:v is the same as -vcodec as -codec:video- // and specified the video codec.- ['-c:v', encoderName],- proResProfileName ? ['-profile:v', proResProfileName] : null,- ['-pix_fmt', pixelFormat],-- // Without explicitly disabling auto-alt-ref,- // transparent WebM generation doesn't work- pixelFormat === 'yuva420p' ? ['-auto-alt-ref', '0'] : null,- ...validateQualitySettings({+ ...generateFfmpegArgs({+ hasPreencoded: false,+ proResProfileName,+ pixelFormat,+ x264Preset: options.x264Preset,+ codec,crf: options.crf,videoBitrate: options.videoBitrate,- codec,}),'-y',diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.tsindex 9f42a486e0..6291f38d31 100644--- a/packages/renderer/src/stitch-frames-to-video.ts+++ b/packages/renderer/src/stitch-frames-to-video.ts@@ -21,9 +21,9 @@ import {convertNumberOfGifLoopsToFfmpegSyntax} from './convert-number-of-gif-looimport {validateQualitySettings} from './crf';import {deleteDirectory} from './delete-directory';import {warnAboutM2Bug} from './does-have-m2-bug';+import {generateFfmpegArgs} from './ffmpeg-args';import type {FfmpegOverrideFn} from './ffmpeg-override';import {findRemotionRoot} from './find-closest-package-json';-import {getCodecName} from './get-codec-name';import {getFileExtensionFromCodec} from './get-extension-from-codec';import {getProResProfileName} from './get-prores-profile-name';import type {LogLevel} from './log-level';@@ -243,7 +243,6 @@ const innerStitchFramesToVideo = async (validateFps(fps, 'in `stitchFramesToVideo()`', false);- const encoderName = getCodecName(codec);const proResProfileName = getProResProfileName(codec, proResProfile);const mediaSupport = codecSupportsMedia(codec);@@ -413,25 +412,15 @@ const innerStitchFramesToVideo = async (numberOfGifLoops === null? null: ['-loop', convertNumberOfGifLoopsToFfmpegSyntax(numberOfGifLoops)],- // -c:v is the same as -vcodec as -codec:video- // and specified the video codec.- ['-c:v', encoderName],- ...(preEncodedFileLocation- ? []- : [- proResProfileName ? ['-profile:v', proResProfileName] : null,- ['-pix_fmt', pixelFormat],-- // Without explicitly disabling auto-alt-ref,- // transparent WebM generation doesn't work- pixelFormat === 'yuva420p' ? ['-auto-alt-ref', '0'] : null,- ...validateQualitySettings({- crf,- videoBitrate,- codec,- }),- ]),- x264Preset ? ['-preset', x264Preset] : null,+ ...generateFfmpegArgs({+ codec,+ crf,+ videoBitrate,+ hasPreencoded: Boolean(preEncodedFileLocation),+ proResProfileName,+ pixelFormat,+ x264Preset,+ }),codec === 'h264' ? ['-movflags', 'faststart'] : null,resolvedAudioCodec? ['-c:a', mapAudioCodecToFfmpegAudioCodecName(resolvedAudioCodec)]