From 873dae00c1869511d2fab4b97545743ad5552d40 Mon Sep 17 00:00:00 2001 From: Vanilagy <1696106+Vanilagy@users.noreply.github.com> Date: Sun, 16 Feb 2025 18:55:47 +0100 Subject: [PATCH] Add more relaxed rules for edit lists --- src/isobmff/isobmff-demuxer.ts | 17 ++++++++++++++++- todo.txt | 3 +-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/isobmff/isobmff-demuxer.ts b/src/isobmff/isobmff-demuxer.ts index 59732a2..a8f2698 100644 --- a/src/isobmff/isobmff-demuxer.ts +++ b/src/isobmff/isobmff-demuxer.ts @@ -57,6 +57,9 @@ type InternalTrack = { fragmentLookupTable: FragmentLookupTableEntry[] | null; currentFragmentState: FragmentTrackState | null; fragments: Fragment[]; + /** The segment durations of all edit list entries leading up to the main one (from which the offset is taken.) */ + editListPreviousSegmentDurations: number; + /** The media time offset of the main edit list entry (with media time !== -1) */ editListOffset: number; } & ({ info: null; @@ -253,6 +256,14 @@ export class IsobmffDemuxer extends Demuxer { ); this.readContiguousBoxes(boxInfo.contentSize); + for (const track of this.tracks) { + // Modify the edit list offset based on the previous segment durations. They are in different + // timescales, so we first convert to seconds and then into the track timescale. + const previousSegmentDurationsInSeconds + = track.editListPreviousSegmentDurations / this.movieTimescale; + track.editListOffset -= Math.round(previousSegmentDurationsInSeconds * track.timescale); + } + break; } @@ -584,6 +595,7 @@ export class IsobmffDemuxer extends Demuxer { fragmentLookupTable: null, currentFragmentState: null, fragments: [], + editListPreviousSegmentDurations: 0, editListOffset: 0, } satisfies InternalTrack as InternalTrack; this.currentTrack = track; @@ -665,6 +677,7 @@ export class IsobmffDemuxer extends Demuxer { this.metadataReader.pos += 3; // Flags let relevantEntryFound = false; + let previousSegmentDurations = 0; const entryCount = this.metadataReader.readU32(); for (let i = 0; i < entryCount; i++) { @@ -686,13 +699,15 @@ export class IsobmffDemuxer extends Demuxer { } if (mediaTime === -1) { - throw new Error('Unsupported edit list: no empty edits allowed.'); + previousSegmentDurations += segmentDuration; + continue; } if (mediaRate !== 1) { throw new Error('Unsupported edit list: media rate must be 1.'); } + track.editListPreviousSegmentDurations = previousSegmentDurations; track.editListOffset = mediaTime; relevantEntryFound = true; } diff --git a/todo.txt b/todo.txt index 6fe340f..cf3e353 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,3 @@ - onHeader, etc callbacks for Matroska - https://github.com/Vanilagy/mp4-muxer/issues/83 tell him it's possible now -- is this fixed? https://github.com/Vanilagy/webm-muxer/issues/50 -- decode and flush abstract in custom coders \ No newline at end of file +- is this fixed? https://github.com/Vanilagy/webm-muxer/issues/50 \ No newline at end of file