Java Code Examples for android.media.MediaMuxer#writeSampleData()

The following examples show how to use android.media.MediaMuxer#writeSampleData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AudioUtil.java    From VideoProcessor with Apache License 2.0 5 votes vote down vote up
/**
 * 去掉视频的音轨
 */
public static void removeAudioTrack(String videoPath, String outPath) throws IOException {
    MediaExtractor videoExtractor = new MediaExtractor();
    videoExtractor.setDataSource(videoPath);
    try {
        int videoTrack = VideoUtil.selectTrack(videoExtractor, false);

        videoExtractor.selectTrack(videoTrack);
        MediaFormat videoFormat = videoExtractor.getTrackFormat(videoTrack);

        MediaMuxer mediaMuxer = new MediaMuxer(outPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
        int muxerVideoTrackIndex = mediaMuxer.addTrack(videoFormat);
        mediaMuxer.start();

        MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
        //写视频
        int maxBufferSize = videoFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
        ByteBuffer videoBuffer = ByteBuffer.allocateDirect(maxBufferSize);
        while (true) {
            long sampleTimeUs = videoExtractor.getSampleTime();
            if (sampleTimeUs == -1) {
                break;
            }
            int flags = videoExtractor.getSampleFlags();
            int size = videoExtractor.readSampleData(videoBuffer, 0);
            info.presentationTimeUs = sampleTimeUs;
            info.flags = flags;
            info.size = size;
            mediaMuxer.writeSampleData(muxerVideoTrackIndex, videoBuffer, info);
            videoExtractor.advance();
        }
        mediaMuxer.stop();
        mediaMuxer.release();
    } finally {
        videoExtractor.release();
    }
}
 
Example 2
Source File: DownloadRedditVideoService.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
private boolean muxVideoAndAudio(String videoFilePath, String audioFilePath, String outputFilePath) {
    try {
        File file = new File(outputFilePath);
        file.createNewFile();
        MediaExtractor videoExtractor = new MediaExtractor();
        videoExtractor.setDataSource(videoFilePath);
        MediaExtractor audioExtractor = new MediaExtractor();
        audioExtractor.setDataSource(audioFilePath);
        MediaMuxer muxer = new MediaMuxer(outputFilePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

        videoExtractor.selectTrack(0);
        MediaFormat videoFormat = videoExtractor.getTrackFormat(0);
        int videoTrack = muxer.addTrack(videoFormat);

        audioExtractor.selectTrack(0);
        MediaFormat audioFormat = audioExtractor.getTrackFormat(0);
        int audioTrack = muxer.addTrack(audioFormat);
        boolean sawEOS = false;
        int offset = 100;
        int sampleSize = 2048 * 1024;
        ByteBuffer videoBuf = ByteBuffer.allocate(sampleSize);
        ByteBuffer audioBuf = ByteBuffer.allocate(sampleSize);
        MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo();
        MediaCodec.BufferInfo audioBufferInfo = new MediaCodec.BufferInfo();

        videoExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
        audioExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);

        muxer.start();

        while (!sawEOS) {
            videoBufferInfo.offset = offset;
            videoBufferInfo.size = videoExtractor.readSampleData(videoBuf, offset);

            if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0) {
                sawEOS = true;
                videoBufferInfo.size = 0;
            } else {
                videoBufferInfo.presentationTimeUs = videoExtractor.getSampleTime();
                videoBufferInfo.flags = videoExtractor.getSampleFlags();
                muxer.writeSampleData(videoTrack, videoBuf, videoBufferInfo);
                videoExtractor.advance();
            }
        }

        boolean sawEOS2 = false;
        while (!sawEOS2) {
            audioBufferInfo.offset = offset;
            audioBufferInfo.size = audioExtractor.readSampleData(audioBuf, offset);

            if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0) {
                sawEOS2 = true;
                audioBufferInfo.size = 0;
            } else {
                audioBufferInfo.presentationTimeUs = audioExtractor.getSampleTime();
                audioBufferInfo.flags = audioExtractor.getSampleFlags();
                muxer.writeSampleData(audioTrack, audioBuf, audioBufferInfo);
                audioExtractor.advance();

            }
        }

        try {
            muxer.stop();
            muxer.release();
        } catch (IllegalStateException ignore) {}
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}
 
Example 3
Source File: AudioUtil.java    From VideoProcessor with Apache License 2.0 4 votes vote down vote up
/**
 * 不需要改变音频速率的情况下,直接读写就可
 */
public static long writeAudioTrack(MediaExtractor extractor, MediaMuxer mediaMuxer, int muxerAudioTrackIndex,
                                   Integer startTimeUs, Integer endTimeUs, long baseMuxerFrameTimeUs, VideoProgressListener listener) throws IOException {
    int audioTrack = VideoUtil.selectTrack(extractor, true);
    extractor.selectTrack(audioTrack);
    if (startTimeUs == null) {
        startTimeUs = 0;
    }
    extractor.seekTo(startTimeUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
    MediaFormat audioFormat = extractor.getTrackFormat(audioTrack);
    long durationUs = audioFormat.getLong(MediaFormat.KEY_DURATION);
    int maxBufferSize = audioFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
    ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize);
    MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();

    long lastFrametimeUs = baseMuxerFrameTimeUs;
    while (true) {
        long sampleTimeUs = extractor.getSampleTime();
        if (sampleTimeUs == -1) {
            break;
        }
        if (sampleTimeUs < startTimeUs) {
            extractor.advance();
            continue;
        }
        if (endTimeUs != null && sampleTimeUs > endTimeUs) {
            break;
        }
        if (listener != null) {
            float progress = (sampleTimeUs - startTimeUs) / (float) (endTimeUs == null ? durationUs : endTimeUs - startTimeUs);
            progress = progress < 0 ? 0 : progress;
            progress = progress > 1 ? 1 : progress;
            listener.onProgress(progress);
        }
        info.presentationTimeUs = sampleTimeUs - startTimeUs + baseMuxerFrameTimeUs;
        info.flags = extractor.getSampleFlags();
        info.size = extractor.readSampleData(buffer, 0);
        if (info.size < 0) {
            break;
        }
        CL.i("writeAudioSampleData,time:" + info.presentationTimeUs / 1000f);
        mediaMuxer.writeSampleData(muxerAudioTrackIndex, buffer, info);
        lastFrametimeUs = info.presentationTimeUs;
        extractor.advance();
    }
    return lastFrametimeUs;
}
 
Example 4
Source File: MP4Muxer.java    From AndroidInstantVideo with Apache License 2.0 4 votes vote down vote up
@Override
public int open(final StreamPublisher.StreamPublisherParam params) {
    isStart = false;
    trackCnt = 0;
    videoTrackIndex = null;
    audioTrackIndex = null;

    this.params = params;
    super.open(params);
    try {
        mMuxer = new MediaMuxer(params.outputFilePath,
                MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
    } catch (IOException e) {
        e.printStackTrace();
    }

    frameSender = new FrameSender(new FrameSender.FrameSenderCallback(){

        @Override
        public void onStart() {
            isStart = true;
            mMuxer.start();
        }

        @Override
        public void onSendVideo(FramePool.Frame sendFrame) {
            if (isStart) {
                mMuxer.writeSampleData(videoTrackIndex, ByteBuffer.wrap(sendFrame.data), sendFrame.bufferInfo.getBufferInfo());
            }
        }

        @Override
        public void onSendAudio(FramePool.Frame sendFrame) {
            if (isStart) {
                mMuxer.writeSampleData(audioTrackIndex, ByteBuffer.wrap(sendFrame.data), sendFrame.bufferInfo.getBufferInfo());
            }
        }

        @Override
        public void close() {
            isStart = false;
            if (mMuxer != null) {
                mMuxer.stop();
                mMuxer.release();
                mMuxer = null;
            }
        }
    });



    return 1;
}
 
Example 5
Source File: VideoConverter.java    From android-video-transcoder with The Unlicense 4 votes vote down vote up
public void writeToMuxer(MediaMuxer muxer, int audioTrack, int videoTrack){
	MediaCodec.BufferInfo info=new MediaCodec.BufferInfo();
	info.set(0, data.length, ptime, flags);
	ByteBuffer buffer=ByteBuffer.wrap(data);
	muxer.writeSampleData(track==1 ? videoTrack : audioTrack, buffer, info);
}
 
Example 6
Source File: MainActivity.java    From Android with Apache License 2.0 4 votes vote down vote up
protected boolean process() throws IOException {

        mMediaExtractor = new MediaExtractor();          
        mMediaExtractor.setDataSource(SDCARD_PATH+"/input.mp4");                
                
        int mVideoTrackIndex = -1;
        int framerate = 0;
        for(int i = 0; i < mMediaExtractor.getTrackCount(); i++) {
            MediaFormat format = mMediaExtractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if(!mime.startsWith("video/")) {                
                continue;
            }
            framerate = format.getInteger(MediaFormat.KEY_FRAME_RATE);            
            mMediaExtractor.selectTrack(i);
            mMediaMuxer = new MediaMuxer(SDCARD_PATH+"/ouput.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4);
            mVideoTrackIndex = mMediaMuxer.addTrack(format);  
            mMediaMuxer.start();
        }
        
        if(mMediaMuxer == null) {
            return false;
        }
        
        BufferInfo info = new BufferInfo();
        info.presentationTimeUs = 0;
        ByteBuffer buffer = ByteBuffer.allocate(500*1024);        
        while(true) {
            int sampleSize = mMediaExtractor.readSampleData(buffer, 0);
            if(sampleSize < 0) {
                break;
            }
            mMediaExtractor.advance();
            info.offset = 0;
            info.size = sampleSize;
            info.flags = MediaCodec.BUFFER_FLAG_SYNC_FRAME;        
            info.presentationTimeUs += 1000*1000/framerate;
            mMediaMuxer.writeSampleData(mVideoTrackIndex,buffer,info);
        }

        mMediaExtractor.release();
        
        mMediaMuxer.stop();
        mMediaMuxer.release();
        
        return true;
    }