🎥 Make videos programmatically with React
more documentation
Jonny Burgerba328fe51219
diff --git a/packages/docs/docs/webcodecs/TableOfContents.tsx b/packages/docs/docs/webcodecs/TableOfContents.tsxindex 4a853facd7..3e75b992ac 100644--- a/packages/docs/docs/webcodecs/TableOfContents.tsx+++ b/packages/docs/docs/webcodecs/TableOfContents.tsx@@ -44,6 +44,14 @@ export const TableOfContents: React.FC = () => {is specified.</div></TOCItem>+ <TOCItem link="/docs/webcodecs/default-on-audio-track-handler">+ <strong>{'defaultOnAudioTrackHandler()'}</strong>+ <div>The default track transformation function for audio tracks.</div>+ </TOCItem>+ <TOCItem link="/docs/webcodecs/default-on-video-track-handler">+ <strong>{'defaultOnVideoTrackHandler()'}</strong>+ <div>The default track transformation function for video tracks.</div>+ </TOCItem></Grid></div>);diff --git a/packages/docs/docs/webcodecs/convert-media.mdx b/packages/docs/docs/webcodecs/convert-media.mdxindex dd7ee4df13..252acbf91d 100644--- a/packages/docs/docs/webcodecs/convert-media.mdx+++ b/packages/docs/docs/webcodecs/convert-media.mdx@@ -51,13 +51,17 @@ The container format to convert to. Currently, only `"webm"` is supported._string_ <TsType type="ConvertMediaVideoCodec" source="@remotion/webcodecs"/>-The video codec to convert to. Currently, only `"vp8"` and `"vp9"` are supported.+The video codec to convert to. Currently, only `"vp8"` and `"vp9"` are supported.+The default is defined by [`getDefaultVideoCodec()`](/docs/webcodecs/get-default-video-codec).+If a [`onVideoTrack`](#onvideotrack) handler is provided, it will override this setting.### `audioCodec?`_string_ <TsType type="ConvertMediaAudioCodec" source="@remotion/webcodecs"/>-The audio codec to convert to. Currently, only `"opus"` is supported.+The audio codec to convert to. Currently, only `"opus"` is supported.+The default is defined by [`getDefaultAudioCodec()`](/docs/webcodecs/get-default-audio-codec).+If an [`onAudioTrack`](#onaudiotrack) handler is provided, it will override this setting.### `logLevel?`@@ -176,14 +180,14 @@ By default the writer is `webFsWriter`._Function_ <TsType type="ConvertMediaOnAudioTrackHandler" source="@remotion/webcodecs"/>Take control of what to do for each audio track in the file: Re-encoding, copying, or dropping.-The API for this feature is not yet documented as it is subject to change.+See [Track Transformation](/docs/webcodecs/track-transformation) for a guide on how to use this callback.### `onVideoTrack?`_Function_ <TsType type="ConvertMediaOnVideoTrackHandler" source="@remotion/webcodecs"/>Take control of what to do for each video track in the file: Re-encoding, copying, or dropping.-The API for this feature is not yet documented as it is subject to change.+See [Track Transformation](/docs/webcodecs/track-transformation) for a guide on how to use this callback.### `fields?` and Callbacksdiff --git a/packages/docs/docs/webcodecs/default-on-audio-track-handler.mdx b/packages/docs/docs/webcodecs/default-on-audio-track-handler.mdxindex 9ebbe95c4f..17d1120e47 100644--- a/packages/docs/docs/webcodecs/default-on-audio-track-handler.mdx+++ b/packages/docs/docs/webcodecs/default-on-audio-track-handler.mdx@@ -1,14 +1,45 @@----image: /generated/articles-docs-webcodecs-default-audio-track-handler.png+image: /generated/articles-docs-webcodecs-default-on-audio-track-handler.pngid: default-on-audio-track-handlertitle: defaultOnAudioTrackHandler()slug: /webcodecs/default-on-audio-track-handlercrumb: '@remotion/webcodecs'----This is the default function if no [`onAudioTrack`](/docs/webcodecs/convert-media#onaudiotrack) handler is provided to [`convertMedia()`](/docs/webcodecs/convert-media).+This is the default function if no [`onAudioTrack`](/docs/webcodecs/convert-media#onaudiotrack) handler is provided to [`convertMedia()`](/docs/webcodecs/convert-media).+You may use this function if you want to customize part of the track transformation logic, but fall back to the default behavior for the rest.-```tsx twoslash+```tsx twoslash title="Falling back to the default behavior"+// @module: es2022+// @target: es2017+import {convertMedia, defaultOnVideoTrackHandler} from '@remotion/webcodecs';++await convertMedia({+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',+ container: 'webm',+ onVideoTrack: (params) => {+ // Custom logic for handling video tracks+ // ...++ // Fall back to the default behavior+ return defaultOnVideoTrackHandler(params);+ },+});+```++## Algorithm++The default behavior is as follows:++- Check if the track can be copied without re-encoding, if true, then do that.+- Determine the audio codec to be used - either [`audioCodec`](/docs/webcodecs/convert-media#audiocodec) which was passed to [`convertMedia()`](/docs/webcodecs/convert-media) or the default codec for the container.+- Check if the track can be re-encoded with the chosen audio codec and bitrate, if true, then do that.+- If the track can be neither copied nor re-encoded, then fail the render.+ You may alternatively return `{type: 'drop'}` to remove the audio track, but still succeed the other tracks.++This is the source code for the default function. You may use this as a reference to create your own custom handler.++```tsx twoslash title="Source code for defaultOnAudioTrackHandler"import {canCopyAudioTrack,canReencodeAudioTrack,@@ -27,7 +58,6 @@ export const defaultOnAudioTrackHandler: ConvertMediaOnAudioTrackHandler =container,}): Promise<AudioOperation> => {const bitrate = DEFAULT_BITRATE;- const audioCodec = defaultAudioCodec ?? getDefaultAudioCodec({container});const canCopy = canCopyAudioTrack({inputCodec: track.codecWithoutConfig,@@ -38,6 +68,7 @@ export const defaultOnAudioTrackHandler: ConvertMediaOnAudioTrackHandler =return Promise.resolve({type: 'copy'});}+ const audioCodec = defaultAudioCodec ?? getDefaultAudioCodec({container});const canReencode = await canReencodeAudioTrack({audioCodec,track,@@ -55,3 +86,8 @@ export const defaultOnAudioTrackHandler: ConvertMediaOnAudioTrackHandler =return Promise.resolve({type: 'fail'});};```++## See also++- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/webcodecs/src/default-on-audio-track-handler.ts)+- [`convertMedia()`](/docs/webcodecs/convert-media)diff --git a/packages/docs/docs/webcodecs/default-on-video-track-handler.mdx b/packages/docs/docs/webcodecs/default-on-video-track-handler.mdxindex 0a1ed94c5b..617e4dad72 100644--- a/packages/docs/docs/webcodecs/default-on-video-track-handler.mdx+++ b/packages/docs/docs/webcodecs/default-on-video-track-handler.mdx@@ -1,11 +1,42 @@----image: /generated/articles-docs-webcodecs-default-video-track-handler.png+image: /generated/articles-docs-webcodecs-default-on-video-track-handler.pngid: default-on-video-track-handler-title: defaultVideoTrackHandler()+title: defaultOnVideoTrackHandler()slug: /webcodecs/default-on-video-track-handlercrumb: '@remotion/webcodecs'---+This is the default function if no [`onVideoTrack`](/docs/webcodecs/convert-media#onvideotrack) handler is provided to [`convertMedia()`](/docs/webcodecs/convert-media).+You may use this function if you want to customize part of the track transformation logic, but fall back to the default behavior for the rest.++```tsx twoslash title="Falling back to the default behavior"+// @module: es2022+// @target: es2017+import {convertMedia, defaultOnAudioTrackHandler} from '@remotion/webcodecs';++await convertMedia({+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',+ container: 'webm',+ onAudioTrack: (params) => {+ // Custom logic for handling video tracks+ // ...++ // Fall back to the default behavior+ return defaultOnAudioTrackHandler(params);+ },+});+```++## Algorithm++The default behavior is as follows:++- Check if the track can be copied without re-encoding, if true, then do that.+- Determine the video codec to be used - either [`videoCodec`](/docs/webcodecs/convert-media#videocodec) which was passed to [`convertMedia()`](/docs/webcodecs/convert-media) or the default codec for the container.+- Check if the track can be re-encoded with the chosen video codec, if true, then do that.+- If the track can be neither copied nor re-encoded, then fail the render.+ You may alternatively return `{type: 'drop'}` to remove the video track, but still succeed the other tracks.+```tsx twoslash title="Source code for defaultOnVideoTrackHandler"import {canReencodeVideoTrack,@@ -22,8 +53,6 @@ export const defaultOnVideoTrackHandler: ConvertMediaOnVideoTrackHandler =logLevel,container,}): Promise<VideoOperation> => {- const videoCodec = defaultVideoCodec ?? getDefaultVideoCodec({container});-const canCopy = canCopyVideoTrack({inputCodec: track.codecWithoutConfig,container,@@ -33,6 +62,7 @@ export const defaultOnVideoTrackHandler: ConvertMediaOnVideoTrackHandler =return Promise.resolve({type: 'copy'});}+ const videoCodec = defaultVideoCodec ?? getDefaultVideoCodec({container});const canReencode = await canReencodeVideoTrack({videoCodec,track,@@ -45,3 +75,8 @@ export const defaultOnVideoTrackHandler: ConvertMediaOnVideoTrackHandler =return Promise.resolve({type: 'fail'});};```++## See also++- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/webcodecs/src/default-on-video-track-handler.ts)+- [`convertMedia()`](/docs/webcodecs/convert-media)diff --git a/packages/docs/docs/webcodecs/track-selection.mdx b/packages/docs/docs/webcodecs/track-transformation.mdxsimilarity index 82%rename from packages/docs/docs/webcodecs/track-selection.mdxrename to packages/docs/docs/webcodecs/track-transformation.mdxindex 8f8369cd34..e0111eb76d 100644--- a/packages/docs/docs/webcodecs/track-selection.mdx+++ b/packages/docs/docs/webcodecs/track-transformation.mdx@@ -1,5 +1,5 @@----image: /generated/articles-docs-webcodecs-track-selection.png+image: /generated/articles-docs-webcodecs-track-transformation.pngid: track-transformationtitle: Track Transformationslug: /webcodecs/track-transformation@@ -29,6 +29,8 @@ await convertMedia({});```+The default codecs are defined by [`getDefaultAudioCodec()`](/docs/webcodecs/get-default-audio-codec) and [`getDefaultVideoCodec()`](/docs/webcodecs/get-default-video-codec).+## Choosing codecsYou can use the [`videoCodec`](/docs/webcodecs/convert-media#videocodec) and [`audioCodec`](/docs/webcodecs/convert-media#audiocodec) options to transform all tracks to the codecs you specify.@@ -150,6 +152,28 @@ While the operations are unresolved, reading of the input fill is paused.## Falling back to default+The default values for [`onVideoTrack`](/docs/webcodecs/convert-media#onvideotrack) and [`onAudioTrack`](/docs/webcodecs/convert-media#onaudiotrack) are the functions [`defaultOnVideoTrackHandler`](/docs/webcodecs/default-on-video-track-handler) and [`defaultOnAudioTrackHandler`](/docs/webcodecs/default-on-audio-track-handler) respectively.++If you only want to override part of the logic, you can return the default resolver functions at the end of your logic.++```tsx twoslash title="Falling back to the default behavior"+// @module: es2022+// @target: es2017+import {convertMedia, defaultOnAudioTrackHandler} from '@remotion/webcodecs';++await convertMedia({+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',+ container: 'webm',+ onAudioTrack: (params) => {+ // Custom logic for handling video tracks+ // ...++ // Fall back to the default behavior+ return defaultOnAudioTrackHandler(params);+ },+});+```+## DebuggingPass [`logLevel: "verbose"`](/docs/webcodecs/convert-media#loglevel) to [`convertMedia()`](/docs/convert-media) to see debug information in the console, including how the defaults have decided which operations to take.diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex 289b34618f..84214b1374 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -3957,17 +3957,17 @@ export const articles = [crumb: '@remotion/webcodecs',},{- id: 'default-audio-track-handler',- title: 'defaultAudioTrackHandler()',- relativePath: 'docs/webcodecs/default-audio-track-handler.mdx',- compId: 'articles-docs-webcodecs-default-audio-track-handler',+ id: 'default-on-audio-track-handler',+ title: 'defaultOnAudioTrackHandler()',+ relativePath: 'docs/webcodecs/default-on-audio-track-handler.mdx',+ compId: 'articles-docs-webcodecs-default-on-audio-track-handler',crumb: '@remotion/webcodecs',},{- id: 'default-video-track-handler',- title: 'defaultVideoTrackHandler()',- relativePath: 'docs/webcodecs/default-video-track-handler.mdx',- compId: 'articles-docs-webcodecs-default-video-track-handler',+ id: 'default-on-video-track-handler',+ title: 'defaultOnVideoTrackHandler()',+ relativePath: 'docs/webcodecs/default-on-video-track-handler.mdx',+ compId: 'articles-docs-webcodecs-default-on-video-track-handler',crumb: '@remotion/webcodecs',},{@@ -3994,8 +3994,8 @@ export const articles = [{id: 'track-transformation',title: 'Track Transformation',- relativePath: 'docs/webcodecs/track-selection.mdx',- compId: 'articles-docs-webcodecs-track-selection',+ relativePath: 'docs/webcodecs/track-transformation.mdx',+ compId: 'articles-docs-webcodecs-track-transformation',crumb: '@remotion/webcodecs',},{diff --git a/packages/docs/static/generated/articles-docs-webcodecs-default-on-audio-track-handler.png b/packages/docs/static/generated/articles-docs-webcodecs-default-on-audio-track-handler.pngnew file mode 100644index 0000000000..68177b0170Binary files /dev/null and b/packages/docs/static/generated/articles-docs-webcodecs-default-on-audio-track-handler.png differdiff --git a/packages/webcodecs/src/default-on-audio-track-handler.ts b/packages/webcodecs/src/default-on-audio-track-handler.tsindex 3bb1256669..bd9971878f 100644--- a/packages/webcodecs/src/default-on-audio-track-handler.ts+++ b/packages/webcodecs/src/default-on-audio-track-handler.ts@@ -17,7 +17,6 @@ export const defaultOnAudioTrackHandler: ConvertMediaOnAudioTrackHandler =container,}): Promise<AudioOperation> => {const bitrate = DEFAULT_BITRATE;- const audioCodec = defaultAudioCodec ?? getDefaultAudioCodec({container});const canCopy = canCopyAudioTrack({inputCodec: track.codecWithoutConfig,@@ -33,6 +32,8 @@ export const defaultOnAudioTrackHandler: ConvertMediaOnAudioTrackHandler =return Promise.resolve({type: 'copy'});}+ const audioCodec = defaultAudioCodec ?? getDefaultAudioCodec({container});+const canReencode = await canReencodeAudioTrack({audioCodec,track,diff --git a/packages/webcodecs/src/default-on-video-track-handler.ts b/packages/webcodecs/src/default-on-video-track-handler.tsindex 76db0ccfac..b051fd7e36 100644--- a/packages/webcodecs/src/default-on-video-track-handler.ts+++ b/packages/webcodecs/src/default-on-video-track-handler.ts@@ -14,8 +14,6 @@ export const defaultOnVideoTrackHandler: ConvertMediaOnVideoTrackHandler =logLevel,container,}): Promise<VideoOperation> => {- const videoCodec = defaultVideoCodec ?? getDefaultVideoCodec({container});-const canCopy = canCopyVideoTrack({inputCodec: track.codecWithoutConfig,container,@@ -30,6 +28,8 @@ export const defaultOnVideoTrackHandler: ConvertMediaOnVideoTrackHandler =return Promise.resolve({type: 'copy'});}+ const videoCodec = defaultVideoCodec ?? getDefaultVideoCodec({container});+const canReencode = await canReencodeVideoTrack({videoCodec,track,diff --git a/packages/webcodecs/src/index.ts b/packages/webcodecs/src/index.tsindex 7c03cd0029..f972621689 100644--- a/packages/webcodecs/src/index.ts+++ b/packages/webcodecs/src/index.ts@@ -19,6 +19,7 @@ export {convertMedia,} from './convert-media';export {defaultOnAudioTrackHandler} from './default-on-audio-track-handler';+export {defaultOnVideoTrackHandler} from './default-on-video-track-handler';export {getDefaultAudioCodec} from './get-default-audio-codec';export {getDefaultVideoCodec} from './get-default-video-codec';export {