gpac / mp4box.js

JavaScript version of GPAC's MP4Box tool
https://gpac.github.io/mp4box.js/
BSD 3-Clause "New" or "Revised" License
1.99k stars 334 forks source link

Extracting specific sample range without reading whole mp4 file #410

Open hcglitte opened 3 weeks ago

hcglitte commented 3 weeks ago

Hi,

I am using FileReader() to read an mp4 file in chunks. Initially I read enough of the start of the file to load the metadata in order for onReady(info) to be called.

After this I would like to only read a specific byte range of this mp4 source file which corresponds to a given set of samples. I know the offsets and sizes for the samples, so I can read at the correct location with FileReader().

The problem seems to be that appendBuffer(arrayBuffer) needs to process the whole mp4 file from the start of the file to the end of the file. I am not sure if this assumption is correct?

Preferably I would like to read appendBuffer(arrayBuffer) with arrayBuffer.fileStart = offset at the starting point corresponding to my set of sampels. Then I would imagine that onSamples is called with the samples I have "requested".

Is there any way to achieve this? In this case, how can I do it?

DenizUgur commented 1 week ago

Hi @hcglitte, I'm trying to understand your issue, so if I miss something, please let me know. For starters, there's no guarantee that onReady will be called at the beginning of the file. The metadata (i.e., moov) doesn't have to be at the beginning of the file. So, it's expected that arrayBuffer wants the entire file.

Therefore, there is no official way to get around that (see the relevant code below). However, you could append zeros before and after your samples to trick MP4Box.js into extracting your samples. But then, why not just use the offset and size information you already have?

https://github.com/gpac/mp4box.js/blob/57463b4e41cfeb9434bf59c698485de8b99e69eb/src/isofile.js#L132-L138