🎥 Make videos programmatically with React
alright
Jonny Burgera340f704641f
diff --git a/packages/docs/docs/webcodecs/TableOfContents.tsx b/packages/docs/docs/webcodecs/TableOfContents.tsxindex 8ba5ff5987..228f633488 100644--- a/packages/docs/docs/webcodecs/TableOfContents.tsx+++ b/packages/docs/docs/webcodecs/TableOfContents.tsx@@ -24,6 +24,12 @@ export const TableOfContents: React.FC = () => {Determine if a video track can be copied without re-encoding</div></TOCItem>+ <TOCItem link="/docs/webcodecs/can-copy-audio-track">+ <strong>{'canCopyAudioTrack()'}</strong>+ <div>+ Determine if a audio track can be copied without re-encoding+ </div>+ </TOCItem></Grid></div>);diff --git a/packages/docs/docs/webcodecs/convert-media.mdx b/packages/docs/docs/webcodecs/convert-media.mdxindex d983d28345..282cc58d36 100644--- a/packages/docs/docs/webcodecs/convert-media.mdx+++ b/packages/docs/docs/webcodecs/convert-media.mdx@@ -23,9 +23,7 @@ import {convertMedia} from '@remotion/webcodecs';await convertMedia({src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',- to: 'webm',- videoCodec: 'vp8',- audioCodec: 'opus',+ container: 'webm',});```@@ -49,13 +47,13 @@ _string_ <TsType type="ConvertMediaContainer" source="@remotion/webcodecs"/>The container format to convert to. Currently, only `"webm"` is supported.-### `videoCodec`+### `videoCodec?`_string_ <TsType type="ConvertMediaVideoCodec" source="@remotion/webcodecs"/>The video codec to convert to. Currently, only `"vp8"` and `"vp9"` are supported.-### `audioCodec`+### `audioCodec?`_string_ <TsType type="ConvertMediaAudioCodec" source="@remotion/webcodecs"/>diff --git a/packages/docs/docs/webcodecs/track-selection.mdx b/packages/docs/docs/webcodecs/track-selection.mdxnew file mode 100644index 0000000000..58305da047--- /dev/null+++ b/packages/docs/docs/webcodecs/track-selection.mdx@@ -0,0 +1,65 @@+---+id: track-transformation+title: Track Transformation+slug: /webcodecs/track-transformation+crumb: '@remotion/webcodecs'+---++When transforming media, there are multiple thing that can be done for each audio or video track:++- Copying the track without re-encoding+- Re-encoding the track into a different codec+- Removing the track++[`@remotion/webcodecs`](/docs/webcodecs) allows you to decide for each track what to do with it.++## Selecting default codecs++The easiest way to configure [`convertMedia()`](/docs/webcodecs/convert-media) is to use the [`videoCodec`](/docs/webcodecs/convert-media#videocodec) and [`audioCodec`](/docs/webcodecs/convert-media#audiocodec) options.++```tsx twoslash+// @module: es2022+// @target: es2017+import {convertMedia} from '@remotion/webcodecs';++await convertMedia({+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',+ container: 'webm',+ videoCodec: 'vp8',+ audioCodec: 'opus',+});+```++These options will be used for all tracks that are not explicitly handled in the [`onVideoTrack`](/docs/webcodecs/convert-media#onvideotrack) and [`onAudioTrack`](/docs/webcodecs/convert-media#onaudiotrack) callbacks.++## Handle each track individually++```tsx twoslash+// @module: es2022+// @target: es2017+import {convertMedia} from '@remotion/webcodecs';++await convertMedia({+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',+ container: 'webm',+ videoCodec: 'vp8',+ audioCodec: 'opus',+ onVideoTrack: ({track}) => {+ if (track.codecWithoutConfig === 'vp9') {+ return {type: 'copy'};+ }++ if (track.codecWithoutConfig === 'h264') {+ return {type: 'reencode', videoCodec: 'vp8'};+ }++ return {type: 'drop'};+ },+});+```++## Asynchronously determining config++## Falling back to default++## Debuggingdiff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex e049a23f54..84d872efed 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -574,6 +574,7 @@ module.exports = {},items: ['webcodecs/index',+ 'webcodecs/track-transformation','webcodecs/convert-media','webcodecs/can-reencode-audio-track','webcodecs/can-reencode-video-track',diff --git a/packages/webcodecs/src/convert-media.ts b/packages/webcodecs/src/convert-media.tsindex 6e04d4e4c3..54ca20084f 100644--- a/packages/webcodecs/src/convert-media.ts+++ b/packages/webcodecs/src/convert-media.ts@@ -74,8 +74,8 @@ export const convertMedia = async function <container: ConvertMediaContainer;onVideoFrame?: ConvertMediaOnVideoFrame;onMediaStateUpdate?: ConvertMediaOnMediaStateUpdate;- videoCodec: ConvertMediaVideoCodec;- audioCodec: ConvertMediaAudioCodec;+ videoCodec?: ConvertMediaVideoCodec;+ audioCodec?: ConvertMediaAudioCodec;signal?: AbortSignal;onAudioTrack?: ResolveAudioActionFn;onVideoTrack?: ResolveVideoActionFn;@@ -99,7 +99,7 @@ export const convertMedia = async function <);}- if (videoCodec !== 'vp8' && videoCodec !== 'vp9') {+ if (videoCodec && videoCodec !== 'vp8' && videoCodec !== 'vp9') {return Promise.reject(new TypeError('Only `videoCodec: "vp8"` and `videoCodec: "vp9"` are supported currently',@@ -171,7 +171,7 @@ export const convertMedia = async function <abortConversion,convertMediaState,controller,- videoCodec,+ defaultVideoCodec: videoCodec ?? null,onVideoTrack: userVideoResolver ?? null,logLevel,container,diff --git a/packages/webcodecs/src/on-audio-track.ts b/packages/webcodecs/src/on-audio-track.tsindex dbea813f42..9132decfde 100644--- a/packages/webcodecs/src/on-audio-track.ts+++ b/packages/webcodecs/src/on-audio-track.ts@@ -34,7 +34,7 @@ export const makeAudioTrackHandler =}): OnAudioTrack =>async (track) => {const audioOperation = await (onAudioTrack ?? defaultResolveAudioAction)({- audioCodec,+ defaultAudioCodec: audioCodec,track,logLevel,container,diff --git a/packages/webcodecs/src/on-video-track.ts b/packages/webcodecs/src/on-video-track.tsindex 131fe51a5c..71782f85c8 100644--- a/packages/webcodecs/src/on-video-track.ts+++ b/packages/webcodecs/src/on-video-track.ts@@ -24,7 +24,7 @@ export const makeVideoTrackHandler =abortConversion,convertMediaState,controller,- videoCodec,+ defaultVideoCodec,onVideoTrack,logLevel,container,@@ -35,7 +35,7 @@ export const makeVideoTrackHandler =abortConversion: (errCause: Error) => void;convertMediaState: ConvertMediaState;controller: AbortController;- videoCodec: ConvertMediaVideoCodec;+ defaultVideoCodec: ConvertMediaVideoCodec | null;onVideoTrack: ResolveVideoActionFn | null;logLevel: LogLevel;container: ConvertMediaContainer;@@ -47,7 +47,7 @@ export const makeVideoTrackHandler =const videoOperation = await (onVideoTrack ?? defaultResolveVideoAction)({track,- videoCodec,+ defaultVideoCodec,logLevel,container,});@@ -103,7 +103,7 @@ export const makeVideoTrackHandler =color: track.color,width: track.codedWidth,height: track.codedHeight,- codec: videoCodec,+ codec: videoOperation.videoCodec,codecPrivate: null,});diff --git a/packages/webcodecs/src/resolve-audio-action.ts b/packages/webcodecs/src/resolve-audio-action.tsindex 3fbef13252..4f3653d7f2 100644--- a/packages/webcodecs/src/resolve-audio-action.ts+++ b/packages/webcodecs/src/resolve-audio-action.ts@@ -12,7 +12,7 @@ export type AudioOperation =export type ResolveAudioActionFn = (options: {track: AudioTrack;- audioCodec: ConvertMediaAudioCodec;+ defaultAudioCodec: ConvertMediaAudioCodec | null;logLevel: LogLevel;container: ConvertMediaContainer;}) => AudioOperation | Promise<AudioOperation>;@@ -21,15 +21,20 @@ const DEFAULT_BITRATE = 128_000;export const defaultResolveAudioAction: ResolveAudioActionFn = async ({track,- audioCodec,+ defaultAudioCodec,logLevel,container,}): Promise<AudioOperation> => {const bitrate = DEFAULT_BITRATE;+ if (defaultAudioCodec === null) {+ throw new Error(+ 'No default video codec provided to convertMedia(). Pass a `audioCodec` parameter to set one.',+ );+ }const canCopy = canCopyAudioTrack({inputCodec: track.codecWithoutConfig,- outputCodec: audioCodec,+ outputCodec: defaultAudioCodec,container,});@@ -43,7 +48,7 @@ export const defaultResolveAudioAction: ResolveAudioActionFn = async ({}const canReencode = await canReencodeAudioTrack({- audioCodec,+ audioCodec: defaultAudioCodec,track,bitrate,});@@ -54,7 +59,11 @@ export const defaultResolveAudioAction: ResolveAudioActionFn = async ({`Track ${track.trackId} (audio): Can re-encode = ${canReencode}, can copy = ${canCopy}, action = reencode`,);- return Promise.resolve({type: 'reencode', bitrate, audioCodec});+ return Promise.resolve({+ type: 'reencode',+ bitrate,+ audioCodec: defaultAudioCodec,+ });}Log.verbose(diff --git a/packages/webcodecs/src/resolve-video-action.ts b/packages/webcodecs/src/resolve-video-action.tsindex aa618816d1..8928a67a9d 100644--- a/packages/webcodecs/src/resolve-video-action.ts+++ b/packages/webcodecs/src/resolve-video-action.ts@@ -11,7 +11,7 @@ export type VideoOperation =| {type: 'drop'};export type ResolveVideoActionFn = (options: {- videoCodec: ConvertMediaVideoCodec;+ defaultVideoCodec: ConvertMediaVideoCodec | null;track: VideoTrack;logLevel: LogLevel;container: ConvertMediaContainer;@@ -19,13 +19,19 @@ export type ResolveVideoActionFn = (options: {export const defaultResolveVideoAction: ResolveVideoActionFn = async ({track,- videoCodec,+ defaultVideoCodec,logLevel,container,}): Promise<VideoOperation> => {+ if (defaultVideoCodec === null) {+ throw new Error(+ 'No default video codec provided to convertMedia(). Pass a `videoCodec` parameter to set one.',+ );+ }+const canCopy = canCopyVideoTrack({inputCodec: track.codecWithoutConfig,- outputCodec: videoCodec,+ outputCodec: defaultVideoCodec,container,});@@ -38,7 +44,10 @@ export const defaultResolveVideoAction: ResolveVideoActionFn = async ({return Promise.resolve({type: 'copy'});}- const canReencode = await canReencodeVideoTrack({videoCodec, track});+ const canReencode = await canReencodeVideoTrack({+ videoCodec: defaultVideoCodec,+ track,+ });if (canReencode) {Log.verbose(@@ -46,7 +55,7 @@ export const defaultResolveVideoAction: ResolveVideoActionFn = async ({`Track ${track.trackId} (video): Cannot copy, but re-enconde, therefore re-encoding`,);- return Promise.resolve({type: 'reencode', videoCodec});+ return Promise.resolve({type: 'reencode', videoCodec: defaultVideoCodec});}Log.verbose(