Skip to content

🎥 Make videos programmatically with React

derive output name also for npx remotion still

Jonny Burger29b89f7fd119
diff --git a/packages/cli/src/determine-image-format.ts b/packages/cli/src/determine-image-format.ts
new file mode 100644
index 0000000000..23c583fb41
--- /dev/null
+++ b/packages/cli/src/determine-image-format.ts
@@ -0,0 +1,80 @@
+import type {ImageFormat, StillImageFormat} from '@remotion/renderer';
+
+const deriveExtensionFromFilename = (
+ filename: string | null
+): StillImageFormat | null => {
+ if (filename?.endsWith('.png')) {
+ return 'png';
+ }
+
+ if (filename?.endsWith('.jpg')) {
+ return 'jpeg';
+ }
+
+ if (filename?.endsWith('.jpeg')) {
+ return 'jpeg';
+ }
+
+ return null;
+};
+
+export const determineFinalImageFormat = ({
+ downloadName,
+ outName,
+ configImageFormat,
+ cliFlag,
+}: {
+ downloadName: string | null;
+ outName: string | null;
+ configImageFormat: ImageFormat | null;
+ cliFlag: ImageFormat | null;
+}): {format: StillImageFormat; source: string} => {
+ const outNameExtension = deriveExtensionFromFilename(outName);
+ const downloadNameExtension = deriveExtensionFromFilename(downloadName);
+
+ if (
+ outNameExtension &&
+ downloadNameExtension &&
+ outNameExtension !== downloadNameExtension
+ ) {
+ throw new TypeError(
+ `Image format mismatch: ${outName} was given as the S3 output name and ${downloadName} was given as the download name, but the extensions don't match.`
+ );
+ }
+
+ if (downloadNameExtension) {
+ if (cliFlag && downloadNameExtension !== cliFlag) {
+ throw new TypeError(
+ `Image format mismatch: ${downloadName} was given as the download name, but --image-format=${cliFlag} was passed. The image formats must match.`
+ );
+ }
+
+ return {format: downloadNameExtension, source: 'Download name extension'};
+ }
+
+ if (outNameExtension) {
+ if (cliFlag && outNameExtension !== cliFlag) {
+ throw new TypeError(
+ `Image format mismatch: ${outName} was given as the S3 out name, but --image-format=${cliFlag} was passed. The image formats must match.`
+ );
+ }
+
+ return {format: outNameExtension, source: 'Out name extension'};
+ }
+
+ if (cliFlag === 'none') {
+ throw new TypeError(
+ 'The --image-format flag must not be "none" for stills.'
+ );
+ }
+
+ if (cliFlag !== null) {
+ return {format: cliFlag, source: '--image-format flag'};
+ }
+
+ if (configImageFormat !== null && configImageFormat !== 'none') {
+ return {format: configImageFormat, source: 'Config file'};
+ }
+
+ return {format: 'png', source: 'Default'};
+};
diff --git a/packages/cli/src/index.tsx b/packages/cli/src/index.tsx
index 77191a3d48..da1b90ffa6 100644
--- a/packages/cli/src/index.tsx
+++ b/packages/cli/src/index.tsx
@@ -3,6 +3,7 @@ import {chalk} from './chalk';
import {checkNodeVersion} from './check-version';
import {listCompositionsCommand} from './compositions';
import {overrideRemotion} from './config/index';
+import {determineFinalImageFormat} from './determine-image-format';
import {getFileSizeDownloadBar} from './download-progress';
import {findRemotionRoot} from './find-closest-package-json';
import {formatBytes} from './format-bytes';
@@ -109,4 +110,5 @@ export const CliInternals = {
getFileSizeDownloadBar,
findRemotionRoot,
getFinalCodec,
+ determineFinalImageFormat,
};
diff --git a/packages/cli/src/still.ts b/packages/cli/src/still.ts
index 6556823b67..b4e8351310 100644
--- a/packages/cli/src/still.ts
+++ b/packages/cli/src/still.ts
@@ -8,7 +8,8 @@ import {
import {mkdirSync} from 'fs';
import path from 'path';
import {chalk} from './chalk';
-import {Config, ConfigInternals} from './config';
+import {ConfigInternals} from './config';
+import {determineFinalImageFormat} from './determine-image-format';
import {
getAndValidateAbsoluteOutputFile,
getCliOptions,
@@ -44,30 +45,11 @@ export const still = async (remotionRoot: string) => {
process.exit(1);
}
- const userPassedOutput = getUserPassedOutputLocation();
- if (
- userPassedOutput?.endsWith('.jpeg') ||
- userPassedOutput?.endsWith('.jpg')
- ) {
- Log.verbose(
- 'Output file has a JPEG extension, setting the image format to JPEG.'
- );
- Config.Rendering.setImageFormat('jpeg');
- }
-
- if (userPassedOutput?.endsWith('.png')) {
- Log.verbose(
- 'Output file has a PNG extension, setting the image format to PNG.'
- );
- Config.Rendering.setImageFormat('png');
- }
-
const {
inputProps,
envVariables,
quality,
browser,
- imageFormat,
stillFrame,
browserExecutable,
chromiumOptions,
@@ -88,6 +70,13 @@ export const still = async (remotionRoot: string) => {
const compositionId = getCompositionId();
+ const {format: imageFormat, source} = determineFinalImageFormat({
+ cliFlag: parsedCli['image-format'] ?? null,
+ configImageFormat: ConfigInternals.getUserPreferredImageFormat() ?? null,
+ downloadName: null,
+ outName: getUserPassedOutputLocation(),
+ });
+
const relativeOutputLocation = getOutputLocation({
compositionId,
defaultExtension: imageFormat,
@@ -100,17 +89,10 @@ export const still = async (remotionRoot: string) => {
Log.info(
chalk.gray(
- `Output = ${relativeOutputLocation}, Format = ${imageFormat}, Composition = ${compositionId}`
+ `Output = ${relativeOutputLocation}, Format = ${imageFormat} (${source}), Composition = ${compositionId}`
)
);
- if (imageFormat === 'none') {
- Log.error(
- 'No image format was selected - this is probably an error in Remotion - please post your command on Github Issues for help.'
- );
- process.exit(1);
- }
-
validateImageFormat(imageFormat, absoluteOutputLocation);
const browserInstance = openBrowser(browser, {
diff --git a/packages/lambda/src/cli/commands/still.ts b/packages/lambda/src/cli/commands/still.ts
index 8c10544621..eb7b35be24 100644
--- a/packages/lambda/src/cli/commands/still.ts
+++ b/packages/lambda/src/cli/commands/still.ts
@@ -1,5 +1,4 @@
import {CliInternals, ConfigInternals} from '@remotion/cli';
-import type {ImageFormat, StillImageFormat} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import {downloadMedia} from '../../api/download-media';
import {renderStillOnLambda} from '../../api/render-still-on-lambda';
@@ -18,85 +17,6 @@ import {Log} from '../log';
export const STILL_COMMAND = 'still';
-const deriveExtensionFromFilename = (
- filename: string | null
-): StillImageFormat | null => {
- if (filename?.endsWith('.png')) {
- return 'png';
- }
-
- if (filename?.endsWith('.jpg')) {
- return 'jpeg';
- }
-
- if (filename?.endsWith('.jpeg')) {
- return 'jpeg';
- }
-
- return null;
-};
-
-const getImageFormat = ({
- downloadName,
- outName,
- configImageFormat,
- cliFlag,
-}: {
- downloadName: string | null;
- outName: string | null;
- configImageFormat: ImageFormat | null;
- cliFlag: ImageFormat | null;
-}): {format: StillImageFormat; source: string} => {
- const outNameExtension = deriveExtensionFromFilename(outName);
- const downloadNameExtension = deriveExtensionFromFilename(downloadName);
-
- if (
- outNameExtension &&
- downloadNameExtension &&
- outNameExtension !== downloadNameExtension
- ) {
- throw new TypeError(
- `Image format mismatch: ${outName} was given as the S3 output name and ${downloadName} was given as the download name, but the extensions don't match.`
- );
- }
-
- if (downloadNameExtension) {
- if (cliFlag && downloadNameExtension !== cliFlag) {
- throw new TypeError(
- `Image format mismatch: ${downloadName} was given as the download name, but --image-format=${cliFlag} was passed. The image formats must match.`
- );
- }
-
- return {format: downloadNameExtension, source: 'Download name extension'};
- }
-
- if (outNameExtension) {
- if (cliFlag && outNameExtension !== cliFlag) {
- throw new TypeError(
- `Image format mismatch: ${outName} was given as the S3 out name, but --image-format=${cliFlag} was passed. The image formats must match.`
- );
- }
-
- return {format: outNameExtension, source: 'Out name extension'};
- }
-
- if (cliFlag === 'none') {
- throw new TypeError(
- 'The --image-format flag must not be "none" for stills.'
- );
- }
-
- if (cliFlag !== null) {
- return {format: cliFlag, source: '--image-format flag'};
- }
-
- if (configImageFormat !== null && configImageFormat !== 'none') {
- return {format: configImageFormat, source: 'Config file'};
- }
-
- return {format: 'png', source: 'Default'};
-};
-
export const stillCommand = async (args: string[]) => {
const serveUrl = args[0];
@@ -149,12 +69,13 @@ export const stillCommand = async (args: string[]) => {
const privacy = parsedLambdaCli.privacy ?? DEFAULT_OUTPUT_PRIVACY;
validatePrivacy(privacy);
- const {format: imageFormat, source: imageFormatReason} = getImageFormat({
- downloadName,
- outName: outName ?? null,
- configImageFormat: ConfigInternals.getUserPreferredImageFormat() ?? null,
- cliFlag: CliInternals.parsedCli['image-format'] ?? null,
- });
+ const {format: imageFormat, source: imageFormatReason} =
+ CliInternals.determineFinalImageFormat({
+ downloadName,
+ outName: outName ?? null,
+ configImageFormat: ConfigInternals.getUserPreferredImageFormat() ?? null,
+ cliFlag: CliInternals.parsedCli['image-format'] ?? null,
+ });
try {
Log.info(