Android 音视频开发之MediaMuxer

简介

在Android中,可以使用MediaMuxer来封装编码后的视频流和音频流到mp4容器中,MediaMuxer最多仅支持一个视频track和一个音频track,所以如果有多个音频track可以先把它们混合成为一个音频track然后再使用MediaMuxer封装到mp4容器中。
通常视频编码使用H.264(AVC)编码,音频编码使用AAC编码,在MediaFormat中我们可以看到各种编码格式:

public static final String MIMETYPE_VIDEO_AVC = “video/avc”;
public static final String MIMETYPE_AUDIO_AAC = “audio/mp4a-latm”;
public static final String MIMETYPE_TEXT_CEA_608 = “text/cea-608”;

  • 使用
  1. 通过**new MediaMuxer(String path, int format)**指定视频文件输出路径和文件格式:

    MediaMuxer mMediaMuxer = new MediaMuxer(mOutputVideoPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
    
  2. 创建MediaMuxer对象之后,一个比较重要的操作就是addTrack(MediaFormat format),添加媒体通道,该函数需要传入MediaFormat对象,通常从MediaExtractor或者MediaCodec中获取,如果希望自己创建的话可以直接new或者通过该类如下静态方法创建:

    MediaFormat.createAudioFormat(...);
    MediaFormat.createVideoFormat(...);
    MediaFormat.createSubtitleFormat(...);
    

    如果是自己创建MediaFormat要根据媒体类型配置好相应的keys,这里注意一定要设置csd参数,否则添加进MediaMuxer的MediaFormat会导致MediaMuxer调用stop()时抛出异常。csd参数在官方文档中叫Codec-specific Data,详细介绍可以看MediaCodec官方文档 - Codec-specific Data部分。对于H.264来说,"csd-0"和"csd-1"分别对应sps和pps;对于AAC来说,"csd-0"对应ADTS。

    MediaFormat videoFormat = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, 1280, 720);
    byte[] header_sps = {0, 0, 0, 1, 103, 100, 0, 31, -84, -76, 2, -128, 45, -56};
    byte[] header_pps = {0, 0, 0, 1, 104, -18, 60, 97, 15, -1, -16, -121, -1, -8, 67, -1, -4, 33, -1, -2, 16, -1, -1, 8, 127, -1, -64};
    videoFormat.setByteBuffer("csd-0", ByteBuffer.wrap(header_sps));
    videoFormat.setByteBuffer("csd-1", ByteBuffer.wrap(header_pps));
    videoFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
    videoFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 1920 * 1080);
    videoFormat.setInteger(MediaFormat.KEY_CAPTURE_RATE, 25);
    
  3. 把MediaFormat添加到MediaMuxer后记录返回的track index,添加完所有track后调用start方法:

    videoTrackIndex = mMediaMuxer.addTrack(format);
    audioTrackIndex = mMediaMuxer.addTrack(format);
    mMediaMuxer.start();
    
  4. 然后就可以调用MediaMuxer.writeSampleData()向mp4文件中写入数据了。这里要注意每次只能添加一帧视频数据或者单个Sample的音频数据,并且BufferInfo对象的值一定要设置正确:

    info.offset = 0;
    info.size = sampleSize;
    必须填入数据的大小
    info.flags = MediaCodec.BUFFER_FLAG_SYNC_FRAME;
    需要给出是否为同步帧/关键帧
    info.presentationTimeUs = mVideoExtractor.getSampleTime();
    必须给出正确的时间戳,注意单位是 us
    mMediaMuxer.writeSampleData(videoTrackIndex, buffer, info);
    
  5. 结束写入后关闭以及释放资源

    mMediaMuxer.stop();
    mMediaMuxer.release();
    
原创文章 11 获赞 10 访问量 542

猜你喜欢

转载自blog.csdn.net/qq_34712399/article/details/105897289
今日推荐