Add configurable minimum MP4 fragment duration and Matroska cluster duration

This commit is contained in:
Vanilagy
2025-03-22 15:33:00 +01:00
parent 1652b4f614
commit b14daf6dca
6 changed files with 38 additions and 22 deletions
+4 -1
View File
@@ -118,6 +118,7 @@ export class IsobmffMuxer extends Muxer {
private nextFragmentNumber = 1;
// Only relevant for fragmented files, to make sure new fragments start with the highest timestamp seen so far
private maxWrittenTimestamp = -Infinity;
private minimumFragmentDuration: number;
constructor(output: Output, format: IsobmffOutputFormat) {
super(output);
@@ -136,6 +137,8 @@ export class IsobmffMuxer extends Muxer {
if (this.fastStart === 'in-memory' || this.isFragmented) {
this.writer.ensureMonotonicity = true;
}
this.minimumFragmentDuration = format._options.minimumFragmentDuration ?? 1;
}
async start() {
@@ -683,7 +686,7 @@ export class IsobmffMuxer extends Muxer {
});
if (
currentChunkDuration >= 1.0
currentChunkDuration >= this.minimumFragmentDuration
&& keyFrameQueuedEverywhere
&& sample.timestamp > this.maxWrittenTimestamp
) {
+1 -1
View File
@@ -703,7 +703,7 @@ export class MatroskaMuxer extends Muxer {
// CURRENT chunk, meaning that starting the next cluster at the same timestamp is forbidden (since
// the already-written block would belong into it instead).
&& msTimestamp > this.currentClusterMaxMsTimestamp
&& relativeTimestamp >= 1000
&& relativeTimestamp >= 1000 * (this.format._options.minimumClusterDuration ?? 1)
)
// The cluster would exceed its maximum allowed length. This puts us in an unfortunate position and forces
// us to begin the next cluster with a delta frame. Although this is undesirable, it is not forbidden by the
+24
View File
@@ -110,6 +110,12 @@ export type IsobmffOutputFormatOptions = {
* the type of output target used.
*/
fastStart?: false | 'in-memory' | 'fragmented';
/**
* When using `fastStart: 'fragmented'`, this field controls the minimum duration of each fragment, in seconds.
* New fragments will only be created when the current fragment is longer than this value. Defaults to 1 second.
*/
minimumFragmentDuration?: number;
};
/**
@@ -127,6 +133,12 @@ export abstract class IsobmffOutputFormat extends OutputFormat {
if (options.fastStart !== undefined && ![false, 'in-memory', 'fragmented'].includes(options.fastStart)) {
throw new TypeError('options.fastStart, when provided, must be false, "in-memory", or "fragmented".');
}
if (
options.minimumFragmentDuration !== undefined
&& (!Number.isFinite(options.minimumFragmentDuration) || options.minimumFragmentDuration < 0)
) {
throw new TypeError('options.minimumFragmentDuration, when provided, must be a non-negative number.');
}
super();
@@ -226,6 +238,12 @@ export type MkvOutputFormatOptions = {
* option when you want to write out a file for later use.
*/
streamable?: boolean;
/**
* This field controls the minimum duration of each Matroska cluster, in seconds. New clusters will only be created
* when the current cluster is longer than this value. Defaults to 1 second.
*/
minimumClusterDuration?: number;
};
/**
@@ -243,6 +261,12 @@ export class MkvOutputFormat extends OutputFormat {
if (options.streamable !== undefined && typeof options.streamable !== 'boolean') {
throw new TypeError('options.streamable, when provided, must be a boolean.');
}
if (
options.minimumClusterDuration !== undefined
&& (!Number.isFinite(options.minimumClusterDuration) || options.minimumClusterDuration < 0)
) {
throw new TypeError('options.minimumClusterDuration, when provided, must be a non-negative number.');
}
super();