nilaoda/N_m3u8DL-RE · error · Exception

The samples do not fit evenly into the sample sizes given…

Error message

The samples do not fit evenly into the sample sizes given in the TRUN box!

What it means

After reading a WVTT sample from the mdat payload, ExtractSub checks that the consumed bytes (totalSize) fit within the sample size declared in the TRUN box (presentation.SampleSize). If the sample data exceeds the declared size, the trun sample table and the actual payload disagree, and cue boundaries would be misaligned, so it throws.

Solutions

  1. Re-download/re-mux the segment so trun sample_size values match the actual payload.
  2. Ensure the correct init segment matches the media segment (same track/sequence).
  3. Validate the fragment with an MP4 inspector to compare trun sample sizes vs mdat length.
  4. Patch parsing to tolerate overflow by clamping to SampleSize if the extra bytes are padding.
Defensive patterns

Strategy: try-catch

Validate before calling

// compare trun-declared total sample size to mdat length before parsing
// if mismatch, re-fetch or re-mux the fragment instead of parsing

Try / catch

try { var subs = MP4VttUtil.ExtractSub(dataSeg, baseTime); }
catch (Exception ex) when (ex.Message.Contains("do not fit evenly")) {
    logger.Warn("trun/mdat size mismatch, re-downloading segment");
    dataSeg = await reDownloadAsync(segmentUrl);
}

Prevention

When it happens

Trigger: The mdat payload contains more bytes than the sum of trun-declared sample sizes — e.g. segment was produced/re-muxed with inconsistent trun sample_size values, or the wrong trun was paired with this mdat.

Common situations: Corrupt or hand-edited fragments; mixing segments from different init media; packager bugs where sample sizes don't match the emitted payload.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of nilaoda/N_m3u8DL-RE@e113dee70c (2026-09-13). Data as JSON: /api/errors/da5f207eccc17800. Report an issue: GitHub.

Appendix: source

Thrown at src/N_m3u8DL-RE.Parser/Mp4/MP4VttUtil.cs:168

                                if (index != -1)
                                {
                                    cues[index].EndTime = cue.EndTime;
                                }
                                else
                                {
                                    cues.Add(cue);
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("WVTT sample duration unknown, and no default found!");
                    }

                    if (!(presentation.SampleSize == 0 || totalSize <= presentation.SampleSize))
                    {
                        throw new Exception("The samples do not fit evenly into the sample sizes given in the TRUN box!");
                    }

                } while (presentation.SampleSize != 0 && (totalSize < presentation.SampleSize));

                if (reader.HasMoreData())
                {
                    // throw new Exception("MDAT which contain VTT cues and non-VTT data are not currently supported!");
                }
            }
        }

        if (cues.Count > 0)
        {
            return new WebVttSub() { Cues = cues };
        }
        return new WebVttSub();
    }

View on GitHub (pinned to e113dee70c)