Skip to content

Fix: Prevent crash in initializeSegmentation when 'mehd' box is missing#532

Closed
AgustinBaffo wants to merge 1 commit into
gpac:mainfrom
AgustinBaffo:fix/segmentation-crash-missing-mehd
Closed

Fix: Prevent crash in initializeSegmentation when 'mehd' box is missing#532
AgustinBaffo wants to merge 1 commit into
gpac:mainfrom
AgustinBaffo:fix/segmentation-crash-missing-mehd

Conversation

@AgustinBaffo

Copy link
Copy Markdown

Description

I recently upgraded mp4box from v0.5.3 to v2.3.0. I am processing a fragmented MP4 file (generated via Chrome's MediaRecorder) in a streaming format (appending chunks from disk).

When calling initializeSegmentation() the library crashes with a TypeError.

TypeError: Cannot read properties of undefined (reading 'fragment_duration')
    at _ISOFile.writeInitializationSegment (mp4box.all.js:xxxx)
    at _ISOFile.initializeSegmentation (mp4box.all.js:xxxx)

The video file is a valid fragmented MP4 (isFragmented: true). However, since it is generated by a live recorder it does not contain the optional mehd box within mvex.

Here is the minimal code I am using in the client:

const mp4box = MP4Box.createFile();

mp4box.onReady = (info) => {
  for (const track of info.tracks) {
    mp4box.setSegmentOptions(track.id, userSourceBuffer, { nbSamples: 100 });
  }
  
  const initSegments = mp4box.initializeSegmentation();   // It is crashing in v2.3.0
};

// ...
arrayBuffer.fileStart = chunkOffset;
mp4box.appendBuffer(arrayBuffer);

Root cause:

I investigated the source code for v2.3.0. The issue seems to be in ISOFile.writeInitializationSegment (here - introduced in this commit).

The code tries to access fragment_duration from the mehd box without verifying if the mehd box actually exists. While moov and mvex are checked (or exist), mehd is optional in the spec and missing in my file.

Testing

I have applied this patch locally and it resolves the crash allowing initializeSegmentation() to proceed correctly with my fragmented files.

@DenizUgur

Copy link
Copy Markdown
Member

Will be fixed via #530

@DenizUgur DenizUgur closed this Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants