From 94b797e227cb581a64f462e578310a8e75a4a6eb Mon Sep 17 00:00:00 2001 From: Vanilagy <1696106+Vanilagy@users.noreply.github.com> Date: Sat, 3 May 2025 21:43:29 +0200 Subject: [PATCH] TEMP timestamp iterator bugfix --- dev/demux.html | 67 +++++++++++++++++++++++++++++++++++++++++++++++ src/media-sink.ts | 11 +++++--- todo.txt | 3 ++- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/dev/demux.html b/dev/demux.html index f4d059d..cff08c4 100644 --- a/dev/demux.html +++ b/dev/demux.html @@ -16,6 +16,72 @@ source }); + /* + const timestamps = [ + 4.16, + 4.266666666666667, + 4.373333333333333, + 4.48, + 4.586666666666667, + 4.693333333333333, + 4.8, + 4.906666666666666, + 5.013333333333334, + 5.12, + 5.226666666666667, + 5.333333333333333, + 5.4399999999999995, + 5.546666666666667, + 5.653333333333333, + 5.76, + 5.866666666666666, + 5.973333333333333, + 6.08, + 6.186666666666666, + 6.293333333333333, + 6.4, + 6.506666666666666, + 6.613333333333333, + 6.72, + 6.826666666666666, + 6.933333333333334, + 7.039999999999999, + 7.1466666666666665, + 7.253333333333333 + ]; + */ + const timestamps = []; + const videoTrack = await input.getPrimaryVideoTrack(); + + const packetSink = new Metamuxer.EncodedPacketSink(videoTrack); + for await (const packet of packetSink.packets()) { + if (packet.timestamp >= 1) { + break; + } + + timestamps.push(packet.timestamp); + } + + timestamps.sort((a, b) => a-b) + + console.log(timestamps); + //return; + /* + const sink = new Metamuxer.EncodedPacketSink(videoTrack); + for await (const packet of sink.packets()) { + console.log(packet.sequenceNumber); + } + */ + + + const sink = new Metamuxer.CanvasSink(videoTrack, { width: 320 }); + + for await (const wrappedCanvas of sink.canvasesAtTimestamps(timestamps)) { + console.log(wrappedCanvas); + } + + + /* const output = new Metamuxer.Output({ format: new Metamuxer.Mp3OutputFormat(), target: new Metamuxer.BufferTarget() @@ -49,6 +115,7 @@ console.log(output.target.buffer) download(new Blob([output.target.buffer]), 'converted.mp3'); + */ /* diff --git a/src/media-sink.ts b/src/media-sink.ts index dd99777..7e78e7f 100644 --- a/src/media-sink.ts +++ b/src/media-sink.ts @@ -526,6 +526,8 @@ export abstract class BaseMediaSampleSink< continue; } + // console.log('target', targetPacket); + const keyPacket = await packetSink.getKeyPacket(timestamp); if (!keyPacket) { pushToQueue(null); @@ -533,15 +535,17 @@ export abstract class BaseMediaSampleSink< } if (lastPacket && targetPacket.sequenceNumber < lastPacket.sequenceNumber) { - // We're going back in time with this one, let's flush and reset to an clean state + // console.log('FLUSH?', lastPacket, targetPacket.sequenceNumber, lastPacket.sequenceNumber); + // We're going back in time with this one, let's flush and reset to a clean state await decoder.flush(); timestampsOfInterest.length = 0; } if ( lastKeyPacket - && keyPacket.sequenceNumber === lastKeyPacket.sequenceNumber - && targetPacket.timestamp >= lastPacket!.timestamp + && keyPacket.sequenceNumber === lastKeyPacket.sequenceNumber // => they're the same packet + // && targetPacket.timestamp >= lastPacket!.timestamp + && targetPacket.sequenceNumber >= lastPacket!.sequenceNumber ) { assert(lastPacket); @@ -558,6 +562,7 @@ export abstract class BaseMediaSampleSink< timestampsOfInterest.push(targetPacket.timestamp); } } else { + // console.log('==== RESET'); // The key packet has changed lastKeyPacket = keyPacket; lastPacket = keyPacket; diff --git a/todo.txt b/todo.txt index 0bb2534..adfe1d4 100644 --- a/todo.txt +++ b/todo.txt @@ -1,3 +1,4 @@ - https://github.com/Vanilagy/mp4-muxer/issues/83 tell him it's possible now - textsubtitlesource, chunked piping -- More efficient MP3 loading when reading sequentially \ No newline at end of file +- More efficient MP3 loading when reading sequentially +- toCanvasImageSource might return a new videoframe; how to handle this? \ No newline at end of file