Skip to content

🎥 Make videos programmatically with React

determine imageFormat from 1. downloadName 2. outName 3. CLI flag 4. config file

Jonny Burgerd92a1f62df1c
diff --git a/packages/lambda/src/cli/commands/still.ts b/packages/lambda/src/cli/commands/still.ts
index 88a22717a4..85dfec1dde 100644
--- a/packages/lambda/src/cli/commands/still.ts
+++ b/packages/lambda/src/cli/commands/still.ts
@@ -1,5 +1,5 @@
-import {CliInternals} from '@remotion/cli';
-import type {StillImageFormat} from '@remotion/renderer';
+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,6 +18,85 @@ 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;
+}): StillImageFormat => {
+ 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 downloadNameExtension;
+ }
+
+ 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 outNameExtension;
+ }
+
+ if (cliFlag === 'none') {
+ throw new TypeError(
+ 'The --image-format flag must not be "none" for stills.'
+ );
+ }
+
+ if (cliFlag !== null) {
+ return cliFlag;
+ }
+
+ if (configImageFormat !== null && configImageFormat !== 'none') {
+ return configImageFormat;
+ }
+
+ return 'png';
+};
+
export const stillCommand = async (args: string[]) => {
const serveUrl = args[0];
@@ -44,7 +123,8 @@ export const stillCommand = async (args: string[]) => {
quit(1);
}
- const outName = args[2] ?? null;
+ const downloadName = args[2] ?? null;
+ const outName = parsedLambdaCli['out-name'];
const {
chromiumOptions,
@@ -69,34 +149,19 @@ export const stillCommand = async (args: string[]) => {
const privacy = parsedLambdaCli.privacy ?? DEFAULT_OUTPUT_PRIVACY;
validatePrivacy(privacy);
- let imageFormat = cliImageFormat;
-
- if (outName) {
- if (cliImageFormat === 'none') {
- if (outName?.endsWith('.jpeg') || outName?.endsWith('.jpg')) {
- Log.verbose(
- 'Output file has a JPEG extension, setting the image format to JPEG.'
- );
- imageFormat = 'jpeg';
- }
-
- if (outName?.endsWith('.png')) {
- Log.verbose(
- 'Output file has a PNG extension, setting the image format to PNG.'
- );
- imageFormat = 'png';
- }
- }
-
- CliInternals.validateImageFormat(imageFormat, outName);
- }
+ const imageFormat = getImageFormat({
+ downloadName,
+ outName: cliImageFormat,
+ configImageFormat: ConfigInternals.getUserPreferredImageFormat() ?? null,
+ cliFlag: CliInternals.parsedCli['image-format'] ?? null,
+ });
try {
const res = await renderStillOnLambda({
functionName,
serveUrl,
inputProps,
- imageFormat: imageFormat as StillImageFormat,
+ imageFormat,
composition,
privacy,
region: getAwsRegion(),
@@ -105,7 +170,7 @@ export const stillCommand = async (args: string[]) => {
frame: stillFrame,
quality,
logLevel,
- outName: parsedLambdaCli['out-name'],
+ outName,
chromiumOptions,
timeoutInMilliseconds: puppeteerTimeout,
scale,
@@ -117,11 +182,11 @@ export const stillCommand = async (args: string[]) => {
);
Log.verbose(`CloudWatch logs (if enabled): ${res.cloudWatchLogs}`);
- if (outName) {
+ if (downloadName) {
Log.info('Finished rendering. Downloading...');
const {outputPath, sizeInBytes} = await downloadMedia({
bucketName: res.bucketName,
- outPath: outName,
+ outPath: downloadName,
region: getAwsRegion(),
renderId: res.renderId,
});