Skip to content

🎥 Make videos programmatically with React

document setImageFormat

Jonny Burger8a0ea04423fc
diff --git a/packages/cli/src/image-formats.ts b/packages/cli/src/image-formats.ts
index 816f9f27d9..5150d301bb 100644
--- a/packages/cli/src/image-formats.ts
+++ b/packages/cli/src/image-formats.ts
@@ -1,6 +1,11 @@
-import {Codec, ImageFormat} from 'remotion';
+import {Codec, ImageFormat, Internals} from 'remotion';
export const getImageFormat = (codec: Codec): ImageFormat => {
+ const userPreferred = Internals.getUserPreferredImageFormat();
+
+ if (typeof userPreferred !== 'undefined') {
+ return userPreferred;
+ }
if (
codec === 'h264' ||
codec === 'h265' ||
diff --git a/packages/cli/src/parse-command-line.ts b/packages/cli/src/parse-command-line.ts
index acbe1d092e..8ecb1bffe5 100644
--- a/packages/cli/src/parse-command-line.ts
+++ b/packages/cli/src/parse-command-line.ts
@@ -1,8 +1,9 @@
import minimist from 'minimist';
-import {Codec, Config, PixelFormat} from 'remotion';
+import {Codec, Config, ImageFormat, PixelFormat} from 'remotion';
export type CommandLineOptions = {
pixelFormat: PixelFormat;
+ imageFormat: ImageFormat;
concurrency: number;
overwrite: boolean;
config: string;
@@ -21,6 +22,9 @@ export const parseCommandLine = () => {
if (parsedCli.pixelFormat) {
Config.Output.setPixelFormat(parsedCli.pixelFormat);
}
+ if (parsedCli.imageFormat) {
+ Config.Rendering.setImageFormat(parsedCli.imageFormat);
+ }
if (parsedCli.concurrency) {
Config.Rendering.setConcurrency(parsedCli.concurrency);
}
diff --git a/packages/core/src/config/image-format.ts b/packages/core/src/config/image-format.ts
index 519c54cd37..8f3212ad38 100644
--- a/packages/core/src/config/image-format.ts
+++ b/packages/core/src/config/image-format.ts
@@ -1,3 +1,20 @@
const validOptions = ['png', 'jpeg'] as const;
export type ImageFormat = typeof validOptions[number];
+
+let currentImageFormat: ImageFormat | undefined = undefined;
+
+export const setImageFormat = (format: ImageFormat) => {
+ if (typeof format === undefined) {
+ currentImageFormat = undefined;
+ return;
+ }
+ if (!validOptions.includes(format)) {
+ throw new TypeError(`Value ${format} is not valid as an image format.`);
+ }
+ currentImageFormat = format;
+};
+
+export const getUserPreferredImageFormat = () => {
+ return currentImageFormat;
+};
diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts
index 1b135c29de..48ed8e83d8 100644
--- a/packages/core/src/config/index.ts
+++ b/packages/core/src/config/index.ts
@@ -1,7 +1,7 @@
import {setCodec, setOutputFormat} from './codec';
import {Concurrency, setConcurrency} from './concurrency';
import {setCrf} from './crf';
-import {ImageFormat} from './image-format';
+import {ImageFormat, setImageFormat} from './image-format';
import {setImageSequence} from './image-sequence';
import {
overrideWebpackConfig,
@@ -34,6 +34,11 @@ export const Config = {
* Default: 80
*/
setQuality,
+ /**
+ * Decide in which image format to render. Can be either 'jpeg' or 'png'.
+ * PNG is slower, but supports transparency.
+ */
+ setImageFormat,
},
Output: {
/**
diff --git a/packages/core/src/config/pixel-format.ts b/packages/core/src/config/pixel-format.ts
index 43ecbb333b..9a2d58dcb8 100644
--- a/packages/core/src/config/pixel-format.ts
+++ b/packages/core/src/config/pixel-format.ts
@@ -13,7 +13,7 @@ let currentPixelFormat: PixelFormat = 'yuv420p';
export const setPixelFormat = (format: PixelFormat) => {
if (!validOptions.includes(format)) {
- throw new Error(`Value ${format} is not valid as a pixel format.`);
+ throw new TypeError(`Value ${format} is not valid as a pixel format.`);
}
currentPixelFormat = format;
};
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index 6f970cc223..6b1063e546 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -15,6 +15,7 @@ import {
getDefaultCrfForCodec,
validateSelectedCrf,
} from './config/crf';
+import {getUserPreferredImageFormat} from './config/image-format';
import {getShouldOutputImageSequence} from './config/image-sequence';
import {
getWebpackOverrideFn,
@@ -54,6 +55,7 @@ export const Internals = {
DEFAULT_CODEC,
getDefaultCrfForCodec,
getActualCrf,
+ getUserPreferredImageFormat,
};
export type {
diff --git a/packages/docs/docs/cli.md b/packages/docs/docs/cli.md
index a66850aeff..75424d68e3 100644
--- a/packages/docs/docs/cli.md
+++ b/packages/docs/docs/cli.md
@@ -16,12 +16,13 @@ Besides choosing a video and output location with the command line arguments, th
- `--props`: React Props to pass to the root component of your video. Must be a serialized JSON string.
- `--concurrency`: [How many CPU threads to use.](config#setconcurrency) Minimum 1. The maximum is the amount of threads you have.
- `--pixel-format`: [Set a custom pixel format. See here for available values.](config#setpixelformat)
-- `--config`: Specify a location for the Remotion config file.
+- `--image-format`: [`jpeg` or `png` - JPEG is faster, but supports transparency.](config#setimageformat) The default image format is `jpeg` since v1.1. Flag available since v1.4.
+- `--config`: Specify a location for the Remotion config file. Available in v1.2 and later.
- `--quality`: [Value between 0 and 100 for JPEG rendering quality](config#setquality). Doesn't work when PNG frames are rendered.
- `--overwrite`: [Write to output even if file already exists.](config#setoverwriteoutput)
-- `--sequence`: [Pass this flag if you want an image sequence as the output instead of a video.](config#setimagesequence)
-- `--codec`: [`h264` or `h265` or `png` or `vp8` or `vp9`](config#setoutputformat). If you don't supply `--codec`, it will use the H.264 encoder.
-- `--crf`: [To set Constant Rate Factor (CRF) of the output](config#setcrf). Minimum 0. Use this rate control mode if you want to keep the best quality and care less about the file size.
+- `--sequence`: [Pass this flag if you want an image sequence as the output instead of a video.](config#setimagesequence) Available since v1.4.
+- `--codec`: [`h264` or `h265` or `png` or `vp8` or `vp9`](config#setoutputformat). If you don't supply `--codec`, it will use the H.264 encoder. Available since v1.4.
+- `--crf`: [To set Constant Rate Factor (CRF) of the output](config#setcrf). Minimum 0. Use this rate control mode if you want to keep the best quality and care less about the file size. Available since v1.4.
:::info
If you don't feel like passing command line flags every time, consider creating a `remotion.config.ts` [config file](config).
diff --git a/packages/docs/docs/config.md b/packages/docs/docs/config.md
index 610729cea7..ed1bf41d36 100644
--- a/packages/docs/docs/config.md
+++ b/packages/docs/docs/config.md
@@ -63,10 +63,24 @@ Default value: `yuv420p`
Config.Output.setPixelFormat('yuv420p')
```
-The [command line flag](cli) `--pixel-format` will take precedene over this option.
+The [command line flag](cli) `--pixel-format` will take precedence over this option.
+
+### setImageFormat()
+
+_Available from Version 1.4._
+
+Determines which in which image format to render the frames. Either `jpeg` _(default since v1.1)_ or `png`. PNG is considerably slower, but supports transparency.
+
+```tsx
+Config.Output.setImageFormat('png')
+```
+
+The [command line flag](cli) `--image-format` will take precedence over this option.
### setCodec()
+_Available from Version 1.4._
+
Choose one of the supported codecs: `h264` _(default)_, `h265`, `vp8`, `vp9`.
- `h264` is the classic MP4 file as you know it.
@@ -80,13 +94,15 @@ Config.Output.setCodec('h265');
### setImageSequence()
+_Available from Version 1.4._
+
Set to true if you want to output an image sequence instead of a video.
```tsx
Config.Output.setImageSequence(true);
```
-### setOutputFormat()
+### ~~setOutputFormat()~~
_Deprecated_. Use `setCodec()` and `setImageSequence()` instead.
@@ -110,6 +126,8 @@ The [command line flag](cli) `--quality` will take precedence over this option.
### setCrf()
+_Available from Version 1.4._
+
The "Constant Rate Factor" (CRF) of the output. [Use this setting to tell FFMPEG how to trade off size and quality.](encoding#controlling-quality-using-the-crf-setting)
Ranges for CRF scale, by codec: