Allow fractional frame rates in track metadata, document frameRate better, other small doc adjustments

This commit is contained in:
Vanilagy
2025-04-28 15:48:51 +02:00
parent cf19e0dcf1
commit e527fe1484
10 changed files with 113 additions and 41 deletions
+15 -15
View File
@@ -441,31 +441,31 @@
const drain = new Metamuxer.VideoFrameDrain(videoTrack); const drain = new Metamuxer.VideoFrameDrain(videoTrack);
for await (const frame of drain.frames()) { for await (const frame of drain.frames()) {
// ... // ...
} }
console.log(await videoTrack.getDuration()); console.log(await videoTrack.getDuration());
*/ */
/* /*
const drain = new Metamuxer.VideoFrameDrain(videoTrack); const drain = new Metamuxer.VideoFrameDrain(videoTrack);
const width = await videoTrack.getWidth(); const width = await videoTrack.getWidth();
const height = await videoTrack.getHeight(); const height = await videoTrack.getHeight();
const canvas = document.createElement('canvas'); const canvas = document.createElement('canvas');
canvas.width = width; canvas.width = width;
canvas.height = height; canvas.height = height;
document.body.append(canvas); document.body.append(canvas);
const context = canvas.getContext('2d'); const context = canvas.getContext('2d');
// Top-level for await loop inside the event listener // Top-level for await loop inside the event listener
for await (const frame of drain.frames()) { for await (const frame of drain.frames()) {
context.drawImage(frame, 0, 0, width, height); context.drawImage(frame, 0, 0, width, height);
frame.close(); frame.close();
} }
*/ */
/* /*
+2 -2
View File
@@ -111,7 +111,7 @@
Example entry 1: Hello <b>world</b>. Example entry 1: Hello <b>world</b>.
`; `;
if (false) simpleWebvttFile = if (false) simpleWebvttFile =
`WEBVTT `WEBVTT
1 1
@@ -178,5 +178,5 @@ Testing... <00:17.350>One... <00:18.125>Two...
await output.finalize(); await output.finalize();
console.log(target); console.log(target);
download(new Blob([target.buffer]), 'test' + format.fileExtension); //download(new Blob([target.buffer]), 'test' + format.fileExtension);
</script> </script>
+1 -1
View File
@@ -94,7 +94,7 @@ track.type; // => 'video' | 'audio' | 'subtitle';
track.isVideoTrack(); // => boolean track.isVideoTrack(); // => boolean
track.isAudioTrack(); // => boolean track.isAudioTrack(); // => boolean
// Retrieve the track's language as an ISO 639-2 language code. // Retrieve the track's language as an ISO 639-2/T language code.
// Resolves to 'und' (undetermined) if the language isn't known. // Resolves to 'und' (undetermined) if the language isn't known.
track.languageCode; // => string track.languageCode; // => string
``` ```
+15 -4
View File
@@ -42,11 +42,13 @@ For each track you want to add, you'll need to create a unique [media source](./
Optionally, you can specify additional track metadata when adding tracks: Optionally, you can specify additional track metadata when adding tracks:
```ts ```ts
// This specifies that the video track should be rotated by 90 degrees clockwise // This specifies that the video track should be rotated by 90 degrees
// before being displayed by video players, and that a frame rate of 30 FPS is // clockwise before being displayed by video players, and that a frame rate
// expected. // of 30 FPS is expected.
output.addVideoTrack(videoSource, { output.addVideoTrack(videoSource, {
rotation: 90, // Clockwise rotation in degrees // Clockwise rotation in degrees
rotation: 90,
// Expected frame rate in hertz
frameRate: 30, frameRate: 30,
}); });
@@ -66,6 +68,15 @@ output.addSubtitleTrack(subtitleSourceFre, { language: 'fre' });
output.addSubtitleTrack(subtitleSourceIta, { language: 'ita' }); output.addSubtitleTrack(subtitleSourceIta, { language: 'ita' });
``` ```
::: info
The optional `frameRate` video track metadata option specifies the expected frame rate of the video. All timestamps and durations of frames that will be added to this track will be snapped to the specified frame rate. You should avoid adding frames more often than the rate permits, as this will lead to multiple frames having the same timestamp.
To precisely achieve common fractional frame rates, make sure to use their exact fractional forms:
$23.976 \rightarrow 24000/1001$\
$29.97 \rightarrow 30000/1001$\
$59.94 \rightarrow 60000/1001$
:::
As an example, let's add two tracks to our output: As an example, let's add two tracks to our output:
- A video track driven by the contents of a `<canvas>` element, encoded using AVC - A video track driven by the contents of a `<canvas>` element, encoded using AVC
- An audio track driven by the user's microphone input, encoded using AAC - An audio track driven by the user's microphone input, encoded using AAC
-1
View File
@@ -36,4 +36,3 @@ things the guide needs to cover:
- supported containers / codecs - supported containers / codecs
- samples & packets - samples & packets
- conversion - conversion
- do a part that "frameRate" must be an integer. and make sure to document numbers that can only be integer in the docblocks
+1 -1
View File
@@ -70,7 +70,7 @@ export abstract class InputTrack {
return this._backing.getId(); return this._backing.getId();
} }
/** The ISO 639-2 language code for this track. If the language is unknown, this field is 'und' (undetermined). */ /** The ISO 639-2/T language code for this track. If the language is unknown, this field is 'und' (undetermined). */
get languageCode() { get languageCode() {
return this._backing.getLanguageCode(); return this._backing.getLanguageCode();
} }
+7 -2
View File
@@ -2,7 +2,7 @@ import { Box, ftyp, IsobmffBoxWriter, mdat, mfra, moof, moov, vtta, vttc, vtte }
import { Muxer } from '../muxer'; import { Muxer } from '../muxer';
import { Output, OutputAudioTrack, OutputSubtitleTrack, OutputTrack, OutputVideoTrack } from '../output'; import { Output, OutputAudioTrack, OutputSubtitleTrack, OutputTrack, OutputVideoTrack } from '../output';
import { BufferTargetWriter, Writer } from '../writer'; import { BufferTargetWriter, Writer } from '../writer';
import { assert, last } from '../misc'; import { assert, computeRationalApproximation, last } from '../misc';
import { IsobmffOutputFormatOptions, IsobmffOutputFormat, MovOutputFormat } from '../output-format'; import { IsobmffOutputFormatOptions, IsobmffOutputFormat, MovOutputFormat } from '../output-format';
import { inlineTimestampRegex, SubtitleConfig, SubtitleCue, SubtitleMetadata } from '../subtitles'; import { inlineTimestampRegex, SubtitleConfig, SubtitleCue, SubtitleMetadata } from '../subtitles';
import { import {
@@ -196,6 +196,11 @@ export class IsobmffMuxer extends Muxer {
assert(meta.decoderConfig.codedWidth !== undefined); assert(meta.decoderConfig.codedWidth !== undefined);
assert(meta.decoderConfig.codedHeight !== undefined); assert(meta.decoderConfig.codedHeight !== undefined);
// The frame rate set by the user may not be an integer. Since timescale is an integer, we'll approximate the
// frame time (inverse of frame rate) with a rational number, then use that approximation's denominator
// as the timescale.
const timescale = computeRationalApproximation(1 / (track.metadata.frameRate ?? 57600), 1e6).denominator;
const newTrackData: IsobmffVideoTrackData = { const newTrackData: IsobmffVideoTrackData = {
track, track,
type: 'video', type: 'video',
@@ -204,7 +209,7 @@ export class IsobmffMuxer extends Muxer {
height: meta.decoderConfig.codedHeight, height: meta.decoderConfig.codedHeight,
decoderConfig: meta.decoderConfig, decoderConfig: meta.decoderConfig,
}, },
timescale: track.metadata.frameRate ?? 57600, timescale,
samples: [], samples: [],
sampleQueue: [], sampleQueue: [],
timestampProcessingQueue: [], timestampProcessingQueue: [],
+11 -2
View File
@@ -7,6 +7,7 @@ import {
colorSpaceIsComplete, colorSpaceIsComplete,
normalizeRotation, normalizeRotation,
readBits, readBits,
roundToMultiple,
textEncoder, textEncoder,
toUint8Array, toUint8Array,
writeBits, writeBits,
@@ -510,8 +511,16 @@ export class MatroskaMuxer extends Muxer {
const trackData = this.getVideoTrackData(track, meta); const trackData = this.getVideoTrackData(track, meta);
const isKeyFrame = packet.type === 'key'; const isKeyFrame = packet.type === 'key';
const timestamp = this.validateAndNormalizeTimestamp(trackData.track, packet.timestamp, isKeyFrame); let timestamp = this.validateAndNormalizeTimestamp(trackData.track, packet.timestamp, isKeyFrame);
const videoChunk = this.createInternalChunk(packet.data, timestamp, packet.duration, packet.type); let duration = packet.duration;
if (track.metadata.frameRate !== undefined) {
// Constrain the time values to the frame rate
timestamp = roundToMultiple(timestamp, 1 / track.metadata.frameRate);
duration = roundToMultiple(duration, 1 / track.metadata.frameRate);
}
const videoChunk = this.createInternalChunk(packet.data, timestamp, duration, packet.type);
if (track.source._codec === 'vp9') this.fixVP9ColorSpace(trackData, videoChunk); if (track.source._codec === 'vp9') this.fixVP9ColorSpace(trackData, videoChunk);
trackData.chunkQueue.push(videoChunk); trackData.chunkQueue.push(videoChunk);
+44
View File
@@ -450,3 +450,47 @@ export const retriedFetch = async (
} }
} }
}; };
export const computeRationalApproximation = (x: number, maxDenominator: number) => {
// Handle negative numbers
const sign = x < 0 ? -1 : 1;
x = Math.abs(x);
let prevNumerator = 0, prevDenominator = 1;
let currNumerator = 1, currDenominator = 0;
// Continued fraction algorithm
let remainder = x;
while (true) {
const integer = Math.floor(remainder);
// Calculate next convergent
const nextNumerator = integer * currNumerator + prevNumerator;
const nextDenominator = integer * currDenominator + prevDenominator;
if (nextDenominator > maxDenominator) {
return {
numerator: sign * currNumerator,
denominator: currDenominator,
};
}
prevNumerator = currNumerator;
prevDenominator = currDenominator;
currNumerator = nextNumerator;
currDenominator = nextDenominator;
remainder = 1 / (remainder - integer);
// Guard against precision issues
if (!isFinite(remainder)) {
break;
}
}
return {
numerator: sign * currNumerator,
denominator: currDenominator,
};
};
+9 -5
View File
@@ -57,7 +57,7 @@ export type OutputSubtitleTrack = OutputTrack & { type: 'subtitle' };
* @public * @public
*/ */
export type BaseTrackMetadata = { export type BaseTrackMetadata = {
/** The three-letter, ISO 639-2 language code specifying the language of this track. */ /** The three-letter, ISO 639-2/T language code specifying the language of this track. */
languageCode?: string; languageCode?: string;
}; };
@@ -68,7 +68,11 @@ export type BaseTrackMetadata = {
export type VideoTrackMetadata = BaseTrackMetadata & { export type VideoTrackMetadata = BaseTrackMetadata & {
/** The angle in degrees by which the track's frames should be rotated (clockwise). */ /** The angle in degrees by which the track's frames should be rotated (clockwise). */
rotation?: Rotation; rotation?: Rotation;
/** The expected video frame rate. You should not exceed the value you set here. */ /**
* The expected video frame rate in hertz. If set, all timestamps and durations of this track will be snapped to
* this frame rate. You should avoid adding more frames than the rate allows, as this will lead to multiple frames
* with the same timestamp.
*/
frameRate?: number; frameRate?: number;
}; };
/** /**
@@ -87,7 +91,7 @@ const validateBaseTrackMetadata = (metadata: BaseTrackMetadata) => {
throw new TypeError('metadata must be an object.'); throw new TypeError('metadata must be an object.');
} }
if (metadata.languageCode !== undefined && !isIso639Dash2LanguageCode(metadata.languageCode)) { if (metadata.languageCode !== undefined && !isIso639Dash2LanguageCode(metadata.languageCode)) {
throw new TypeError('metadata.languageCode must be a three-letter, ISO 639-2 language code.'); throw new TypeError('metadata.languageCode must be a three-letter, ISO 639-2/T language code.');
} }
}; };
@@ -158,10 +162,10 @@ export class Output<
} }
if ( if (
metadata.frameRate !== undefined metadata.frameRate !== undefined
&& (!Number.isInteger(metadata.frameRate) || metadata.frameRate <= 0) && (!Number.isFinite(metadata.frameRate) || metadata.frameRate <= 0)
) { ) {
throw new TypeError( throw new TypeError(
`Invalid video frame rate: ${metadata.frameRate}. Must be a positive integer.`, `Invalid video frame rate: ${metadata.frameRate}. Must be a positive number.`,
); );
} }