Fix interleaving of chunks in finalize()

This commit is contained in:
Vanilagy
2025-01-11 19:57:31 +01:00
parent c38c285eab
commit 11d41fcbc7
4 changed files with 23 additions and 29 deletions
+3 -3
View File
@@ -39,7 +39,7 @@
const context = canvas.getContext('2d'); const context = canvas.getContext('2d');
let format = new Metamuxer.WebMOutputFormat({ streamable: false }); let format = new Metamuxer.WebMOutputFormat({ streamable: false });
//format = new Metamuxer.Mp4OutputFormat({ fastStart: false }); // new Metamuxer.MkvOutputFormat();// new Metamuxer.Mp4OutputFormat({ fastStart: false }); format = new Metamuxer.Mp4OutputFormat({ fastStart: 'fragmented' }); // new Metamuxer.MkvOutputFormat();// new Metamuxer.Mp4OutputFormat({ fastStart: false });
let target = new Metamuxer.BufferTarget(); let target = new Metamuxer.BufferTarget();
/* /*
@@ -95,7 +95,7 @@
output.addVideoTrack(videoSource); output.addVideoTrack(videoSource);
output.addAudioTrack(audioSource); output.addAudioTrack(audioSource);
//output.addSubtitleTrack(subtitleSource); output.addSubtitleTrack(subtitleSource);
output.start(); output.start();
@@ -174,5 +174,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.webm'); download(new Blob([target.buffer]), 'test.mp4');
</script> </script>
+5 -10
View File
@@ -743,14 +743,16 @@ export class IsobmffMuxer extends Muxer {
await this.writer.flush(); await this.writer.flush();
} }
private async interleaveSamples() { private async interleaveSamples(isFinalCall = false) {
assert(this.fastStart === 'fragmented'); assert(this.fastStart === 'fragmented');
if (!isFinalCall) {
for (const track of this.output._tracks) { for (const track of this.output._tracks) {
if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) { if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) {
return; // We haven't seen a sample from this open track yet return; // We haven't seen a sample from this open track yet
} }
} }
}
outer: outer:
while (true) { while (true) {
@@ -758,7 +760,7 @@ export class IsobmffMuxer extends Muxer {
let minTimestamp = Infinity; let minTimestamp = Infinity;
for (const trackData of this.trackDatas) { for (const trackData of this.trackDatas) {
if (trackData.sampleQueue.length === 0 && !trackData.track.source._closed) { if (!isFinalCall && trackData.sampleQueue.length === 0 && !trackData.track.source._closed) {
break outer; break outer;
} }
@@ -878,14 +880,7 @@ export class IsobmffMuxer extends Muxer {
} }
if (this.fastStart === 'fragmented') { if (this.fastStart === 'fragmented') {
for (const trackData of this.trackDatas) { await this.interleaveSamples(true);
for (const sample of trackData.sampleQueue) {
await this.addSampleToTrack(trackData, sample);
}
this.processTimestamps(trackData);
}
await this.finalizeFragment(false); // Don't flush the last fragment as we will flush it with the mfra box await this.finalizeFragment(false); // Don't flush the last fragment as we will flush it with the mfra box
} else { } else {
for (const trackData of this.trackDatas) { for (const trackData of this.trackDatas) {
+7 -7
View File
@@ -496,12 +496,14 @@ export class MatroskaMuxer extends Muxer {
} }
} }
private async interleaveChunks() { private async interleaveChunks(isFinalCall = false) {
if (!isFinalCall) {
for (const track of this.output._tracks) { for (const track of this.output._tracks) {
if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) { if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) {
return; // We haven't seen a sample from this open track yet return; // We haven't seen a sample from this open track yet
} }
} }
}
outer: outer:
while (true) { while (true) {
@@ -509,7 +511,7 @@ export class MatroskaMuxer extends Muxer {
let minTimestamp = Infinity; let minTimestamp = Infinity;
for (const trackData of this.trackDatas) { for (const trackData of this.trackDatas) {
if (trackData.chunkQueue.length === 0 && !trackData.track.source._closed) { if (!isFinalCall && trackData.chunkQueue.length === 0 && !trackData.track.source._closed) {
break outer; break outer;
} }
@@ -527,8 +529,10 @@ export class MatroskaMuxer extends Muxer {
this.writeBlock(trackWithMinTimestamp, chunk); this.writeBlock(trackWithMinTimestamp, chunk);
} }
if (!isFinalCall) {
await this.writer.flush(); await this.writer.flush();
} }
}
/** Due to [a bug in Chromium](https://bugs.chromium.org/p/chromium/issues/detail?id=1377842), VP9 streams often /** Due to [a bug in Chromium](https://bugs.chromium.org/p/chromium/issues/detail?id=1377842), VP9 streams often
* lack color space information. This method patches in that information. */ * lack color space information. This method patches in that information. */
@@ -764,11 +768,7 @@ export class MatroskaMuxer extends Muxer {
} }
// Flush any remaining queued chunks to the file // Flush any remaining queued chunks to the file
for (const trackData of this.trackDatas) { await this.interleaveChunks(true);
while (trackData.chunkQueue.length > 0) {
this.writeBlock(trackData, trackData.chunkQueue.shift()!);
}
}
if (!this.format._options.streamable && this.currentCluster) { if (!this.format._options.streamable && this.currentCluster) {
this.finalizeCurrentCluster(); this.finalizeCurrentCluster();
-1
View File
@@ -6,4 +6,3 @@
- onHeader, etc callbacks for Matroska - onHeader, etc callbacks for Matroska
- https://github.com/Vanilagy/mp4-muxer/issues/83 tell him it's possible now - https://github.com/Vanilagy/mp4-muxer/issues/83 tell him it's possible now
- Matroska projection for video rotation in muxer - Matroska projection for video rotation in muxer
- Fix interleaving at the end in finalize!!