简体   繁体   中英

Live encode H.264 stream on Android

I am writing an Android app where I plan to encode several images in to a live h.264 video stream that can be replayed on any browser. I am using the MediaCodec API for encoding and then MediaMuxer to write it to a file as per the example here http://bigflake.com/mediacodec/ .

What I am stuck with is that how to tell the encoder/muxer to encode it such that it can be progressively played back. From the examples only when the encoder/muxer.stop()/encoder/muxer.release() call is made, then the video file gets the right meta headers, etc..

Thanks

I guess you are considering the time at which each frame is shown.

You need to give MediaMuxer, along with the frame, the right " MediaCodec.BufferInfo ," whose " presentationTimeUs " is set accordingly.

For example, there are 3 frames, each is shown for 1 second in the video:

sec 0---------1---------2-----------
    frame1    frame2    frame3 


int[] timestampSec = {0, 1, 2};
for (int i = 0; i < 3; i++) {
    muxer.writeSampleData(trackId, 
                          frame[i], 
                          timeStampSec[i] * 1000000); 
}

As to initialization and ending of MediaMuxer:

  1. addTrack: when you get index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED when you call MediaCodec.dequeueOutputBuffer() , send the format to MediaMuxer to initialize a new track for this format(it's "vidio/avc" in this case).

  2. mediamuxer.start()

  3. start put frames as above

  4. mediamuxer.stop(), release()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM