简体   繁体   English

在Android上实时编码H.264流

[英]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. 我正在编写一个Android应用程序,打算在其中将多个图像编码为可在任何浏览器上重播的实时h.264视频流。 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/ . 我使用MediaCodec API进行编码,然后使用MediaMuxer按照此处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.. 仅在示例中,当调用了encoder / muxer.stop()/ encoder / muxer.release()时,视频文件才获得正确的meta标头,依此类推。

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. 您需要为MediaMuxer及其框架提供正确的“ MediaCodec.BufferInfo ”,其“ presentationTimeUs ”已相应设置。

For example, there are 3 frames, each is shown for 1 second in the video: 例如,有3帧,每个帧在视频中显示1秒钟:

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: 关于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). addTrack:当您获得索引== MediaCodec.INFO_OUTPUT_FORMAT_CHANGED时,调用MediaCodec.dequeueOutputBuffer()时 ,将格式发送给MediaMuxer以为此格式初始化一个新轨道(本例中为“ vidio / avc”)。

  2. mediamuxer.start() mediamuxer.start()

  3. start put frames as above 从上面开始放帧

  4. mediamuxer.stop(), release() mediamuxer.stop(),release()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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