Android提取视频或音频

【Android】使用MediaExtractor、MediaMuxer去掉视频文件中的音频数据_android 去掉视频音频_little_fat_sheep的博客-CSDN博客

final int VIDEO=0, AUDIO=1;

extract(VIDEO);

extract(AUDIO);

void extract(int type) {
    try {
        String path = Environment.getExternalStorageDirectory().getPath();
        String filename = "";
        if (type == VIDEO)
            filename = "no_sound_" + input_path.substring(input_path.lastIndexOf("/") + 1);
        else if (type == AUDIO)
            filename = "no_video_" + input_path.substring(input_path.lastIndexOf("/") + 1);
        String output_path = path + File.separator + filename;
        MediaExtractor mediaExtractor = new MediaExtractor();
        mediaExtractor.setDataSource(input_path);
        MediaMuxer mediaMuxer = new MediaMuxer(output_path, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
        int input_size = 0;
        int videoTrackIndex = -1;
        int count = mediaExtractor.getTrackCount(); //获取轨道数
        for (int i = 0; i < count; i++) {
            MediaFormat format = mediaExtractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            Log.e(Thread.currentThread().getStackTrace()[2] + "", "track" + i + ":" + mime);
            if (type == VIDEO) {
                if (mime.startsWith("video/")) { // mp4为"video/avc"
                    input_size = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
                    mediaExtractor.selectTrack(i);
                    videoTrackIndex = mediaMuxer.addTrack(format);
                    break;
                }
            } else if (type == AUDIO) {
                if (mime.startsWith("audio/")) {
                    input_size = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
                    mediaExtractor.selectTrack(i);
                    videoTrackIndex = mediaMuxer.addTrack(format);
                    break;
                }
            }
        }
        mediaMuxer.start();
        int sampleSize = 0;
        MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
        ByteBuffer buffer = ByteBuffer.allocate(input_size); //也可以设置为:500*1024
        while ((sampleSize = mediaExtractor.readSampleData(buffer, 0)) > 0) {
            info.offset = 0;
            info.size = sampleSize;
            info.flags = mediaExtractor.getSampleFlags();
            info.presentationTimeUs = mediaExtractor.getSampleTime();
            mediaMuxer.writeSampleData(videoTrackIndex, buffer, info); //写入数据
            mediaExtractor.advance(); //下一帧
        }
        mediaExtractor.release();
        mediaMuxer.stop();
        mediaMuxer.release();
        MediaScannerConnection.scanFile(MainActivity.this, new String[]{ output_path }, null, null);
        Toast.makeText(getApplicationContext(), "视频生成完成:" + output_path, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
		//java.io.IOException: Failed to instantiate extractor.
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
    }
}

选择视频文件

String input_path = "";

void chooseFile() {    
    int CHOOSE_VIDEO = 200;
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, CHOOSE_VIDEO);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Uri uri = data.getData();
        String[] proj = { MediaStore.Video.Media.DATA };
        Cursor cursor = managedQuery(uri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        input_path = cursor.getString(column_index);
        Log.e(Thread.currentThread().getStackTrace()[2] + "", input_path);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值