🎥 Make videos programmatically with React
added support for h265 codec
Shankhadeep Dey0ca2161de7d1
diff --git a/packages/cli/src/get-filename.ts b/packages/cli/src/get-filename.tsindex 4d882ccac3..d1e7f09681 100644--- a/packages/cli/src/get-filename.ts+++ b/packages/cli/src/get-filename.ts@@ -16,13 +16,24 @@ export const getOutputFilename = (renderMode: OutputFormat): string => {console.info('User has given a file without extension , adding the extension automatically.');- if (renderMode === 'mp4') {+ if (+ renderMode === 'mp4' ||+ renderMode === 'mp4-h264' ||+ renderMode === 'mp4-h265'+ ) {filename += '.mp4';}if (renderMode === 'webm-v8' || renderMode === 'webm-v9') {filename += '.webm';}}+ if (extension === 'mp4') {+ if (renderMode === 'mp4') {+ console.info(+ 'You have specified a .mp4 extension, encoding it using the default H264 codec. To use H265 codec use --format=mp4-h265 flag.'+ );+ }+ }if (extension === 'webm') {if (renderMode === 'mp4') {console.info(@@ -32,7 +43,11 @@ export const getOutputFilename = (renderMode: OutputFormat): string => {renderMode = 'webm-v8';}}- if (renderMode === 'mp4') {+ if (+ renderMode === 'mp4' ||+ renderMode === 'mp4-h264' ||+ renderMode === 'mp4-h265'+ ) {if (hasExtension && extension !== 'mp4') {console.error('The output filename must end in .mp4.');process.exit(1);diff --git a/packages/cli/src/image-formats.ts b/packages/cli/src/image-formats.tsindex f6e57fcce2..4c42e4627b 100644--- a/packages/cli/src/image-formats.ts+++ b/packages/cli/src/image-formats.ts@@ -3,7 +3,7 @@ import {OutputFormat} from 'remotion';export type FrameFormat = 'png' | 'jpeg';export const getFrameFormat = (format: OutputFormat): FrameFormat => {- if (format === 'mp4') {+ if (format === 'mp4' || format === 'mp4-h264' || format === 'mp4-h265') {return 'jpeg';}if (format === 'png' || format === 'webm-v8' || format === 'webm-v9') {diff --git a/packages/cli/src/parse-command-line.ts b/packages/cli/src/parse-command-line.tsindex 7ae5a397d3..53fbd4da9c 100644--- a/packages/cli/src/parse-command-line.ts+++ b/packages/cli/src/parse-command-line.ts@@ -27,6 +27,7 @@ export const parseCommandLine = () => {console.warn('--png has been deprecated. Use --format=png instead.');}if (parsedCli.format) {+ console.log(parsedCli.format);Config.Output.setOutputFormat(parsedCli.format);}if (typeof parsedCli.overwrite !== 'undefined') {diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsxindex 05f6c2ae91..71754544da 100644--- a/packages/cli/src/render.tsx+++ b/packages/cli/src/render.tsx@@ -43,6 +43,8 @@ export const render = async () => {}if (renderMode === 'mp4' ||+ renderMode === 'mp4-h264' ||+ renderMode === 'mp4-h265' ||renderMode === 'webm-v8' ||renderMode === 'webm-v9') {@@ -120,6 +122,8 @@ export const render = async () => {}if (renderMode === 'mp4' ||+ renderMode === 'mp4-h264' ||+ renderMode === 'mp4-h265' ||renderMode === 'webm-v8' ||renderMode === 'webm-v9') {diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.tsindex a21bc40f08..057958e5b5 100644--- a/packages/core/src/config/index.ts+++ b/packages/core/src/config/index.ts@@ -45,8 +45,8 @@ export const Config = {*/setPixelFormat,/**- * Specify what kind of output you, want, either 'mp4' or 'png' or 'webm-v8' or 'webm-v8'.- * Default: 'mp4'+ * Specify what kind of output you, want, either 'mp4' (is an alias for 'mp4-h264') or 'mp4-h265' or 'png' or 'webm-v8' or 'webm-v8'.+ * Default: 'mp4' (uses the default h264 codec)*/setOutputFormat,},diff --git a/packages/core/src/config/render-mode.ts b/packages/core/src/config/render-mode.tsindex 7cb924dd3d..c6501cf72f 100644--- a/packages/core/src/config/render-mode.ts+++ b/packages/core/src/config/render-mode.ts@@ -1,4 +1,11 @@-const validRenderModes = ['png', 'mp4', 'webm-v8', 'webm-v9'] as const;+const validRenderModes = [+ 'png',+ 'mp4',+ 'mp4-h264',+ 'mp4-h265',+ 'webm-v8',+ 'webm-v9',+] as const;export type OutputFormat = typeof validRenderModes[number];diff --git a/packages/docs/docs/cli.md b/packages/docs/docs/cli.mdindex d183c28704..65f5446496 100644--- a/packages/docs/docs/cli.md+++ b/packages/docs/docs/cli.md@@ -19,7 +19,7 @@ Besides choosing a video and output location with the command line arguments, th- `--config`: Specify a location for the Remotion config file.- `--quality`: [Value between 0 and 100 for JPEG rendering quality](config#setquality). Doesn't work when `--png` is set.- `--overwrite`: [Write to output even if file already exists.](config#setoverwriteoutput)-- `--format`: [Either `mp4` or `png` or `webm-v8` or `webm-v9`](config#setoutputformat). If you don't supply `--format` flag `--format=mp4` is the default behaviour.+- `--format`: [Either `mp4`(is an alias for 'mp4-h264') or 'mp4-h264' or 'mp4-h265' or `png` or `webm-v8` or `webm-v9`](config#setoutputformat). If you don't supply `--format` flag `--format=mp4` is the default behaviour which uses the default h264 codec.:::infoIf you supply `--png` and `--format` flag both, `--format` flag will take precedence over `--png` flag.diff --git a/packages/docs/docs/config.md b/packages/docs/docs/config.mdindex 62ff9234f6..cdb9452812 100644--- a/packages/docs/docs/config.md+++ b/packages/docs/docs/config.md@@ -67,7 +67,7 @@ The [command line flag](cli) `--pixel-format` will take precedene over this opti### setOutputFormat()-Either `'mp4'` or `'png'` or `'webm-v8'` or `'webm-v9'`. Use the PNG sequence option if you want transparency in your output.+Either `'mp4'` or `'mp4-h264'` or `'mp4-h265'` or `'png'` or `'webm-v8'` or `'webm-v9'`. Use the PNG sequence option if you want transparency in your output.```tsxConfig.output.setOutputFormat('png');diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.tsindex b096900794..522eba2382 100644--- a/packages/renderer/src/stitcher.ts+++ b/packages/renderer/src/stitcher.ts@@ -4,6 +4,17 @@ import {OutputFormat, PixelFormat} from 'remotion';import {DEFAULT_IMAGE_FORMAT, ImageFormat} from './image-format';import {validateFfmpeg} from './validate-ffmpeg';+const getCodecName = (outputFormat: OutputFormat): string => {+ if (outputFormat === 'mp4' || outputFormat === 'mp4-h264') {+ return 'libx264';+ }+ if (outputFormat === 'mp4-h265') {+ return 'libx265';+ }++ return outputFormat === 'webm-v8' ? 'libvpx' : 'libvpx-vp9';+};+export const stitchFramesToVideo = async (options: {dir: string;fps: number;@@ -13,7 +24,7 @@ export const stitchFramesToVideo = async (options: {force: boolean;imageFormat?: ImageFormat;pixelFormat?: PixelFormat;- outputFormat?: OutputFormat;+ outputFormat: OutputFormat;}): Promise<void> => {const format = options.imageFormat ?? DEFAULT_IMAGE_FORMAT;await validateFfmpeg();@@ -40,11 +51,7 @@ export const stitchFramesToVideo = async (options: {'-i',`element-%0${numberLength}d.${format}`,'-c:v',- options.outputFormat === 'mp4'- ? 'libx264'- : options.outputFormat === 'webm-v8'- ? 'libvpx'- : 'libvpx-vp9',+ getCodecName(options.outputFormat),'-crf','16','-b:v',