🎥 Make videos programmatically with React
feat: concurrentMode
辉宇634ffa5193a3
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.tsindex bf838fecf3..9b515a96ce 100644--- a/packages/cli/src/get-cli-options.ts+++ b/packages/cli/src/get-cli-options.ts@@ -193,6 +193,8 @@ export const getCliOptions = async (type: 'still' | 'series') => {return {parallelism: Internals.getConcurrency(),+ concurrentMode: Internals.getConcurrentMode(),+ parallelEncoding: Internals.getParallelEncoding(),frameRange,shouldOutputImageSequence,codec,diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsxindex d67787cf89..20fab91249 100644--- a/packages/cli/src/render.tsx+++ b/packages/cli/src/render.tsx@@ -54,6 +54,8 @@ export const render = async () => {codec,proResProfile,parallelism,+ concurrentMode,+ parallelEncoding,frameRange,shouldOutputImageSequence,absoluteOutputFile,@@ -72,7 +74,7 @@ export const render = async () => {const actualParallelism = getActualConcurrency(parallelism ?? null);- const browserInstance = Promise.all(new Array(actualParallelism).fill(true).map(()=>RenderInternals.openBrowser(browser, {+ const browserInstance = Promise.all(new Array(concurrentMode==='browser'?actualParallelism:1).fill(true).map(()=>RenderInternals.openBrowser(browser, {browserExecutable,shouldDumpIo: Internals.Logging.isEqualOrBelowLogLevel('verbose'),})));@@ -129,6 +131,8 @@ export const render = async () => {);},parallelism,+ concurrentMode,+ parallelEncoding,compositionId,outputDir,onError,diff --git a/packages/core/src/config/concurrent-mode.ts b/packages/core/src/config/concurrent-mode.tsindex a4684bf421..7d90980db0 100644--- a/packages/core/src/config/concurrent-mode.ts+++ b/packages/core/src/config/concurrent-mode.ts@@ -1,10 +1,10 @@const validOptions = ['tab','browser'] as const;-export type concurrentMode = typeof validOptions[number];+export type ConcurrentMode = typeof validOptions[number];-let currentConcurrentMode: concurrentMode = 'tab';+let currentConcurrentMode: ConcurrentMode = 'tab';-export const setConcurrentMode = (mode: concurrentMode) => {+export const setConcurrentMode = (mode: ConcurrentMode) => {if (!validOptions.includes(mode)) {throw new TypeError(`Value ${mode} is not valid as a concurrent mode.`);}diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.tsindex b69c54e557..42c2199b5c 100644--- a/packages/core/src/config/index.ts+++ b/packages/core/src/config/index.ts@@ -20,7 +20,7 @@ import {setPort} from './preview-server';import {setProResProfile} from './prores-profile';import {setQuality} from './quality';import {setWebpackCaching} from './webpack-caching';-import {setConcurrentMode} from "./concurrent-mode";+import {setConcurrentMode,ConcurrentMode} from "./concurrent-mode";import {setParallelEncoding} from "./parallel-encoding";export const Config = {@@ -165,4 +165,5 @@ export type {Browser,FrameRange,LogLevel,+ ConcurrentMode,};diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex 3a39d20885..6508fd9f9f 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -21,6 +21,8 @@ import {getOutputCodecOrUndefined,} from './config/codec';import {getConcurrency} from './config/concurrency';+import {getConcurrentMode} from "./config/concurrent-mode";+import {getParallelEncoding} from "./config/parallel-encoding";import {getActualCrf,getDefaultCrfForCodec,@@ -128,6 +130,8 @@ export const Internals = {getIsEvaluation,getPixelFormat,getConcurrency,+ getConcurrentMode,+ getParallelEncoding,getRange,getShouldOverwrite,getOutputCodecOrUndefined,diff --git a/packages/example/remotion.config.ts b/packages/example/remotion.config.tsindex b40d19add9..fc596e78b0 100644--- a/packages/example/remotion.config.ts+++ b/packages/example/remotion.config.ts@@ -3,6 +3,7 @@ import path from 'path';import {Config, WebpackOverrideFn} from 'remotion';Config.Output.setOverwriteOutput(true);+Config.Rendering.setConcurrentMode('browser');type Bundler = 'webpack' | 'esbuild';diff --git a/packages/renderer/src/render.ts b/packages/renderer/src/render.tsindex 88d3f09da7..1d1318215b 100644--- a/packages/renderer/src/render.ts+++ b/packages/renderer/src/render.ts@@ -7,6 +7,7 @@ import {ImageFormat,Internals,VideoConfig,+ ConcurrentMode,} from 'remotion';import {getActualConcurrency} from './get-concurrency';import {getFrameCount} from './get-frame-range';@@ -19,6 +20,7 @@ import {seekToFrame} from './seek-to-frame';import {serveStatic} from './serve-static';import {setPropsAndEnv} from './set-props-and-env';import {OnErrorInfo, OnStartData, RenderFramesOutput} from './types';+import {cycleBrowserTabs} from "./cycle-browser-tabs";export const renderFrames = async ({config,@@ -38,6 +40,8 @@ export const renderFrames = async ({puppeteerInstance,onError,browserExecutable,+ concurrentMode,+ parallelEncoding,}: {config: VideoConfig;compositionId: string;@@ -56,6 +60,8 @@ export const renderFrames = async ({puppeteerInstance?: Array<PuppeteerBrowser>;browserExecutable?: BrowserExecutable;onError?: (info: OnErrorInfo) => void;+ concurrentMode?: ConcurrentMode;+ parallelEncoding?: boolean;}): Promise<RenderFramesOutput> => {Internals.validateDimension(config.height,@@ -88,7 +94,7 @@ export const renderFrames = async ({const [{port, close}, browserInstance] = await Promise.all([serveStatic(webpackBundle),puppeteerInstance ??- Promise.all(new Array(actualParallelism).fill(true).map(()=>+ Promise.all(new Array(concurrentMode==='browser'?actualParallelism:1).fill(true).map(()=>openBrowser(browser, {shouldDumpIo: dumpBrowserLogs,browserExecutable,@@ -99,8 +105,9 @@ export const renderFrames = async ({// browserExecutable,// }),]);- const pages = browserInstance.map(async o => {- const page = await o.newPage();++ const initPage=async (_browser:PuppeteerBrowser) => {+ const page = await _browser.newPage();// const page = await browserInstance.newPage();page.setViewport({width: config.width,@@ -132,8 +139,15 @@ export const renderFrames = async ({await page.goto(site);page.off('pageerror', errorCallback);return page;- });- // const {stopCycling} = cycleBrowserTabs(browserInstance);+ }++ const pages = concurrentMode==='browser'?+ browserInstance.map(initPage):+ new Array(actualParallelism).fill(true).map(()=>initPage(browserInstance[0]));++ let stopCycling = () => {};+ if(concurrentMode!=='browser')+ stopCycling=cycleBrowserTabs(browserInstance[0]).stopCycling;const puppeteerPages = await Promise.all(pages);const pool = new Pool(puppeteerPages);@@ -213,7 +227,7 @@ export const renderFrames = async ({close().catch((err) => {console.log('Unable to close web server', err);});- // stopCycling();+ stopCycling();// If browser instance was passed in, we close all the pages// we opened.// If new browser was opened, then closing the browser as a cleanup.