Feras Feras - 1 month ago 17
HTTP Question

How to download only the moov atom from mp4 file

I already figured out how to parse the moov atom and get some information from it, but how to fire an http request to only get the moov atom from a seekable mp4 url so i can skip the mdat and other atoms.

Probably one way of doing it is to use content-range starting at 0, but how do i determine what is the end point of the moov atom. One way I can see past this problem is to just open the url stream and start reading it until I am past the moov atom at which point I can terminate the request, but this is quite difficult to do, so I was wondering if there is a special request structure that will download only the moov atom and video metadata?

Thanks!

Answer

An .mp4 file consists of chunks (atoms) of different size but with regular structure.

http://atomicparsley.sourceforge.net/ explains the atom chunk structure nicely (and give you the source to study, albeit this is for local files, not via http). The relevant part: Each file "is comprised of discrete units called 'boxes' or 'atoms'. An atom is defined as being a very simple building block:

[4bytes atom length] [4bytes atom name] [contents of the atom, if any]"

For example:

Atom ftyp @ 0 of size: 32, ends @ 32
Atom moov @ 32 of size: 23276, ends @ 23308
  Atom mvhd @ 40 of size: 108, ends @ 148
  Atom trak @ 148 of size: 3664, ends @ 3812
    Atom tkhd @ 156 of size: 92, ends @ 248
    Atom edts @ 248 of size: 36, ends @ 284
  Atom elst @ 256 of size: 28, ends @ 284
    Atom mdia @ 284 of size: 3484, ends @ 3768
...

You could "brachiate" through the file by reading the atom type & size of a chunk (with CONTENT-RANGE), calculate the offset to the next chunk with that information, and repeat until you get the moov atom.

I'm afraid this is just in theory and this might not be of real practical help...

Comments