sannies / mp4parser

A Java API to read, write and create MP4 files
Apache License 2.0
2.76k stars 567 forks source link

Java mp4parser to create a single .m4s fragment (without malformation) #447

Open charneykaye opened 2 years ago

charneykaye commented 2 years ago

Also posted to: https://stackoverflow.com/questions/69625970/java-mp4parser-to-create-a-single-m4s-fragment with a +100 reputation bounty for a solution!

This use case is a service that manually encodes a series of uncompressed .wav media segments into .m4s fragments for broadcast via MPEG-DASH, using ffmpeg to compress the .wav to .aac and sannies/mp4parser to assemble the aac audio into an .m4s media fragment.

I created this public GitHub project to reproduce the issue in its entirety.

For example, here's the custom CustomFragmentMp4Builder.java class.

It's critical that we be able to designate this single .m4s fragment with a sequence number (index) that we will manually increment for each media segment.

The objective is to build an .m4s fragment comprising the box types SegmentTypeBox, SegmentIndexBox, and MovieFragmentBox. As For reference, I have used mp4parser to inspect an .m4s fragment that was generated via ffmpeg -f hls. This specification is available here as a .yaml file

My implementation creates an MP4 without error. But, when the unit test attempts to read the file that the ChunkMp4Builder just wrote to a temp folder:

java.lang.RuntimeException: A cast to int has gone wrong. Please contact the mp4parser discussion group (3724673092)
    at org.mp4parser.tools.CastUtils.l2i(CastUtils.java:30)
    at org.mp4parser.support.AbstractBox.parse(AbstractBox.java:97)
    at org.mp4parser.AbstractBoxParser.parseBox(AbstractBoxParser.java:116)
    at org.mp4parser.BasicContainer.initContainer(BasicContainer.java:107)
    at org.mp4parser.IsoFile.<init>(IsoFile.java:57)
    at org.mp4parser.IsoFile.<init>(IsoFile.java:52)
    at com.charneykaye.TestBase.getMp4Boxes(TestBase.java:116)
    at com.charneykaye.CustomFragmentMp4BuilderTest.run(CustomFragmentMp4BuilderTest.java:78)

The expected box types SegmentTypeBox, SegmentIndexBox, and MovieFragmentBox do appear in the output:

The expected box types `SegmentTypeBox`, `SegmentIndexBox`, and `MovieFragmentBox` do appear in the output

However, at the end of the file appears a box of an unknown type:

There seems to be a box of an unknown type, appearing at the end of the file.