Skip to content

Commit

Permalink
Merge pull request #4495 from remotion-dev/docs
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyBurger authored Nov 8, 2024
2 parents a848ffd + 99801f8 commit 62e96c1
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 48 deletions.
6 changes: 6 additions & 0 deletions packages/docs/docs/webcodecs/TableOfContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export const TableOfContents: React.FC = () => {
<strong>{'canReencodeAudioTrack()'}</strong>
<div>Determine if a audio track can be re-encoded</div>
</TOCItem>
<TOCItem link="/docs/webcodecs/can-copy-video-track">
<strong>{'canCopyVideoTrack()'}</strong>
<div>
Determine if a video track can be copied without re-encoding
</div>
</TOCItem>
</Grid>
</div>
);
Expand Down
98 changes: 98 additions & 0 deletions packages/docs/docs/webcodecs/can-copy-video-track.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
id: can-copy-video-track
title: canCopyVideoTrack()
slug: /webcodecs/can-copy-video-track
crumb: '@remotion/webcodecs'
---

_Part of the [`@remotion/webcodecs`](/docs/webcodecs) package._

:::warning
**Unstable API**: This package is experimental. We might change the API at any time, until we remove this notice.
:::

Given a `VideoTrack`, determine if it can be copied to the output without re-encoding.

You can obtain a `VideoTrack` using [`parseMedia()`](/docs/media-parser/parse-media) or during the conversion process using the [`onVideoTrack`](/docs/webcodecs/convert-media#onvideotrack) callback of [`convertMedia()`](/docs/webcodecs/convert-media).

## Examples

```tsx twoslash title="Check if a video tracks can be copied"
// @module: es2022
// @target: es2017
import {parseMedia} from '@remotion/media-parser';
import {canCopyVideoTrack} from '@remotion/webcodecs';

const {videoTracks} = await parseMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
fields: {
tracks: true,
},
});

for (const track of videoTracks) {
canCopyVideoTrack({
inputCodec: track.codecWithoutConfig,
outputCodec: 'vp8',
container: 'webm',
}); // boolean
}
```

```tsx twoslash title="Copy a video track to VP8, otherwise drop it"
// @module: es2022
// @target: es2017

import {convertMedia, canCopyVideoTrack} from '@remotion/webcodecs';

await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
videoCodec: 'vp8',
audioCodec: 'opus',
onVideoTrack: async ({track}) => {
const canCopy = canCopyVideoTrack({
inputCodec: track.codecWithoutConfig,
outputCodec: 'vp8',
container: 'webm',
});

if (canCopy) {
return {type: 'copy'};
}

// In reality, you would re-encode the track here
return {type: 'drop'};
},
});
```

## API

### `inputCodec`

_string_ <TsType type="ConvertMediaVideoCodec" source="@remotion/webcodecs" />

The codec of the input video track.

### `outputCodec`

_string_ <TsType type="ConvertMediaVideoCodec" source="@remotion/webcodecs" />

The codec of the output video track.

### `container`

_string_ <TsType type="ConvertMediaContainer" source="@remotion/webcodecs" />

The container format of the output file

## Return value

Returns a `boolean`.

## See also

- [Source code for this function on GitHub](https://github.com/remotion-dev/remotion/blob/main/packages/webcodecs/src/can-copy-video-track.ts)
- [`canReencodeVideoTrack()`](/docs/webcodecs/can-reencode-video-track)
- [`convertMedia()`](/docs/webcodecs/convert-media)
2 changes: 1 addition & 1 deletion packages/docs/docs/webcodecs/can-reencode-audio-track.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You can obtain an `AudioTrack` using [`parseMedia()`](/docs/media-parser/parse-m

## Examples

```tsx twoslash title="Check if an audio track can be re-encoded to Opus"
```tsx twoslash title="Check if audio tracks can be re-encoded to Opus"
// @module: es2022
// @target: es2017

Expand Down
2 changes: 1 addition & 1 deletion packages/docs/docs/webcodecs/can-reencode-video-track.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You can obtain a `VideoTrack` using [`parseMedia()`](/docs/media-parser/parse-me

## Examples

```tsx twoslash title="Check if a video tracks can be re-encoded to VP8"
```tsx twoslash title="Check if video tracks can be re-encoded to VP8"
// @module: es2022
// @target: es2017

Expand Down
1 change: 1 addition & 0 deletions packages/docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ module.exports = {
'webcodecs/convert-media',
'webcodecs/can-reencode-video-track',
'webcodecs/can-reencode-audio-track',
'webcodecs/can-copy-video-track',
],
},
{
Expand Down
19 changes: 19 additions & 0 deletions packages/webcodecs/src/can-copy-audio-track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type {MediaParserAudioCodec} from '@remotion/media-parser';
import type {ConvertMediaAudioCodec} from './codec-id';
import type {ConvertMediaContainer} from './convert-media';

export const canCopyAudioTrack = ({
inputCodec,
outputCodec,
container,
}: {
inputCodec: MediaParserAudioCodec;
outputCodec: ConvertMediaAudioCodec;
container: ConvertMediaContainer;
}) => {
if (outputCodec === 'opus') {
return inputCodec === 'opus' && container === 'webm';
}

throw new Error(`Unhandled codec: ${outputCodec satisfies never}`);
};
23 changes: 23 additions & 0 deletions packages/webcodecs/src/can-copy-video-track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {MediaParserVideoCodec} from '@remotion/media-parser';
import type {ConvertMediaVideoCodec} from './codec-id';
import type {ConvertMediaContainer} from './convert-media';

export const canCopyVideoTrack = ({
inputCodec,
outputCodec,
container,
}: {
inputCodec: MediaParserVideoCodec;
outputCodec: ConvertMediaVideoCodec;
container: ConvertMediaContainer;
}) => {
if (outputCodec === 'vp8') {
return inputCodec === 'vp8' && container === 'webm';
}

if (outputCodec === 'vp9') {
return inputCodec === 'vp9' && container === 'webm';
}

throw new Error(`Unhandled codec: ${outputCodec satisfies never}`);
};
2 changes: 2 additions & 0 deletions packages/webcodecs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export {WebCodecsAudioDecoder, createAudioDecoder} from './audio-decoder';
export {WebCodecsAudioEncoder, createAudioEncoder} from './audio-encoder';
export {canCopyAudioTrack} from './can-copy-audio-track';
export {canCopyVideoTrack} from './can-copy-video-track';
export {canReencodeAudioTrack} from './can-reencode-audio-track';
export {canReencodeVideoTrack} from './can-reencode-video-track';
export {ConvertMediaAudioCodec, ConvertMediaVideoCodec} from './codec-id';
Expand Down
23 changes: 2 additions & 21 deletions packages/webcodecs/src/resolve-audio-action.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type {
AudioTrack,
LogLevel,
MediaParserAudioCodec,
} from '@remotion/media-parser';
import type {AudioTrack, LogLevel} from '@remotion/media-parser';
import {canCopyAudioTrack} from './can-copy-audio-track';
import {canReencodeAudioTrack} from './can-reencode-audio-track';
import type {ConvertMediaAudioCodec} from './codec-id';
import type {ConvertMediaContainer} from './convert-media';
Expand All @@ -13,22 +10,6 @@ export type AudioOperation =
| {type: 'copy'}
| {type: 'drop'};

const canCopyAudioTrack = ({
inputCodec,
outputCodec,
container,
}: {
inputCodec: MediaParserAudioCodec;
outputCodec: ConvertMediaAudioCodec;
container: ConvertMediaContainer;
}) => {
if (outputCodec === 'opus') {
return inputCodec === 'opus' && container === 'webm';
}

throw new Error(`Unhandled codec: ${outputCodec satisfies never}`);
};

export type ResolveAudioActionFn = (options: {
track: AudioTrack;
audioCodec: ConvertMediaAudioCodec;
Expand Down
27 changes: 2 additions & 25 deletions packages/webcodecs/src/resolve-video-action.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type {
LogLevel,
MediaParserVideoCodec,
VideoTrack,
} from '@remotion/media-parser';
import type {LogLevel, VideoTrack} from '@remotion/media-parser';
import {canCopyVideoTrack} from './can-copy-video-track';
import {canReencodeVideoTrack} from './can-reencode-video-track';
import type {ConvertMediaVideoCodec} from './codec-id';
import type {ConvertMediaContainer} from './convert-media';
Expand All @@ -13,26 +10,6 @@ export type VideoOperation =
| {type: 'copy'}
| {type: 'drop'};

const canCopyVideoTrack = ({
inputCodec,
outputCodec,
container,
}: {
inputCodec: MediaParserVideoCodec;
outputCodec: ConvertMediaVideoCodec;
container: ConvertMediaContainer;
}) => {
if (outputCodec === 'vp8') {
return inputCodec === 'vp8' && container === 'webm';
}

if (outputCodec === 'vp9') {
return inputCodec === 'vp9' && container === 'webm';
}

throw new Error(`Unhandled codec: ${outputCodec satisfies never}`);
};

export type ResolveVideoActionFn = (options: {
videoCodec: ConvertMediaVideoCodec;
track: VideoTrack;
Expand Down

0 comments on commit 62e96c1

Please sign in to comment.