-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4494 from remotion-dev/webcodecs-handler
- Loading branch information
Showing
23 changed files
with
459 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
image: /generated/articles-docs-webcodecs-can-reencode-audio-track.png | ||
id: can-reencode-audio-track | ||
title: canReencodeAudioTrack() | ||
slug: /webcodecs/can-reencode-audio-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 an `AudioTrack`, determine if it can be re-encoded to another track. | ||
|
||
You can obtain an `AudioTrack` using [`parseMedia()`](/docs/media-parser/parse-media) or during the conversion process using the [`onAudioTrack`](/docs/webcodecs/convert-media#onaudiotrack) callback of [`convertMedia()`](/docs/webcodecs/convert-media). | ||
|
||
## Examples | ||
|
||
```tsx twoslash title="Check if an audio track can be re-encoded to Opus" | ||
// @module: es2022 | ||
// @target: es2017 | ||
|
||
import {parseMedia} from '@remotion/media-parser'; | ||
import {canReencodeAudioTrack} from '@remotion/webcodecs'; | ||
|
||
const {audioTracks} = await parseMedia({ | ||
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', | ||
fields: { | ||
tracks: true, | ||
}, | ||
}); | ||
|
||
for (const track of audioTracks) { | ||
await canReencodeAudioTrack({ | ||
track, | ||
audioCodec: 'opus', | ||
bitrate: 128000, | ||
}); | ||
} | ||
``` | ||
|
||
```tsx twoslash title="Convert an audio track to Opus, otherwise drop it" | ||
// @module: es2022 | ||
// @target: es2017 | ||
import {convertMedia, canReencodeAudioTrack} from '@remotion/webcodecs'; | ||
|
||
await convertMedia({ | ||
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', | ||
container: 'webm', | ||
videoCodec: 'vp8', | ||
audioCodec: 'opus', | ||
onAudioTrack: async ({track}) => { | ||
const canReencode = await canReencodeAudioTrack({ | ||
track, | ||
audioCodec: 'opus', | ||
bitrate: 128000, | ||
}); | ||
|
||
if (canReencode) { | ||
return {type: 'reencode', audioCodec: 'opus', bitrate: 128000}; | ||
} | ||
|
||
return {type: 'drop'}; | ||
}, | ||
}); | ||
``` | ||
|
||
## API | ||
|
||
### `track` | ||
|
||
A `AudioTrack` object. | ||
|
||
### `audioCodec` | ||
|
||
_string_ <TsType type="ConvertMediaAudioCodec" source="@remotion/webcodecs" /> | ||
|
||
### `bitrate` | ||
|
||
_number_ | ||
|
||
The bitrate with which you'd like to re-encode the audio track. | ||
|
||
## Return value | ||
|
||
Returns a `Promise<boolean>`. | ||
|
||
## See also | ||
|
||
- [Source code for this function on GitHub](https://github.com/remotion-dev/remotion/blob/main/packages/webcodecs/src/can-reencode-audio-track.ts) | ||
- [`canReencodeVideoTrack()`](/docs/webcodecs/can-reencode-video-track) | ||
- [`convertMedia()`](/docs/webcodecs/convert-media) | ||
- [`parseMedia()`](/docs/media-parser/parse-media) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
--- | ||
image: /generated/articles-docs-webcodecs-can-reencode-video-track.png | ||
id: can-reencode-video-track | ||
title: canReencodeVideoTrack() | ||
slug: /webcodecs/can-reencode-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 re-encoded to another track. | ||
|
||
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 re-encoded to VP8" | ||
// @module: es2022 | ||
// @target: es2017 | ||
|
||
import {parseMedia} from '@remotion/media-parser'; | ||
import {canReencodeVideoTrack} 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) { | ||
await canReencodeVideoTrack({ | ||
track, | ||
videoCodec: 'vp8', | ||
}); | ||
} | ||
``` | ||
|
||
```tsx twoslash title="Convert a video track to VP8, otherwise drop it" | ||
// @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', | ||
videoCodec: 'vp8', | ||
audioCodec: 'opus', | ||
onVideoTrack: async ({track}) => { | ||
const canReencode = await canReencodeVideoTrack({ | ||
track, | ||
videoCodec: 'vp8', | ||
}); | ||
|
||
if (canReencode) { | ||
return {type: 'reencode', videoCodec: 'vp8'}; | ||
} | ||
|
||
return {type: 'drop'}; | ||
}, | ||
}); | ||
``` | ||
|
||
## API | ||
|
||
### `track` | ||
|
||
A `VideoTrack` object. | ||
|
||
### `videoCodec` | ||
|
||
_string_ <TsType type="ConvertMediaVideoCodec" source="@remotion/webcodecs" /> | ||
|
||
One of the supported video codecs: `"vp8"`, `"vp9"`. | ||
|
||
## Return value | ||
|
||
Returns a `Promise<boolean>`. | ||
|
||
## See also | ||
|
||
- [Source code for this function on GitHub](https://github.com/remotion-dev/remotion/blob/main/packages/webcodecs/src/can-reencode-video-track.ts) | ||
- [`convertMedia()`](/docs/webcodecs/convert-media) | ||
- [`parseMedia()`](/docs/media-parser/parse-media) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+48.1 KB
...ages/docs/static/generated/articles-docs-webcodecs-can-reencode-audio-track.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+48 KB
...ages/docs/static/generated/articles-docs-webcodecs-can-reencode-video-track.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+45.2 KB
packages/docs/static/generated/articles-docs-webcodecs-convert-media.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type {AudioTrack} from '@remotion/media-parser'; | ||
import {getAudioDecoderConfig} from './audio-decoder-config'; | ||
import {getAudioEncoderConfig} from './audio-encoder-config'; | ||
import type {ConvertMediaAudioCodec} from './codec-id'; | ||
|
||
export const canReencodeAudioTrack = async ({ | ||
track, | ||
audioCodec, | ||
bitrate, | ||
}: { | ||
track: AudioTrack; | ||
audioCodec: ConvertMediaAudioCodec; | ||
bitrate: number; | ||
}): Promise<boolean> => { | ||
const audioDecoderConfig = await getAudioDecoderConfig({ | ||
codec: track.codec, | ||
numberOfChannels: track.numberOfChannels, | ||
sampleRate: track.sampleRate, | ||
description: track.description, | ||
}); | ||
|
||
const audioEncoderConfig = await getAudioEncoderConfig({ | ||
codec: audioCodec, | ||
numberOfChannels: track.numberOfChannels, | ||
sampleRate: track.sampleRate, | ||
bitrate, | ||
}); | ||
|
||
return Boolean(audioDecoderConfig && audioEncoderConfig); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type {VideoTrack} from '@remotion/media-parser'; | ||
import type {ConvertMediaVideoCodec} from './codec-id'; | ||
import {getVideoDecoderConfigWithHardwareAcceleration} from './video-decoder-config'; | ||
import {getVideoEncoderConfig} from './video-encoder-config'; | ||
|
||
export const canReencodeVideoTrack = async ({ | ||
videoCodec, | ||
track, | ||
}: { | ||
videoCodec: ConvertMediaVideoCodec; | ||
track: VideoTrack; | ||
}) => { | ||
const videoEncoderConfig = await getVideoEncoderConfig({ | ||
codec: videoCodec, | ||
height: track.displayAspectHeight, | ||
width: track.displayAspectWidth, | ||
}); | ||
const videoDecoderConfig = | ||
await getVideoDecoderConfigWithHardwareAcceleration(track); | ||
|
||
return Boolean(videoDecoderConfig && videoEncoderConfig); | ||
}; |
Oops, something went wrong.