nareix / joy4

Golang audio/video library and streaming server
MIT License
2.66k stars 501 forks source link

How to produce fragmented mp4 files? #121

Closed ivanjaros closed 3 years ago

ivanjaros commented 3 years ago

Related to https://github.com/nareix/joy4/issues/120 the issue with media source is that it will simply not play mp4 files that are not fragmented. Hence I am asking - how to produce fragmeented mp4 files?

As I understand it, standard mp4 file will have one big chunk of data in the container whereas fragmented mp4 will have multiple chuks of data - individual atoms, hence the reader can start playing immediately after being able to read the first atom and proceed to the other one. So it is only a matter of mp4 muxer to write packets in such form. The writePacket method would need to be changed or allow for a flag argument.

Some info: https://stackoverflow.com/a/35180327/11493911

edit: seems it is more about the mp4io than mp4 muxer.

edit2: it looks like the mp4 muxer WriteHeader() will write the MDAT right away which essentially means that anything written by WritePacket will be put into this one big container. So I think that write header needs to stop doing this and it should be moved into write packet method, together with MOOF and trailers should just adjust the SIDX, as per the images in the SO post linked above.

just to get visual context, images from the SO above:

standard mp4 container("mono"): mp4-mono

fragmented mp4 container: mp-fragmented

ivanjaros commented 3 years ago

is looks that the mp4 muxer is outputting only mdat and moov fields. there is no ftyp field. and http://mp4parser.com/ plus looking at WriteTrailers shows that the moov follows mdat, which is strange.

ivanjaros commented 3 years ago

anyone interested in this should have a look at the https://github.com/edgeware/mp4ff

cedricve commented 3 years ago

@ivanjaros did you made any progress on this? Really interested in this!

ivanjaros commented 2 years ago

yes, i used another library but i abandoned the project since then so i don't remember

cedricve commented 2 years ago

no worries by this time, we already implemented the solution in Kerberos.io