From cd87e60673b29e2cc10a8bbc3178cf3d652d8d17 Mon Sep 17 00:00:00 2001 From: "Pohsiang (John) Hsu" Date: Wed, 29 May 2024 17:06:43 -0700 Subject: [PATCH] look for sample duration in thfd box and use that if sampleduration is not present in trun box --- migrationTool/transform/TransMuxer.cs | 30 +++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/migrationTool/transform/TransMuxer.cs b/migrationTool/transform/TransMuxer.cs index 8dcadd8..e54cbf2 100644 --- a/migrationTool/transform/TransMuxer.cs +++ b/migrationTool/transform/TransMuxer.cs @@ -245,13 +245,32 @@ private void UpdateTrackRunDuration(string fileName) if (c.Type == MP4BoxType.traf) { offsetToTrun += moofBox.ComputeBaseSizeBox(); + + // get defaultSampleDuration if exists + bool hasDefaultSampleDuration = false; + uint defaultSampleDuration = 0; + foreach (var cc in c.Children) + { + if (cc.Type == MP4BoxType.tfhd) + { + tfhdBox tfhdBox = (tfhdBox)cc; + if (tfhdBox.DefaultSampleDuration != null) + { + hasDefaultSampleDuration = true; + defaultSampleDuration = (uint) tfhdBox.DefaultSampleDuration!; + } + } + } + foreach (var cc in c.Children) { if (cc.Type == MP4BoxType.trun) { trunBox trunBox = (trunBox)cc; // will throw trunBox.TrunFlags flag = (trunBox.TrunFlags)trunBox.Flags; - if ((flag & trunBox.TrunFlags.SampleDurationPresent) != trunBox.TrunFlags.SampleDurationPresent) + + bool sampleDurationPresent = (flag & trunBox.TrunFlags.SampleDurationPresent) == trunBox.TrunFlags.SampleDurationPresent; + if (!sampleDurationPresent && !hasDefaultSampleDuration) { throw new InvalidDataException("Unexpected, sampleDurationPresent must be present"); } @@ -261,7 +280,14 @@ private void UpdateTrackRunDuration(string fileName) ulong totalDuration = 0; for (int i = 0; i < trunBox.Entries.Count; ++i) { - totalDuration += (ulong)trunBox.Entries[i].SampleDuration!; + if (sampleDurationPresent) + { + totalDuration += (ulong)trunBox.Entries[i].SampleDuration!; + } + else + { + totalDuration += defaultSampleDuration; + } } if (curDecodeTimesIndex + 1 < decodeTimes.Count) {