Skip to content

🎥 Make videos programmatically with React

jpeg quality

Jonny Burgerd5aa9a099aeb
diff --git a/packages/cli/src/benchmark.ts b/packages/cli/src/benchmark.ts
index 494d67fe3b..28cac5ccde 100644
--- a/packages/cli/src/benchmark.ts
+++ b/packages/cli/src/benchmark.ts
@@ -28,6 +28,7 @@ const {
offthreadVideoCacheSizeInBytesOption,
scaleOption,
crfOption,
+ jpegQualityOption,
} = BrowserSafeApis.options;
const getValidConcurrency = (cliConcurrency: number | string | null) => {
@@ -177,7 +178,6 @@ export const benchmarkCommand = async (
proResProfile,
frameRange: defaultFrameRange,
overwrite,
- jpegQuality,
pixelFormat,
numberOfGifLoops,
everyNthFrame,
@@ -305,6 +305,9 @@ export const benchmarkCommand = async (
commandLine: parsedCli,
});
const configFileCrf = crfOption.getValue({commandLine: parsedCli}).value;
+ const jpegQuality = jpegQualityOption.getValue({
+ commandLine: parsedCli,
+ }).value;
for (const composition of compositions) {
const {codec, reason: codecReason} = getFinalOutputCodec({
diff --git a/packages/cli/src/config/index.ts b/packages/cli/src/config/index.ts
index 834f0ffdb9..f6277aaf27 100644
--- a/packages/cli/src/config/index.ts
+++ b/packages/cli/src/config/index.ts
@@ -23,7 +23,6 @@ import {
setVideoImageFormat,
} from './image-format';
import {getShouldOutputImageSequence} from './image-sequence';
-import {getJpegQuality} from './jpeg-quality';
import * as Logging from './log';
import {getOutputLocation} from './output-location';
import {
@@ -94,7 +93,6 @@ import {
import {setFrameRange} from './frame-range';
import {getHeight, overrideHeight} from './height';
import {setImageSequence} from './image-sequence';
-import {setJpegQuality} from './jpeg-quality';
import {
getKeyboardShortcutsEnabled,
setKeyboardShortcutsEnabled,
@@ -131,6 +129,7 @@ const {
audioBitrateOption,
scaleOption,
crfOption,
+ jpegQualityOption,
} = BrowserSafeApis.options;
declare global {
@@ -411,7 +410,7 @@ declare global {
| 'slower'
| 'veryslow'
| 'placebo'
- | undefined,
+ | null,
) => void;
/**
* Override the arguments that Remotion passes to FFmpeg.
@@ -584,15 +583,13 @@ export const Config: FlatConfig = {
'Config.setImageFormat() has been renamed - use Config.setVideoImageFormat() instead (default "jpeg"). For rendering stills, use Config.setStillImageFormat() (default "png")',
);
},
- setJpegQuality,
+ setJpegQuality: jpegQualityOption.setConfig,
setStillImageFormat,
setVideoImageFormat,
setEncodingMaxRate,
setEncodingBufferSize,
setFrameRange,
- setScale: (option) => {
- scaleOption.setConfig(option);
- },
+ setScale: scaleOption.setConfig,
setEveryNthFrame,
setNumberOfGifLoops,
setMuted,
@@ -601,14 +598,10 @@ export const Config: FlatConfig = {
setOverwriteOutput,
setPixelFormat,
setCodec,
- setCrf: (crf) => {
- crfOption.setConfig(crf);
- },
+ setCrf: crfOption.setConfig,
setImageSequence,
setProResProfile,
- setX264Preset: (preset) => {
- x264Option.setConfig(preset ?? null);
- },
+ setX264Preset: x264Option.setConfig,
setAudioBitrate: (bitrate) => {
audioBitrateOption.setConfig(bitrate);
},
@@ -645,7 +638,6 @@ export const ConfigInternals = {
getEveryNthFrame,
getConcurrency,
getCurrentPuppeteerTimeout,
- getJpegQuality,
getAudioCodec,
getStillFrame,
getShouldOutputImageSequence,
diff --git a/packages/cli/src/config/jpeg-quality.ts b/packages/cli/src/config/jpeg-quality.ts
deleted file mode 100644
index e88de7e44a..0000000000
--- a/packages/cli/src/config/jpeg-quality.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import {RenderInternals} from '@remotion/renderer';
-
-const defaultValue = RenderInternals.DEFAULT_JPEG_QUALITY;
-let quality: number = defaultValue;
-
-export const setJpegQuality = (q: number | undefined) => {
- RenderInternals.validateJpegQuality(q);
-
- if (q === 0 || q === undefined) {
- quality = defaultValue;
- return;
- }
-
- quality = q;
-};
-
-export const getJpegQuality = () => quality;
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.ts
index 159d4594d7..e64bff1cd4 100644
--- a/packages/cli/src/get-cli-options.ts
+++ b/packages/cli/src/get-cli-options.ts
@@ -118,7 +118,6 @@ export const getCliOptions = (options: {
shouldOutputImageSequence,
inputProps: getInputProps(null, options.logLevel),
envVariables: getEnvironmentVariables(null, options.logLevel, false),
- jpegQuality: ConfigInternals.getJpegQuality(),
pixelFormat,
proResProfile,
everyNthFrame,
diff --git a/packages/cli/src/get-render-defaults.ts b/packages/cli/src/get-render-defaults.ts
index 15ba7f64a5..536cf0bd7e 100644
--- a/packages/cli/src/get-render-defaults.ts
+++ b/packages/cli/src/get-render-defaults.ts
@@ -9,10 +9,13 @@ const {
audioBitrateOption,
offthreadVideoCacheSizeInBytesOption,
scaleOption,
+ jpegQualityOption,
} = BrowserSafeApis.options;
export const getRenderDefaults = (): RenderDefaults => {
- const defaultJpegQuality = ConfigInternals.getJpegQuality();
+ const defaultJpegQuality = jpegQualityOption.getValue({
+ commandLine: parsedCli,
+ }).value;
const logLevel = ConfigInternals.Logging.getLogLevel();
const defaultCodec = ConfigInternals.getOutputCodecOrUndefined();
const concurrency = RenderInternals.getActualConcurrency(
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index cc2a36e708..44b1d746c4 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -19,6 +19,7 @@ const {
offthreadVideoCacheSizeInBytesOption,
scaleOption,
crfOption,
+ jpegQualityOption,
} = BrowserSafeApis.options;
export const render = async (
@@ -57,7 +58,6 @@ export const render = async (
overwrite,
inputProps,
envVariables,
- jpegQuality,
browserExecutable,
chromiumOptions,
everyNthFrame,
@@ -92,6 +92,9 @@ export const render = async (
commandLine: parsedCli,
}).value;
const scale = scaleOption.getValue({commandLine: parsedCli}).value;
+ const jpegQuality = jpegQualityOption.getValue({
+ commandLine: parsedCli,
+ }).value;
const crf = shouldOutputImageSequence
? null
: crfOption.getValue({commandLine: parsedCli}).value;
diff --git a/packages/cli/src/still.ts b/packages/cli/src/still.ts
index 0d7bd7a712..81ffdcd97f 100644
--- a/packages/cli/src/still.ts
+++ b/packages/cli/src/still.ts
@@ -10,7 +10,7 @@ import {Log} from './log';
import {parsedCli} from './parse-command-line';
import {renderStillFlow} from './render-flows/still';
-const {offthreadVideoCacheSizeInBytesOption, scaleOption} =
+const {offthreadVideoCacheSizeInBytesOption, scaleOption, jpegQualityOption} =
BrowserSafeApis.options;
export const still = async (
@@ -51,7 +51,6 @@ export const still = async (
overwrite,
publicDir,
puppeteerTimeout,
- jpegQuality,
stillFrame,
width,
} = getCliOptions({
@@ -61,6 +60,11 @@ export const still = async (
logLevel,
});
+ const jpegQuality = jpegQualityOption.getValue({
+ commandLine: parsedCli,
+ }).value;
+ const scale = scaleOption.getValue({commandLine: parsedCli}).value;
+
await renderStillFlow({
remotionRoot,
entryPointReason,
@@ -82,9 +86,7 @@ export const still = async (
publicDir,
puppeteerTimeout,
jpegQuality,
- scale: scaleOption.getValue({
- commandLine: parsedCli,
- }).value,
+ scale,
stillFrame,
width,
compositionIdFromUi: null,
diff --git a/packages/cli/src/test/quality.test.ts b/packages/cli/src/test/quality.test.ts
index dded0a3b86..653f3d6291 100644
--- a/packages/cli/src/test/quality.test.ts
+++ b/packages/cli/src/test/quality.test.ts
@@ -1,24 +1,27 @@
+import {BrowserSafeApis} from '@remotion/renderer/client';
import {describe, expect, test} from 'vitest';
-import {getJpegQuality, setJpegQuality} from '../config/jpeg-quality';
import {expectToThrow} from './expect-to-throw';
+const setJpegQuality = BrowserSafeApis.options.jpegQualityOption.setConfig;
+const getJpegQuality = BrowserSafeApis.options.jpegQualityOption.getValue;
+
describe('Test valid setQuality inputs', () => {
test('Integers within accepted range', () => {
const validInputes = [1, 50, 100];
validInputes.forEach((entry) => {
setJpegQuality(entry);
- expect(getJpegQuality()).toEqual(entry);
+ expect(getJpegQuality({commandLine: {}})).toEqual(entry);
});
});
test('Undefined input', () => {
setJpegQuality(undefined);
- expect(getJpegQuality()).toEqual(80);
+ expect(getJpegQuality({commandLine: {}})).toEqual(80);
});
test('0 input', () => {
setJpegQuality(0);
- expect(getJpegQuality()).toEqual(80);
+ expect(getJpegQuality({commandLine: {}})).toEqual(80);
});
});
diff --git a/packages/cloudrun/src/cli/commands/render/index.ts b/packages/cloudrun/src/cli/commands/render/index.ts
index 256b55bf97..177f10cbb0 100644
--- a/packages/cloudrun/src/cli/commands/render/index.ts
+++ b/packages/cloudrun/src/cli/commands/render/index.ts
@@ -21,6 +21,7 @@ const {
offthreadVideoCacheSizeInBytesOption,
scaleOption,
crfOption,
+ jpegQualityOption,
} = BrowserSafeApis.options;
export const renderCommand = async (
@@ -59,7 +60,6 @@ export const renderCommand = async (
puppeteerTimeout,
pixelFormat,
proResProfile,
- jpegQuality,
everyNthFrame,
numberOfGifLoops,
muted,
@@ -202,6 +202,9 @@ ${downloadName ? ` Downloaded File = ${downloadName}` : ''}
const crf = crfOption.getValue({
commandLine: CliInternals.parsedCli,
}).value;
+ const jpegQuality = jpegQualityOption.getValue({
+ commandLine: CliInternals.parsedCli,
+ }).value;
const res = await internalRenderMediaOnCloudrun({
cloudRunUrl,
serviceName: undefined,
diff --git a/packages/cloudrun/src/cli/commands/still.ts b/packages/cloudrun/src/cli/commands/still.ts
index 4bf7d227f6..d4de75be37 100644
--- a/packages/cloudrun/src/cli/commands/still.ts
+++ b/packages/cloudrun/src/cli/commands/still.ts
@@ -13,7 +13,7 @@ import {renderArgsCheck} from './render/helpers/renderArgsCheck';
export const STILL_COMMAND = 'still';
-const {offthreadVideoCacheSizeInBytesOption, scaleOption} =
+const {offthreadVideoCacheSizeInBytesOption, scaleOption, jpegQualityOption} =
BrowserSafeApis.options;
export const stillCommand = async (
@@ -36,7 +36,6 @@ export const stillCommand = async (
envVariables,
inputProps,
puppeteerTimeout,
- jpegQuality,
stillFrame,
height,
width,
@@ -158,6 +157,9 @@ ${downloadName ? ` Downloaded File = ${downloadName}` : ''}
const scale = scaleOption.getValue({
commandLine: CliInternals.parsedCli,
}).value;
+ const jpegQuality = jpegQualityOption.getValue({
+ commandLine: CliInternals.parsedCli,
+ }).value;
const res = await renderStillOnCloudrun({
cloudRunUrl,
diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.ts
index a8a2ffde85..3e4cbca8cc 100644
--- a/packages/lambda/src/cli/commands/render/render.ts
+++ b/packages/lambda/src/cli/commands/render/render.ts
@@ -46,6 +46,7 @@ const {
offthreadVideoCacheSizeInBytesOption,
scaleOption,
crfOption,
+ jpegQualityOption,
} = BrowserSafeApis.options;
export const renderCommand = async (
@@ -76,7 +77,6 @@ export const renderCommand = async (
pixelFormat,
proResProfile,
puppeteerTimeout,
- jpegQuality,
everyNthFrame,
numberOfGifLoops,
muted,
@@ -112,6 +112,9 @@ export const renderCommand = async (
const crf = crfOption.getValue({
commandLine: CliInternals.parsedCli,
}).value;
+ const jpegQuality = jpegQualityOption.getValue({
+ commandLine: CliInternals.parsedCli,
+ }).value;
let composition: string = args[1];
if (!composition) {
diff --git a/packages/lambda/src/cli/commands/still.ts b/packages/lambda/src/cli/commands/still.ts
index 5afb7ede4c..0ec24f6df0 100644
--- a/packages/lambda/src/cli/commands/still.ts
+++ b/packages/lambda/src/cli/commands/still.ts
@@ -20,8 +20,12 @@ import {findFunctionName} from '../helpers/find-function-name';
import {quit} from '../helpers/quit';
import {Log} from '../log';
-const {offthreadVideoCacheSizeInBytesOption, scaleOption, deleteAfterOption} =
- BrowserSafeApis.options;
+const {
+ offthreadVideoCacheSizeInBytesOption,
+ scaleOption,
+ deleteAfterOption,
+ jpegQualityOption,
+} = BrowserSafeApis.options;
export const STILL_COMMAND = 'still';
@@ -49,7 +53,6 @@ export const stillCommand = async (
envVariables,
inputProps,
puppeteerTimeout,
- jpegQuality,
stillFrame,
height,
width,
@@ -148,6 +151,9 @@ export const stillCommand = async (
const scale = scaleOption.getValue({
commandLine: CliInternals.parsedCli,
}).value;
+ const jpegQuality = jpegQualityOption.getValue({
+ commandLine: CliInternals.parsedCli,
+ }).value;
const res = await renderStillOnLambda({
functionName,
diff --git a/packages/renderer/src/jpeg-quality.ts b/packages/renderer/src/jpeg-quality.ts
index 89263ab49e..eaa9390e47 100644
--- a/packages/renderer/src/jpeg-quality.ts
+++ b/packages/renderer/src/jpeg-quality.ts
@@ -1,6 +1,6 @@
export const DEFAULT_JPEG_QUALITY = 80;
-export const validateJpegQuality = (q: number | undefined) => {
+export const validateJpegQuality = (q: unknown) => {
if (typeof q !== 'undefined' && typeof q !== 'number') {
throw new Error(
`JPEG Quality option must be a number or undefined. Got ${typeof q} (${JSON.stringify(
diff --git a/packages/renderer/src/options/jpeg-quality.tsx b/packages/renderer/src/options/jpeg-quality.tsx
index aaceb4d3f1..7fbd0d8c4f 100644
--- a/packages/renderer/src/options/jpeg-quality.tsx
+++ b/packages/renderer/src/options/jpeg-quality.tsx
@@ -1,8 +1,27 @@
+import {DEFAULT_JPEG_QUALITY, validateJpegQuality} from '../jpeg-quality';
import type {AnyRemotionOption} from './option';
+const defaultValue = DEFAULT_JPEG_QUALITY;
+let quality: number = defaultValue;
+
+export const setJpegQuality = (q: number | undefined) => {
+ validateJpegQuality(q);
+
+ if (q === 0 || q === undefined) {
+ quality = defaultValue;
+ return;
+ }
+
+ quality = q;
+};
+
+export const getJpegQuality = () => quality;
+
+const cliFlag = 'jpeg-quality' as const;
+
export const jpegQualityOption = {
name: 'JPEG Quality',
- cliFlag: 'jpeg-quality' as const,
+ cliFlag,
description: () => (
<>
Sets the quality of the generated JPEG images. Must be an integer between
@@ -12,4 +31,26 @@ export const jpegQualityOption = {
ssrName: 'jpegQuality',
docLink: 'https://www.remotion.dev/docs/renderer/render-media#jpeg-quality',
type: 0 as number,
+ setConfig: setJpegQuality,
+ getValue: ({commandLine}) => {
+ if (commandLine[cliFlag] !== undefined) {
+ validateJpegQuality(commandLine[cliFlag]);
+ return {
+ source: 'cli',
+ value: commandLine[cliFlag] as number,
+ };
+ }
+
+ if (quality !== defaultValue) {
+ return {
+ source: 'config',
+ value: quality,
+ };
+ }
+
+ return {
+ source: 'default',
+ value: defaultValue,
+ };
+ },
} satisfies AnyRemotionOption<number>;