android 采用Mediamutex合成音视频

public int getTrack(MediaExtractor extractor,String mime)
    {
        for(int i = 0 ; i < extractor.getTrackCount();i++)
        {
            MediaFormat format = extractor.getTrackFormat(i);
            String key_mime = format.getString(KEY_MIME);
            if(key_mime.startsWith(mime))
            {
                return i;
            }
        }
        return -1;
    }

    public int mergeVideo(String videoFile,String audioFile,String outMp4File){
        MediaExtractor extractor = new MediaExtractor();
        MediaExtractor audioExtractor = new MediaExtractor();
        try {
            extractor.setDataSource(videoFile);
            audioExtractor.setDataSource(audioFile);
        }catch (IOException ex)
        {
            return -1;
        }

        int videoTrack = getTrack(extractor,"video/");
        int audioTrack = getTrack(audioExtractor,"audio/");
        if(videoTrack == -1)
        {
            return -1;
        }
        if(audioTrack == -1)
        {
            return -1;
        }

        extractor.selectTrack(videoTrack);
        audioExtractor.selectTrack(audioTrack);
        MediaFormat videoFormat = extractor.getTrackFormat(videoTrack);
        MediaFormat audioFormat = audioExtractor.getTrackFormat(audioTrack);

        MediaMuxer muxer = null;
        try {
            muxer = new MediaMuxer(outMp4File, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
        }catch(IOException ex)
        {
            return -1;
        }
        int videoIndex = muxer.addTrack(videoFormat);
        int audioIndex = muxer.addTrack(audioFormat);
        ByteBuffer videoBuffer = ByteBuffer.allocate(1024*1024*5);
        ByteBuffer audioBuffer = ByteBuffer.allocate(1024*1024*5);
        muxer.start();

        boolean videoEnd = false;
        boolean audioEnd = false;
        while(true)
        {
            int vRet = extractor.readSampleData(videoBuffer,0);
            if(vRet == -1)
            {
                videoEnd = true;
            }
            long vTime = extractor.getSampleTime();
            int vFlags = extractor.getSampleFlags();

            int aRet = audioExtractor.readSampleData(audioBuffer,0);
            if(aRet == -1)
            {
                audioEnd = true;
            }
            long aTime = audioExtractor.getSampleTime();
            int aFlags = audioExtractor.getSampleFlags();

            if(videoEnd && audioEnd)
            {
                MediaCodec.BufferInfo bi = new MediaCodec.BufferInfo();
                bi.presentationTimeUs = 0;
                bi.offset = 0;
                bi.size = 0;
                bi.flags = BUFFER_FLAG_END_OF_STREAM;
                muxer.writeSampleData(videoIndex,videoBuffer,bi);
                muxer.writeSampleData(audioIndex,audioBuffer,bi);
                break;
            }
            if(videoEnd)
            {
                //Log.d(TAG,"time: videoEnd:" + aTime + " audio Size:" + aRet);
                MediaCodec.BufferInfo bi = new MediaCodec.BufferInfo();
                bi.presentationTimeUs = aTime;
                bi.offset = 0;
                bi.size = aRet;
                bi.flags = aFlags;
                muxer.writeSampleData(audioIndex,audioBuffer,bi);
                audioExtractor.advance();
            }
            else if(audioEnd)
            {
                //Log.d(TAG,"time: audioEnd:" + vTime + " video Size:" + vRet);
                MediaCodec.BufferInfo bi = new MediaCodec.BufferInfo();
                bi.presentationTimeUs = vTime;
                bi.offset = 0;
                bi.size = vRet;
                bi.flags = vFlags;
                muxer.writeSampleData(videoIndex,videoBuffer,bi);
                extractor.advance();
            }
            else if(vTime <= aTime)
            {
                //Log.d(TAG,"time:V-A" + vTime + " " + aTime + " video Size:" + vRet);
                MediaCodec.BufferInfo bi = new MediaCodec.BufferInfo();
                bi.presentationTimeUs = vTime;
                bi.offset = 0;
                bi.size = vRet;
                bi.flags = vFlags;
                muxer.writeSampleData(videoIndex,videoBuffer,bi);
                extractor.advance();
            }else
            {
                //Log.d(TAG,"time:V-A :" + vTime + " " + aTime + " audio Size:" + aRet);
                MediaCodec.BufferInfo bi = new MediaCodec.BufferInfo();
                bi.presentationTimeUs = aTime;
                bi.offset = 0;
                bi.size = aRet;
                bi.flags = aFlags;
                muxer.writeSampleData(audioIndex,audioBuffer,bi);
                audioExtractor.advance();
            }
        }
        while(true)
        {
            try {
                muxer.stop();
            }catch(IllegalStateException ex)
            {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                continue;
            }
            break;
        }
        muxer.release();
        //Log.d(TAG,"over.");
        return 0;
    }


采用此代码可直接将音频mp4和视频mp4合成成一份带音视频mp4


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值