Skip to content

🎥 Make videos programmatically with React

Merge branch 'main' into lambda-testing

Jonny Burger31c3831d101e
diff --cc packages/renderer/src/stitcher.ts
index e341f55cda,5934a1bfa0..c49bf0b773
--- a/packages/renderer/src/stitcher.ts
+++ b/packages/renderer/src/stitcher.ts
@@@ -10,21 -11,16 +10,19 @@@ import
import {assetsToFfmpegInputs} from './assets-to-ffmpeg-inputs';
import {calculateAssetPositions} from './assets/calculate-asset-positions';
import {convertAssetsToFileUrls} from './assets/convert-assets-to-file-urls';
-import {markAllAssetsAsDownloaded} from './assets/download-and-map-assets-to-file';
+import {
+ markAllAssetsAsDownloaded,
+ RenderMediaOnDownload,
+} from './assets/download-and-map-assets-to-file';
import {getAssetAudioDetails} from './assets/get-asset-audio-details';
+import {Assets} from './assets/types';
- import {uncompressMediaAsset} from './assets/types';
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-prores-profile-name';
-import {DEFAULT_IMAGE_FORMAT} from './image-format';
import {parseFfmpegProgress} from './parse-ffmpeg-progress';
- import {resolveAssetSrc} from './resolve-asset-src';
+import {DEFAULT_SAMPLE_RATE} from './sample-rate';
import {validateEvenDimensionsWithCodec} from './validate-even-dimensions-with-codec';
import {validateFfmpeg} from './validate-ffmpeg';
@@@ -34,72 -31,20 +32,65 @@@ export type StitcherOptions =
height: number;
outputLocation: string;
force: boolean;
- assetsInfo: RenderAssetInfo;
- // TODO: Let's make this parameter mandatory in the next major release
- imageFormat?: ImageFormat;
+ assetsInfo: RenderAssetInfo | null;
pixelFormat?: PixelFormat;
codec?: Codec;
- crf?: number;
- // TODO: Do we want a parallelism flag for stitcher?
- parallelism?: number | null;
+ crf?: number | null;
onProgress?: (progress: number) => void;
- onDownload?: (src: string) => void;
+ onDownload?: RenderMediaOnDownload;
proResProfile?: ProResProfile;
verbose?: boolean;
+ parallelEncoding?: boolean;
+ preEncodedFileLocation?: string | null;
ffmpegExecutable?: FfmpegExecutable;
-}): Promise<void> => {
+};
+
+const getAssetsData = async (options: StitcherOptions) => {
+ const codec = options.codec ?? Internals.DEFAULT_CODEC;
+ const encoderName = getCodecName(codec);
+ const isAudioOnly = encoderName === null;
+ const fileUrlAssets = await (options.assetsInfo
+ ? convertAssetsToFileUrls({
+ assets: options.assetsInfo.assets,
+ downloadDir: options.assetsInfo.downloadDir,
+ onDownload: options.onDownload ?? (() => () => undefined),
+ })
+ : null);
+
+ markAllAssetsAsDownloaded();
+ const assetPositions: Assets =
+ fileUrlAssets === null ? [] : calculateAssetPositions(fileUrlAssets);
+
+ markAllAssetsAsDownloaded();
+
- const assetPaths = assetPositions.map((asset) =>
- resolveAssetSrc(
- uncompressMediaAsset((options.assetsInfo?.assets ?? []).flat(1), asset)
- .src
- )
- );
-
+ const assetAudioDetails = await getAssetAudioDetails({
- assetPaths,
++ assetPaths: assetPositions.map((a) => a.src),
+ });
+
+ const filters = calculateFfmpegFilters({
+ assetAudioDetails,
+ assetPositions,
+ fps: options.fps,
+ videoTrackCount: isAudioOnly ? 0 : 1,
+ });
+ if (options.verbose) {
+ console.log('asset positions', assetPositions);
+ }
+
+ if (options.verbose) {
+ console.log('filters', filters);
+ }
+
+ const {complexFilterFlag, cleanup} = await createFfmpegComplexFilter(filters);
+
+ return {
+ complexFilterFlag,
+ cleanup,
- assetPaths,
++ assetPositions,
+ };
+};
+
+export const spawnFfmpeg = async (options: StitcherOptions) => {
Internals.validateDimension(
options.height,
'height',
@@@ -146,42 -93,62 +137,42 @@@
}
Internals.validateSelectedCrfAndCodecCombination(crf, codec);
- Internals.validateSelectedPixelFormatAndImageFormatCombination(
- pixelFormat,
- imageFormat
- );
Internals.validateSelectedPixelFormatAndCodecCombination(pixelFormat, codec);
- const [frameInfo, fileUrlAssets] = await Promise.all([
- getFrameInfo({
- dir: options.dir,
- isAudioOnly,
- }),
- convertAssetsToFileUrls({
- assets: options.assetsInfo.assets,
- dir: options.assetsInfo.bundleDir,
- onDownload: options.onDownload ?? (() => undefined),
- }),
- ]);
-
- markAllAssetsAsDownloaded();
- const assetPositions = calculateAssetPositions(fileUrlAssets);
-
- const assetAudioDetails = await getAssetAudioDetails({
- assetPaths: assetPositions.map((a) => a.src),
- parallelism: options.parallelism,
- });
-
- const filters = calculateFfmpegFilters({
- assetAudioDetails,
- assetPositions,
- fps: options.fps,
- videoTrackCount: isAudioOnly ? 0 : 1,
- });
- if (options.verbose) {
- console.log('asset positions', assetPositions);
- }
-
- if (options.verbose) {
- console.log('filters', filters);
- }
+ const {
+ complexFilterFlag = undefined,
+ cleanup = undefined,
- assetPaths = undefined,
++ assetPositions = undefined,
+ } = options.parallelEncoding ? {} : await getAssetsData(options);
- const {complexFilterFlag, cleanup} = await createFfmpegComplexFilter(filters);
const ffmpegArgs = [
['-r', String(options.fps)],
- isAudioOnly ? null : ['-f', 'image2'],
- isAudioOnly ? null : ['-s', `${options.width}x${options.height}`],
- frameInfo ? ['-start_number', String(frameInfo.startNumber)] : null,
- frameInfo
- ? ['-i', `element-%0${frameInfo.numberLength}d.${imageFormat}`]
- : null,
- ...assetsToFfmpegInputs({
- assets: assetPositions.map((a) => a.src),
- isAudioOnly,
- fps: options.fps,
- frameCount: options.assetsInfo.assets.length,
- }),
- encoderName
+ ...(options.preEncodedFileLocation
+ ? [['-i', options.preEncodedFileLocation]]
+ : isAudioOnly
+ ? []
+ : [
+ ['-f', options.parallelEncoding ? 'image2pipe' : 'image2'],
+ ['-s', `${options.width}x${options.height}`],
+ options.assetsInfo
+ ? ['-start_number', String(options.assetsInfo.firstFrameIndex)]
+ : null,
+ options.assetsInfo
+ ? ['-i', options.assetsInfo.imageSequenceName]
+ : null,
+ options.parallelEncoding ? ['-i', '-'] : null,
+ ]),
- ...(options.assetsInfo && assetPaths
++ ...(options.assetsInfo && assetPositions
+ ? assetsToFfmpegInputs({
- assets: assetPaths,
++ assets: assetPositions.map((a) => a.src),
+ isAudioOnly,
+ fps: options.fps,
+ frameCount: options.assetsInfo.assets.length,
+ })
+ : []),
+ options.preEncodedFileLocation
+ ? ['-c:v', 'copy']
+ : encoderName
? // -c:v is the same as -vcodec as -codec:video
// and specified the video codec.
['-c:v', encoderName]