Вопрос

I'm reading an .mp4-file in chunks and feeding them over network to a client/player.

If the client skips to a part of the video that it hasn't received yet, it'll send either time or frame# back to the server, and I want to start reading from that part of the file.

I've been reading quite a bit and looking at BmffViewer, as well as the source for BmffViewer, but how to find the offsets eludes me still.

The contents of the files will all be in the same format (h.264 vid, aac sound). The mdat is at the end of the files it looks like, but they still seem to start playing instantaneously.

Here's a pic of the ftyp and file structure from BmffViewer:

Here's a pic from MediaInfo:

Can anyone provide some sample code or at least point me in the right direction? It's too early to start reading ISO-specifications...

Thanks

Это было полезно?

Решение

As you may be aware of, isom files are built out of atoms. Generally these are constructed

length (4 bytes), type (4 bytes), *body*

To get information about your encoded stream, you will need to parse the atoms containing the information you require. For the frame information, you will have to focus on the stbl. A great (shorter) introduction can be found in the quicktime format. The isom format has a few changes, but the general stuff (like frame information) is the same and it's freely available. More info here: quicktime file format

A short explanation: the stbl contains all sample information. The samples are grouped in chunks and stored in the mdat. One chunk could be a sample, but it could also be a group of samples (defined in the stsc). Each chunk has an offset regarding to the file start (defined in stco) and each sample has a size (defined in stsz). For sample timestamps, you can use the sample durations (defined in stts). To know which samples are keyframes, you can use the stss which lists the numbers of the samples which are key samples.

So if you bring this all together: if you have a frame number and want to find the offset, look in the stsc to find the chunk which you need, look at the stco to find the offset of that chunk and add the sizes found in the stsz for the samples which are before the sample you need.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top