博客
关于我
多媒体系统之MediaMuxer和MediaExtractor(五)
阅读量:83 次
发布时间:2019-02-26

本文共 6320 字,大约阅读时间需要 21 分钟。

MediaExtractor ? MediaMuxer ?????

MediaExtractor ?????

MediaExtractor ???????????????????????????????????????

  • ???????? setDataSource ?????????????????
  • ????????? getTrackCount() ??????????????
  • ????????? MIME ???? video/ ? audio/????????????? selectTrack ???
  • ??????? readSampleData ??????????????????
  • ???????? advance ??????????
  • ?????????????? release ???????
  • MediaMuxer ?????

    MediaMuxer ??????????????????????????????

  • ??????? addTrack ??????????????????
  • ??????? writeSampleData ????????????????
  • ?????????????? release ???????
  • ????

    ????????????????

  • BufferInfo ????? bufferInfo ???????? size?offset ? presentationTimeUs????????? presentationTimeUs????????????????
  • PTS ???????? B ???presentationTimeUs ???????????????? PTS??????? I ?????
  • ????????? MediaFormat ?? FRAME_RATE ????????????????????
  • ???? PTS

    ???? PTS ??????????????

  • ?? I ????? PTS ?????? I ?????????????
  • ????????????FRAME_RATE?????????????????? 30 ?/?????????? 1000ms / 30 ? 33.333ms?
  • ????

    ???????????

    ?? 1??????

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)public static void trackExtractor(String inPath, String outPath, boolean isVideo) {    MediaExtractor mediaExtractor = new MediaExtractor();    try {        mediaExtractor.setDataSource(inPath);    } catch (IOException e) {        e.printStackTrace();    }    MediaMuxer mediaMuxer = null;    try {        mediaMuxer = new MediaMuxer(outPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);    } catch (IOException e) {        e.printStackTrace();    }    int trackIndex = 0;    int trackCount = mediaExtractor.getTrackCount();    for (int i = 0; i < trackCount; i++) {        MediaFormat trackFormat = mediaExtractor.getTrackFormat(i);        String mime = trackFormat.getString(MediaFormat.KEY_MIME);        if (mime.startsWith("video/") && isVideo) {            trackIndex = mediaMuxer.addTrack(trackFormat);            mediaExtractor.selectTrack(i);            break;        } else if (mime.startsWith("audio/") && !isVideo) {            trackIndex = mediaMuxer.addTrack(trackFormat);            mediaExtractor.selectTrack(i);            break;        }    }    mediaMuxer.start();    ByteBuffer buffer = ByteBuffer.allocate(1024 * 1000);    MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();    long videoSampleTime = getSampleTime(mediaExtractor, buffer);    while (true) {        int readSampleDataSize = mediaExtractor.readSampleData(buffer, 0);        if (readSampleDataSize < 0) {            break;        }        bufferInfo.size = readSampleDataSize;        bufferInfo.offset = 0;        bufferInfo.flags = mediaExtractor.getSampleFlags();        bufferInfo.presentationTimeUs += videoSampleTime;        mediaMuxer.writeSampleData(trackIndex, buffer, bufferInfo);        mediaExtractor.advance();    }    mediaExtractor.release();    mediaMuxer.stop();    mediaMuxer.release();}

    ?? 2??????

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)public static void trackMuxer(String videoPath, String audioPath, String outPath) {    MediaExtractor videoExtractor = new MediaExtractor();    try {        videoExtractor.setDataSource(videoPath);    } catch (IOException e) {        e.printStackTrace();    }    MediaExtractor audioExtractor = new MediaExtractor();    try {        audioExtractor.setDataSource(audioPath);    } catch (IOException e) {        e.printStackTrace();    }    MediaMuxer mediaMuxer = null;    try {        mediaMuxer = new MediaMuxer(outPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);    } catch (IOException e) {        e.printStackTrace();    }    int videoTrackIndex = -1;    int audioTrackIndex = -1;    MediaFormat videoFormat = null;    MediaFormat audioFormat = null;    int videoTrackCount = videoExtractor.getTrackCount();    for (int i = 0; i < videoTrackCount; i++) {        videoFormat = videoExtractor.getTrackFormat(i);        String mime = videoFormat.getString(MediaFormat.KEY_MIME);        if (mime.startsWith("video/")) {            videoTrackIndex = i;            break;        }    }    int audioTrackCount = audioExtractor.getTrackCount();    for (int i = 0; i < audioTrackCount; i++) {        audioFormat = audioExtractor.getTrackFormat(i);        String mime = audioFormat.getString(MediaFormat.KEY_MIME);        if (mime.startsWith("audio/")) {            audioTrackIndex = i;            break;        }    }    videoExtractor.selectTrack(videoTrackIndex);    audioExtractor.selectTrack(audioTrackIndex);    MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo();    MediaCodec.BufferInfo audioBufferInfo = new MediaCodec.BufferInfo();    ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1000);    long videoSampleTime = getSampleTime(videoExtractor, byteBuffer);    while (true) {        int readVideoSampleSize = videoExtractor.readSampleData(byteBuffer, 0);        if (readVideoSampleSize < 0) {            break;        }        videoBufferInfo.size = readVideoSampleSize;        videoBufferInfo.presentationTimeUs += videoSampleTime;        videoBufferInfo.offset = 0;        videoBufferInfo.flags = videoExtractor.getSampleFlags();        mediaMuxer.writeSampleData(videoTrackIndex, byteBuffer, videoBufferInfo);        videoExtractor.advance();    }    long audioSampleTime = getSampleTime(audioExtractor, byteBuffer);    while (true) {        int readAudioSampleSize = audioExtractor.readSampleData(byteBuffer, 0);        if (readAudioSampleSize < 0) {            break;        }        audioBufferInfo.size = readAudioSampleSize;        audioBufferInfo.presentationTimeUs += audioSampleTime;        audioBufferInfo.offset = 0;        audioBufferInfo.flags = audioExtractor.getSampleFlags();        mediaMuxer.writeSampleData(audioTrackIndex, byteBuffer, audioBufferInfo);        audioExtractor.advance();    }    mediaMuxer.stop();    mediaMuxer.release();    videoExtractor.release();    audioExtractor.release();}

    PTS ????

    ??????? PTS ??????

    private static long getSampleTime(MediaExtractor mediaExtractor, ByteBuffer buffer) {    long videoSampleTime;    mediaExtractor.readSampleData(buffer, 0);    long firstVideoPTS = mediaExtractor.getSampleTime();    mediaExtractor.advance();    mediaExtractor.readSampleData(buffer, 0);    long secondVideoPTS = mediaExtractor.getSampleTime();    videoSampleTime = Math.abs(secondVideoPTS - firstVideoPTS);    return videoSampleTime;}

    ????????????????? PTS??????????????

    转载地址:http://vcfz.baihongyu.com/

    你可能感兴趣的文章
    python | werkzeug,一个不可思议的 Python 库!
    查看>>
    python | xlsxwriter,一个实用的 Python 库!
    查看>>
    python | xlwings,一个非常实用的 Excel 相关的 Python 库!
    查看>>
    python | xmltodict,一个非常厉害的 关于XML数据 Python 库!
    查看>>
    python | xonsh,一个超酷的 Python 库!
    查看>>
    python | yagmail,一个实用的 Python 库!
    查看>>
    python | 一文掌握Python的上下文管理器和with语句
    查看>>
    python | 一文看懂Python闭包机制与变量作用域规则
    查看>>
    python读取含中文的json
    查看>>
    python | 如何用Python锁避免并发错误?
    查看>>
    python | 提升代码迭代速度的Python重载方法
    查看>>
    python | 深入理解Python并发编程中的GIL限制与解决方案
    查看>>
    Python | 爬虫实战——亚马逊搜索页监控(附详细源码)
    查看>>
    python | 高效使用Python工具自动生成模块文档的秘诀
    查看>>
    python 一个list去除另一个list中的值
    查看>>
    python 三大框架的 介绍。
    查看>>
    Python 下载的 11 种姿势,一种比一种高级!
    查看>>
    python读取一个文件夹下所有图片_初学Python-找出文件夹下的所有图片
    查看>>
    Python 中 3 个不可思议的返回功能
    查看>>
    python 中 dict 的另一种用法
    查看>>