🎥 Make videos programmatically with React
"fail" operation
Jonny Burgerba0469780002
diff --git a/packages/docs/docs/webcodecs/track-selection.mdx b/packages/docs/docs/webcodecs/track-selection.mdxindex 7ce63f1afe..55d35e3a04 100644--- a/packages/docs/docs/webcodecs/track-selection.mdx+++ b/packages/docs/docs/webcodecs/track-selection.mdx@@ -98,7 +98,7 @@ await convertMedia({src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',container: 'webm',audioCodec: 'opus',- onVideoTrack: ({track, container, defaultVideoCodec}) => {+ onVideoTrack: ({track, container}) => {const canCopy = canCopyVideoTrack({inputCodec: track.codecWithoutConfig,container,@@ -113,8 +113,45 @@ await convertMedia({});```+## Checking if a track can be re-encoded++To check if it is possible to return `{"type": "reencode"}`, you can use the [`canReencodeVideoTrack()`](/docs/webcodecs/convert-media#canreencodevideotrack) and [`canReencodeAudioTrack()`](/docs/webcodecs/convert-media#canreencodeaudiotrack) APIs.++```tsx twoslash title="Using the canReencodeVideoTrack() API"+// @module: es2022+// @target: es2017+import {convertMedia, canReencodeVideoTrack} from '@remotion/webcodecs';++await convertMedia({+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',+ container: 'webm',+ audioCodec: 'opus',+ onVideoTrack: async ({track, container}) => {+ const canReencode = await canReencodeVideoTrack({+ videoCodec: 'vp8',+ track,+ });++ if (canReencode) {+ return {type: 'reencode', videoCodec: 'vp8'};+ }++ return {type: 'drop'};+ },+});+```+## Asynchronously determining config+The [`onAudioTrack`](/docs/webcodecs/convert-media#onaudiotrack) and [`onVideoTrack`](/docs/webcodecs/convert-media#onvideotrack) callbacks can be asynchronous.+While the operations are unresolved, reading of the input fill is paused.+## Falling back to default## Debugging++Pass [`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.++## Reference implementation++Visit the [source code](https://github.com/remotion-dev/remotion/tree/main/packages/convert) for [convert.remotion.dev](https://convert.remotion.dev) to see a reference implementation for an online video converter that displays a user interface for all possible options.diff --git a/packages/webcodecs/src/on-audio-track.ts b/packages/webcodecs/src/on-audio-track.tsindex 9132decfde..953cd277c1 100644--- a/packages/webcodecs/src/on-audio-track.ts+++ b/packages/webcodecs/src/on-audio-track.ts@@ -44,6 +44,12 @@ export const makeAudioTrackHandler =return null;}+ if (audioOperation.type === 'fail') {+ throw new Error(+ `Audio track with ID ${track.trackId} could resolved with {"type": "fail"}. This could mean that this audio track could neither be copied to the output container or re-encoded. You have the option to drop the track instead of failing it: https://remotion.dev/docs/webcodecs/track-transformation`,+ );+ }+if (audioOperation.type === 'copy') {const addedTrack = await state.addTrack({type: 'audio',diff --git a/packages/webcodecs/src/on-video-track.ts b/packages/webcodecs/src/on-video-track.tsindex 71782f85c8..a76f78ff2d 100644--- a/packages/webcodecs/src/on-video-track.ts+++ b/packages/webcodecs/src/on-video-track.ts@@ -56,6 +56,12 @@ export const makeVideoTrackHandler =return null;}+ if (videoOperation.type === 'fail') {+ throw new Error(+ `Video track with ID ${track.trackId} could resolved with {"type": "fail"}. This could mean that this video track could neither be copied to the output container or re-encoded. You have the option to drop the track instead of failing it: https://remotion.dev/docs/webcodecs/track-transformation`,+ );+ }+if (videoOperation.type === 'copy') {const videoTrack = await state.addTrack({type: 'video',diff --git a/packages/webcodecs/src/resolve-audio-action.ts b/packages/webcodecs/src/resolve-audio-action.tsindex 9500af429d..85c4a35f2a 100644--- a/packages/webcodecs/src/resolve-audio-action.ts+++ b/packages/webcodecs/src/resolve-audio-action.ts@@ -8,6 +8,7 @@ import {Log} from './log';export type AudioOperation =| {type: 'reencode'; bitrate: number; audioCodec: ConvertMediaAudioCodec}| {type: 'copy'}+ | {type: 'fail'}| {type: 'drop'};export type ResolveAudioActionFn = (options: {@@ -70,6 +71,5 @@ export const defaultResolveAudioAction: ResolveAudioActionFn = async ({`Track ${track.trackId} (audio): Can re-encode = ${canReencode}, can copy = ${canCopy}, action = drop`,);- // TODO: Make a fail option?- return Promise.resolve({type: 'drop'});+ return Promise.resolve({type: 'fail'});};diff --git a/packages/webcodecs/src/resolve-video-action.ts b/packages/webcodecs/src/resolve-video-action.tsindex b52834b427..7f057b9b59 100644--- a/packages/webcodecs/src/resolve-video-action.ts+++ b/packages/webcodecs/src/resolve-video-action.ts@@ -8,7 +8,8 @@ import {Log} from './log';export type VideoOperation =| {type: 'reencode'; videoCodec: ConvertMediaVideoCodec}| {type: 'copy'}- | {type: 'drop'};+ | {type: 'drop'}+ | {type: 'fail'};export type ResolveVideoActionFn = (options: {defaultVideoCodec: ConvertMediaVideoCodec | null;@@ -62,6 +63,5 @@ export const defaultResolveVideoAction: ResolveVideoActionFn = async ({`Track ${track.trackId} (video): Can neither copy nor re-encode, therefore dropping`,);- // TODO: Make a fail option?- return Promise.resolve({type: 'drop'});+ return Promise.resolve({type: 'fail'});};