Skip to content

Commit

Permalink
Clean up min fragment duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanilagy committed Feb 2, 2025
1 parent 4984452 commit 1cea945
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 23 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ interface MuxerOptions {
firstTimestampBehavior?: 'strict' | 'offset' | 'cross-track-offset',

// The minimum duration of each fragment in seconds. Only applicable when
// `fastStart` is set to `'fragmented'`. Default is 1.0.
// `fastStart` is set to `'fragmented'`. Default is 1.
minFragmentDuration?: number
}
```
Expand Down Expand Up @@ -161,7 +161,9 @@ This option specifies where the data created by the muxer will be written. The o
default chunk size of 16 MiB, which can be overridden by manually setting `chunkSize` to the desired byte length.

If you want to use this target for *live-streaming*, i.e. playback before muxing has finished, you also need to set
`fastStart: 'fragmented'`. If implementing live-streaming, also consider setting `minFragmentDuration` to a value that is appropriate for your use case (default value is 1 second). A new fragment is emitted when a keyframe is encountered after the duration of the buffered data has reached `minFragmentDuration` seconds.
`fastStart: 'fragmented'`. Also consider setting `minFragmentDuration` to a value that is appropriate for your use
case and latency requirements (default value is 1 second). A new fragment is emitted when a keyframe is encountered
after the duration of the buffered data has reached `minFragmentDuration` seconds.

Usage example:
```js
Expand Down
9 changes: 2 additions & 7 deletions build/mp4-muxer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build/mp4-muxer.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions build/mp4-muxer.min.mjs

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions build/mp4-muxer.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mp4-muxer",
"version": "5.2.1",
"version": "5.2.0",
"description": "MP4 multiplexer in pure TypeScript with support for WebCodecs API, video & audio.",
"main": "./build/mp4-muxer.js",
"module": "./build/mp4-muxer.mjs",
Expand Down
11 changes: 9 additions & 2 deletions src/muxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Mp4MuxerOptions<T extends Target> = {
expectedAudioChunks?: number
},
firstTimestampBehavior?: FirstTimestampBehavior,
fragmentedChunkDuration?: number
minFragmentDuration?: number
};

export interface Track {
Expand Down Expand Up @@ -240,6 +240,13 @@ export class Muxer<T extends Target> {
} else if (![false, 'in-memory', 'fragmented'].includes(options.fastStart)) {
throw new TypeError(`'fastStart' option must be false, 'in-memory', 'fragmented' or an object.`);
}

if (
options.minFragmentDuration !== undefined
&& (!Number.isFinite(options.minFragmentDuration) || options.minFragmentDuration < 0)
) {
throw new TypeError(`'minFragmentDuration' must be a non-negative number.`);
}
}

#writeHeader() {
Expand Down Expand Up @@ -682,7 +689,7 @@ export class Muxer<T extends Target> {

if (this.#options.fastStart === 'fragmented') {
let mostImportantTrack = this.#videoTrack ?? this.#audioTrack;
const chunkDuration = this.#options.fragmentedChunkDuration ?? 1.0;
const chunkDuration = this.#options.minFragmentDuration ?? 1;
if (track === mostImportantTrack && sample.type === 'key' && currentChunkDuration >= chunkDuration) {
beginNewChunk = true;
this.#finalizeFragment();
Expand Down

0 comments on commit 1cea945

Please sign in to comment.