Remove input track descriptors, add async getters on tracks for everything, deprecate old sync getters

This commit is contained in:
Vanilagy
2026-04-10 16:23:17 +02:00
parent aa810fc433
commit 051578c2ca
42 changed files with 1386 additions and 1504 deletions
+3 -3
View File
@@ -306,7 +306,7 @@ const conversion = await Conversion.init({
output,
// Function gets invoked for each video track:
video: (videoTrack) => {
video: async (videoTrack) => {
if (videoTrack.number > 1) {
// Keep only the first video track
return { discard: true };
@@ -314,13 +314,13 @@ const conversion = await Conversion.init({
return {
// Shrink width to 640 only if the track is wider
width: Math.min(videoTrack.displayWidth, 640),
width: Math.min(await videoTrack.getDisplayWidth(), 640),
};
},
// Async functions work too:
audio: async (audioTrack) => {
if (audioTrack.languageCode !== 'rus') {
if (await audioTrack.getLanguageCode() !== 'rus') {
// Keep only Russian audio tracks
return { discard: true };
}
+5 -5
View File
@@ -18,9 +18,9 @@ const allTracks = await input.getTracks(); // List of all tracks
// Extract video metadata
const videoTrack = await input.getPrimaryVideoTrack();
if (videoTrack) {
videoTrack.displayWidth; // in pixels
videoTrack.displayHeight; // in pixels
videoTrack.rotation; // in degrees clockwise
await videoTrack.getDisplayWidth(); // in pixels
await videoTrack.getDisplayHeight(); // in pixels
await videoTrack.getRotation(); // in degrees clockwise
// Estimate frame rate (FPS)
const packetStats = await videoTrack.computePacketStats(100);
@@ -30,8 +30,8 @@ if (videoTrack) {
// Extract audio metadata
const audioTrack = await input.getPrimaryAudioTrack();
if (audioTrack) {
audioTrack.numberOfChannels;
audioTrack.sampleRate; // in Hz
await audioTrack.getNumberOfChannels();
await audioTrack.getSampleRate(); // in Hz
}
// Extract metadata tags
+15 -15
View File
@@ -111,21 +111,21 @@ track.isAudioTrack(); // => boolean
// Retrieve the track's language as an ISO 639-2/T language code.
// Resolves to 'und' (undetermined) if the language isn't known.
track.languageCode; // => string
await track.getLanguageCode(); // => string
// A user-defined name for this track.
track.name; // => string
await track.getName(); // => string | null
// Information about the intended usage of the track
// (default, commentary, hearing-impaired, visually-impaired, etc.)
track.disposition; // TrackDisposition
await track.getDisposition(); // TrackDisposition
```
#### Codec information
You can query metadata related to the track's codec:
```ts
track.codec; // => MediaCodec | null
await track.getCodec(); // => MediaCodec | null
```
This field is `null` when the track's codec couldn't be recognized or is not supported by Mediabunny. See [Codecs](./supported-formats-and-codecs#codecs) for the full list of supported codecs. When Mediabunny doesn't recognize the format, you can still use the `internalCodecId` field to figure out the codec of the track, although its format depends on the container format used and is not homogenized by Mediabunny.
@@ -167,7 +167,7 @@ A _negative start timestamp_ means the track begins *before* the composition doe
Another metric related to track timing info is its *time resolution*, which is given in hertz:
```ts
track.timeResolution; // => 24
await track.getTimeResolution(); // => 24
```
Intuitively, this is the maximum possible "frame rate" of the track (assuming that no two samples have the same timestamp). Mathematically, if $x$ is equal to a track's time resolution, then all timestamps and durations of that track can be expressed as:
@@ -217,25 +217,25 @@ In addition to the [common track metadata](#common-track-metadata), video tracks
```ts
// Get the raw pixel dimensions of the track's coded samples:
videoTrack.codedWidth; // => number
videoTrack.codedHeight; // => number
await videoTrack.getCodedWidth(); // => number
await videoTrack.getCodedHeight(); // => number
// Get the pixel dimensions of the track after aspect ratio adjustments,
// but before rotation:
videoTrack.squarePixelWidth; // => number
videoTrack.squarePixelHeight; // => number
await videoTrack.getSquarePixelWidth(); // => number
await videoTrack.getSquarePixelHeight(); // => number
// Get the displayed pixel dimensions of the track's samples, after
// aspect ratio adjustments and rotation:
videoTrack.displayWidth; // => number
videoTrack.displayHeight; // => number
await videoTrack.getDisplayWidth(); // => number
await videoTrack.getDisplayHeight(); // => number
// Get the clockwise rotation in degrees by which the
// track's frames should be rotated:
videoTrack.rotation; // => 0 | 90 | 180 | 270
await videoTrack.getRotation(); // => 0 | 90 | 180 | 270
// Get the aspect ratio of the track's pixels (usually 1:1):
videoTrack.pixelAspectRatio; // => { num: number, den: number }
await videoTrack.getPixelAspectRatio(); // => { num: number, den: number }
```
To compute a video track's average frame rate (FPS), use [`computePacketStats`](#packet-statistics):
@@ -284,10 +284,10 @@ In addition to the [common track metadata](#common-track-metadata), audio tracks
```ts
// Get the number of audio channels:
audioTrack.numberOfChannels; // => number
await audioTrack.getNumberOfChannels(); // => number
// Get the audio sample rate in hertz:
audioTrack.sampleRate; // => number
await audioTrack.getSampleRate(); // => number
```
You can retrieve the track's decoder configuration, which is an `AudioDecoderConfig` from the WebCodecs API for usage within `AudioDecoder`:
+5 -2
View File
@@ -189,10 +189,13 @@ const input = new Input({
const duration = await input.computeDuration();
const videoTrack = await input.getPrimaryVideoTrack();
const { displayWidth, displayHeight, rotation } = videoTrack;
const displayWidth = await videoTrack.getDisplayWidth();
const displayHeight = await videoTrack.getDisplayHeight();
const rotation = await videoTrack.getRotation();
const audioTrack = await input.getPrimaryAudioTrack();
const { sampleRate, numberOfChannels } = audioTrack;
const sampleRate = await audioTrack.getSampleRate();
const numberOfChannels = await audioTrack.getNumberOfChannels();
// Get the frame halfway through the video
const sink = new VideoSampleSink(videoTrack);