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>
+8 -13
View File
@@ -743,12 +743,14 @@ 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');
for (const track of this.output._tracks) { if (!isFinalCall) {
if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) { for (const track of this.output._tracks) {
return; // We haven't seen a sample from this open track yet if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) {
return; // We haven't seen a sample from this open track yet
}
} }
} }
@@ -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) {
+11 -11
View File
@@ -496,10 +496,12 @@ export class MatroskaMuxer extends Muxer {
} }
} }
private async interleaveChunks() { private async interleaveChunks(isFinalCall = false) {
for (const track of this.output._tracks) { if (!isFinalCall) {
if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) { for (const track of this.output._tracks) {
return; // We haven't seen a sample from this open track yet if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) {
return; // We haven't seen a sample from this open track yet
}
} }
} }
@@ -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,7 +529,9 @@ export class MatroskaMuxer extends Muxer {
this.writeBlock(trackWithMinTimestamp, chunk); this.writeBlock(trackWithMinTimestamp, chunk);
} }
await this.writer.flush(); if (!isFinalCall) {
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
@@ -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 -2
View File
@@ -5,5 +5,4 @@
- A stream source?? Or like a callback-driven source - A stream source?? Or like a callback-driven source
- 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!!