Skip to content

Commit 3de75d2

Browse files
committed
Allow clusters starting with delta chunks
1 parent bbf2157 commit 3de75d2

File tree

7 files changed

+50
-66
lines changed

7 files changed

+50
-66
lines changed

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,6 @@ await fileStream.close();
292292
```
293293
294294
## Details
295-
### Video key frame frequency
296-
Canonical WebM files can only have a maximum Matroska Cluster length of 32.768 seconds, and each cluster must begin with
297-
a video key frame. You therefore need to tell your `VideoEncoder` to encode a `VideoFrame` as a key frame at least every
298-
32 seconds, otherwise your WebM file will be incorrect. You can do this by doing:
299-
```js
300-
videoEncoder.encode(frame, { keyFrame: true });
301-
```
302-
303295
### Media chunk buffering
304296
When muxing a file with a video **and** an audio track, it is important that the individual chunks inside the WebM file
305297
be stored in monotonically increasing time. This does mean, however, that the multiplexer must buffer chunks of one

build/webm-muxer.js

Lines changed: 14 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/webm-muxer.min.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/webm-muxer.min.mjs

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/webm-muxer.mjs

Lines changed: 14 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webm-muxer",
3-
"version": "5.0.4",
3+
"version": "5.1.0",
44
"description": "WebM multiplexer in pure TypeScript with support for WebCodecs API, video & audio.",
55
"main": "./build/webm-muxer.js",
66
"module": "./build/webm-muxer.mjs",

src/muxer.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -755,33 +755,29 @@ export class Muxer<T extends Target> {
755755
}
756756

757757
let msTimestamp = Math.floor(chunk.timestamp / 1000);
758+
let relativeTimestamp = msTimestamp - this.#currentClusterTimestamp;
759+
758760
let shouldCreateNewClusterFromKeyFrame =
759761
canCreateNewCluster &&
760762
chunk.type === 'key' &&
761-
msTimestamp - this.#currentClusterTimestamp >= 1000;
763+
relativeTimestamp >= 1000;
764+
let clusterWouldBeTooLong = relativeTimestamp >= MAX_CHUNK_LENGTH_MS;
762765

763766
if (
764767
!this.#currentCluster ||
765-
shouldCreateNewClusterFromKeyFrame
768+
shouldCreateNewClusterFromKeyFrame ||
769+
// If we hit this case, then we're sadly forced to create a chunk that starts with a delta chunk
770+
clusterWouldBeTooLong
766771
) {
767772
this.#createNewCluster(msTimestamp);
773+
relativeTimestamp = 0;
768774
}
769775

770-
let relativeTimestamp = msTimestamp - this.#currentClusterTimestamp;
771776
if (relativeTimestamp < 0) {
772-
// The chunk lies out of the current cluster
777+
// The chunk lies outside of the current cluster
773778
return;
774779
}
775780

776-
let clusterIsTooLong = relativeTimestamp >= MAX_CHUNK_LENGTH_MS;
777-
if (clusterIsTooLong) {
778-
throw new Error(
779-
`Current Matroska cluster exceeded its maximum allowed length of ${MAX_CHUNK_LENGTH_MS} ` +
780-
`milliseconds. In order to produce a correct WebM file, you must pass in a key frame at least every ` +
781-
`${MAX_CHUNK_LENGTH_MS} milliseconds.`
782-
);
783-
}
784-
785781
let prelude = new Uint8Array(4);
786782
let view = new DataView(prelude.buffer);
787783
// 0x80 to indicate it's the last byte of a multi-byte number

0 commit comments

Comments
 (0)