Skip to content

🎥 Make videos programmatically with React

Added all required parameters in select-composition.ts, added selectComposition() call in get-composition-id.ts if no <composition-id> is passed

Stefan Uzunov8442784168e4
diff --git a/packages/cli/src/benchmark.ts b/packages/cli/src/benchmark.ts
index 58e0fa976d..8be8860172 100644
--- a/packages/cli/src/benchmark.ts
+++ b/packages/cli/src/benchmark.ts
@@ -9,6 +9,7 @@ import path from 'path';
import {chalk} from './chalk';
import {ConfigInternals} from './config';
import {getCliOptions, getFinalCodec} from './get-cli-options';
+import {getCompositionId} from './get-composition-id';
import {getRenderMediaOptions} from './get-render-media-options';
import {Log} from './log';
import {makeProgressBar} from './make-progress-bar';
@@ -204,6 +205,11 @@ export const benchmarkCommand = async (
.map((c) => c.trim())
.filter(truthy);
+ if (ids[0] === '') {
+ const composition = await getCompositionId();
+ return composition;
+ }
+
const compositions = ids.map((compId) => {
const composition = comps.find((c) => c.id === compId);
diff --git a/packages/cli/src/composition-prompts.ts b/packages/cli/src/composition-prompts.ts
index 6f05ee9bc5..cd3025869c 100644
--- a/packages/cli/src/composition-prompts.ts
+++ b/packages/cli/src/composition-prompts.ts
@@ -26,7 +26,7 @@ prompt.separator = (title: string) => ({
value: undefined,
});
-export async function selectComposition(
+export async function selectAsync(
questions: NamelessQuestion,
options?: PromptOptions
): Promise<unknown> {
diff --git a/packages/cli/src/get-composition-id.ts b/packages/cli/src/get-composition-id.ts
index f27c31a1cd..ee41fdfb19 100644
--- a/packages/cli/src/get-composition-id.ts
+++ b/packages/cli/src/get-composition-id.ts
@@ -1,11 +1,13 @@
import {Log} from './log';
import {parsedCli} from './parse-command-line';
+import {selectComposition} from './select-composition';
-export const getCompositionId = () => {
+export const getCompositionId = async () => {
if (!parsedCli._[2]) {
Log.error('Composition ID not passed.');
- Log.error('Pass an extra argument <composition-id>.');
- process.exit(1);
+
+ const selectedComposition = await selectComposition();
+ return selectedComposition;
}
return parsedCli._[2];
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index 66b6484fa3..a5ba1585f2 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -93,10 +93,12 @@ export const render = async (remotionRoot: string) => {
codec,
});
+ const compositionId = await getCompositionId();
+
const relativeOutputLocation = getOutputFilename({
codec,
imageSequence: shouldOutputImageSequence,
- compositionName: getCompositionId(),
+ compositionName: compositionId,
defaultExtension: RenderInternals.getFileExtensionFromCodec(codec, 'final'),
});
@@ -105,8 +107,6 @@ export const render = async (remotionRoot: string) => {
overwrite
);
- const compositionId = getCompositionId();
-
Log.info(
chalk.gray(
`Composition = ${compositionId}, Codec = ${codec} (${codecReason}), Output = ${relativeOutputLocation}`
diff --git a/packages/cli/src/select-composition.ts b/packages/cli/src/select-composition.ts
index b71ea27cda..846f674e2e 100644
--- a/packages/cli/src/select-composition.ts
+++ b/packages/cli/src/select-composition.ts
@@ -1,5 +1,107 @@
-import {getCompositions} from '@remotion/renderer';
+import {
+ getCompositions,
+ openBrowser,
+ RenderInternals,
+} from '@remotion/renderer';
+import path from 'path';
+import prompts from './composition-prompts';
+// eslint-disable-next-line no-restricted-imports
+import {Internals} from 'remotion';
+import {selectAsync} from './composition-prompts';
-const compositions = async () => await getCompositions();
+import {chalk} from './chalk';
+import {ConfigInternals} from './config';
+import {findRemotionRoot} from './find-closest-package-json';
+import {getCliOptions, getFinalCodec} from './get-cli-options';
+import {parsedCli} from './parse-command-line';
+import {bundleOnCliOrTakeServeUrl} from './setup-cache';
+import type {RenderStep} from './step';
+import {getUserPassedOutputLocation} from './user-passed-output-location';
-export const selectComposition = () => {};
+export const selectComposition = async () => {
+ const remotionRoot = findRemotionRoot();
+ const file = parsedCli._[1];
+ const downloadMap = RenderInternals.makeDownloadMap();
+
+ const fullPath = RenderInternals.isServeUrl(file)
+ ? file
+ : path.join(process.cwd(), file);
+
+ const {codec} = getFinalCodec({
+ downloadName: null,
+ outName: getUserPassedOutputLocation(),
+ });
+
+ const {
+ shouldOutputImageSequence,
+ inputProps,
+ envVariables,
+ browserExecutable,
+ browser,
+ chromiumOptions,
+ port,
+ puppeteerTimeout,
+ publicDir,
+ scale,
+ } = await getCliOptions({
+ isLambda: false,
+ type: 'series',
+ codec,
+ });
+
+ const steps: RenderStep[] = [
+ RenderInternals.isServeUrl(fullPath) ? null : ('bundling' as const),
+ 'rendering' as const,
+ shouldOutputImageSequence ? null : ('stitching' as const),
+ ].filter(Internals.truthy);
+
+ const {urlOrBundle} = await bundleOnCliOrTakeServeUrl({
+ fullPath,
+ remotionRoot,
+ steps,
+ publicDir,
+ });
+
+ const browserInstance = openBrowser(browser, {
+ browserExecutable,
+ shouldDumpIo: RenderInternals.isEqualOrBelowLogLevel(
+ ConfigInternals.Logging.getLogLevel(),
+ 'verbose'
+ ),
+ chromiumOptions,
+ forceDeviceScaleFactor: scale,
+ });
+
+ const puppeteerInstance = await browserInstance;
+
+ const compositions = await getCompositions(urlOrBundle, {
+ inputProps,
+ puppeteerInstance,
+ envVariables,
+ timeoutInMilliseconds: puppeteerTimeout,
+ chromiumOptions,
+ browserExecutable,
+ downloadMap,
+ port,
+ });
+
+ const selectedComposition = (await selectAsync(
+ {
+ message: 'Select a composition:',
+ optionsPerPage: 20,
+ choices: compositions.map((comp) => {
+ if (typeof comp.id === 'string') {
+ return prompts.separator(comp.id);
+ }
+
+ return {
+ value: comp.id,
+ title: chalk.bold(comp.id),
+ };
+ }),
+ },
+ {}
+ )) as string;
+
+ return selectedComposition;
+};
diff --git a/packages/cli/src/still.ts b/packages/cli/src/still.ts
index b353e806f9..3875f04dcf 100644
--- a/packages/cli/src/still.ts
+++ b/packages/cli/src/still.ts
@@ -67,7 +67,7 @@ export const still = async (remotionRoot: string) => {
Log.verbose('Browser executable: ', browserExecutable);
- const compositionId = getCompositionId();
+ const compositionId = await getCompositionId();
const {format: imageFormat, source} = determineFinalImageFormat({
cliFlag: parsedCli['image-format'] ?? null,
diff --git a/packages/cli/src/strip-ansi.ts b/packages/cli/src/strip-ansi.ts
new file mode 100644
index 0000000000..6021fad479
--- /dev/null
+++ b/packages/cli/src/strip-ansi.ts
@@ -0,0 +1,20 @@
+/**
+ * Code inlined from https://github.com/chalk/strip-ansi#readme
+ * This file is MIT licensed.
+ */
+const ansiRegex = () => {
+ const pattern = [
+ '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
+ '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))',
+ ].join('|');
+
+ return new RegExp(pattern, 'g');
+};
+
+export const stripAnsi = (str: string) => {
+ if (typeof str !== 'string') {
+ throw new TypeError(`Expected a \`string\`, got \`${typeof str}\``);
+ }
+
+ return str.replace(ansiRegex(), '');
+};