Skip to content

🎥 Make videos programmatically with React

Extend concurrency property to accept percentage

Ravi Jaina7629d44f8eb
diff --git a/packages/cli/src/config/concurrency.ts b/packages/cli/src/config/concurrency.ts
index ee3dc08fd3..82b97a4ac8 100644
--- a/packages/cli/src/config/concurrency.ts
+++ b/packages/cli/src/config/concurrency.ts
@@ -1,6 +1,6 @@
-export type Concurrency = number | null;
+export type Concurrency = number | string | null;
-let currentConcurrency: null | number = null;
+let currentConcurrency: null | number | string = null;
export const setConcurrency = (newConcurrency: Concurrency) => {
currentConcurrency = newConcurrency;
diff --git a/packages/docs/docs/cli/render.md b/packages/docs/docs/cli/render.md
index fc00c7121b..34a5e9ec27 100644
--- a/packages/docs/docs/cli/render.md
+++ b/packages/docs/docs/cli/render.md
@@ -48,7 +48,7 @@ _available from v3.2.40_
### `--concurrency`
-[How many CPU threads to use.](/docs/config#setconcurrency) Minimum 1. The maximum is the amount of threads you have (In Node.JS `os.cpus().length`).
+[How many CPU threads to use.](/docs/config#setconcurrency) Minimum 1. The maximum is the amount of threads you have (In Node.JS `os.cpus().length`). You can also provide a percentage value (e.g. 50%).
### `--pixel-format`
diff --git a/packages/docs/docs/renderer/render-frames.md b/packages/docs/docs/renderer/render-frames.md
index 234cda46e0..df9af99946 100644
--- a/packages/docs/docs/renderer/render-frames.md
+++ b/packages/docs/docs/renderer/render-frames.md
@@ -87,7 +87,7 @@ A `string` which must be either `png`, `jpeg` or `none`.
_optional_
-A `number` specifying how many render processes should be started in parallel or `null` to let Remotion decide based on the CPU of the host machine. Default is half of the CPU threads available.
+A `number` specifying how many render processes should be started in parallel, a `string` specifying the percentage of the cpu threads to use, or `null` to let Remotion decide based on the CPU of the host machine. Default is half of the CPU threads available.
### ~~`parallelism?`~~
diff --git a/packages/docs/docs/renderer/render-media.md b/packages/docs/docs/renderer/render-media.md
index e52792d1c2..017dfaab21 100644
--- a/packages/docs/docs/renderer/render-media.md
+++ b/packages/docs/docs/renderer/render-media.md
@@ -74,7 +74,7 @@ An object of arbitrary shape that will be passed as [props to your composition](
_optional_
-A `number` specifying how many render processes should be started in parallel or `null` to let Remotion decide based on the CPU of the host machine. Default is half of the CPU threads available.
+A `number` specifying how many render processes should be started in parallel, a `string` specifying the percentage of the cpu threads to use, or `null` to let Remotion decide based on the CPU of the host machine. Default is half of the CPU threads available.
### ~~`parallelism?`~~
diff --git a/packages/renderer/src/get-concurrency.ts b/packages/renderer/src/get-concurrency.ts
index 80d64dd8be..117788438e 100644
--- a/packages/renderer/src/get-concurrency.ts
+++ b/packages/renderer/src/get-concurrency.ts
@@ -1,13 +1,20 @@
import os from 'os';
-export const getActualConcurrency = (userPreference: number | null) => {
+export const getActualConcurrency = (userPreference: number | string | null) => {
if (userPreference === null) {
return Math.round(Math.min(8, Math.max(1, os.cpus().length / 2)));
}
- const rounded = Math.floor(userPreference);
const max = os.cpus().length;
const min = 1;
+
+ if (typeof userPreference === 'string') {
+ const percentage = parseInt(userPreference.slice(0, -1));
+ const rounded = Math.floor(percentage/100 * max);
+ return rounded;
+ }
+
+ const rounded = Math.floor(userPreference);
if (rounded > max) {
throw new Error(
`Maximum for --concurrency is ${max} (number of cores on this system)`
diff --git a/packages/renderer/src/render-frames.ts b/packages/renderer/src/render-frames.ts
index 447a095be0..b8d9d06643 100644
--- a/packages/renderer/src/render-frames.ts
+++ b/packages/renderer/src/render-frames.ts
@@ -62,7 +62,7 @@ type ConfigOrComposition =
type ConcurrencyOrParallelism =
| {
- concurrency?: number | null;
+ concurrency?: number | string | null;
}
| {
/**
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index 5a14e0e61d..b134c1a077 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -115,7 +115,7 @@ export type RenderMediaOptions = {
type ConcurrencyOrParallelism =
| {
- concurrency?: number | null;
+ concurrency?: number | string | null;
}
| {
/**
diff --git a/packages/renderer/src/validate-concurrency.ts b/packages/renderer/src/validate-concurrency.ts
index d4e1d317f9..ac8094bf37 100644
--- a/packages/renderer/src/validate-concurrency.ts
+++ b/packages/renderer/src/validate-concurrency.ts
@@ -7,23 +7,29 @@ export const validateConcurrency = (value: unknown, setting: string) => {
return;
}
- if (typeof value !== 'number') {
- throw new Error(setting + ' must be a number but is ' + value);
+ if (typeof value !== 'number' && typeof value !== 'string') {
+ throw new Error(setting + ' must a number or a string but is ' + value);
}
- if (value % 1 !== 0) {
- throw new Error(setting + ' must be an integer, but is ' + value);
- }
+ if (typeof value === 'number') {
+ if (value % 1 !== 0) {
+ throw new Error(setting + ' must be an integer, but is ' + value);
+ }
- if (value < 1) {
- throw new Error(setting + ' must be at least 1, but is ' + value);
- }
+ if (value < 1) {
+ throw new Error(setting + ' must be at least 1, but is ' + value);
+ }
- if (value > require('os').cpus().length) {
- throw new Error(
- `${setting} is set higher than the amount of CPU cores available. Available CPU cores: ${
- require('os').cpus().length
- }, value set: ${value}`
- );
+ if (value > require('os').cpus().length) {
+ throw new Error(
+ `${setting} is set higher than the amount of CPU cores available. Available CPU cores: ${
+ require('os').cpus().length
+ }, value set: ${value}`
+ );
+ }
+ } else {
+ if (!(/^\d+(\.\d+)?%$/.test(value))) {
+ throw new Error(setting + ' must be a valid percentage, but is ' + value);
+ }
}
};