sannies / mp4parser

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

Wrap Stream H264 with Standard MP4 #331

Open aqzwss opened 6 years ago

aqzwss commented 6 years ago

Hi experts, I'm a beginner of video encoding and container, I encounter a challenge: Wrap a stream h264 into a mock standard MP4 and translate it to web page for playing at the same time. My idea is to define a fixed sample size, fill it into mp4 meta data at first, and then make samples from H264AnnexBTrack follow my pre-setting data later. I extend and fill empty byte array to h264 StreamingSampleImpl, and fount it can't play normally any more.

   `
    TrackBox tb = trackBoxes.get(streamingTrack);
    SampleTableBox stbl = Path.getPath(tb, "mdia[0]/minf[0]/stbl[0]");
    SampleSizeBox stsz = Path.getPath(stbl, "stsz[0]");
    final int length = stsz.getSampleSizes().length;
    long[] sampleSizes = new long[length];
    for (int i = 0; i < length; i++) {
        sampleSizes[i] = MAX_SAMPLE_SIZE;
    }
    stsz.setSampleSizes(sampleSizes);
  ` 

   `
    this.duration = duration;
    int size = 0;
    for (ByteBuffer nal : nals) {
        size += 4;
        size += nal.limit();
    }

    final int length = Integer.parseInt(String.valueOf(StreamingMp4Writer.MAX_SAMPLE_SIZE));
    int replenishSize = 0;
    if (size < length) {
        replenishSize = length - size;
    }
    s = ByteBuffer.allocate(length);

    for (ByteBuffer nal : nals) {
        s.put((byte) ((nal.limit() & 0xff000000) >> 24));
        s.put((byte) ((nal.limit() & 0xff0000) >> 16));
        s.put((byte) ((nal.limit() & 0xff00) >> 8));
        s.put((byte) ((nal.limit() & 0xff)));
        s.put((ByteBuffer) nal.rewind());
    }

    if (replenishSize > 0) {
        byte[] replenishBytes = new byte[replenishSize];
        s.put(replenishBytes);
    }
   `

I think the sample data cannot be modified except re-encoded.

Could someone please help me on this challenge?

Pls forgive my poor English. Thanks u in advance.

EdgeAI-Lab commented 5 years ago

Hi, Are you resolve it?