Android去掉视频声音

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

java 复制代码
void removeSound() {
    try {
        String path = Environment.getExternalStorageDirectory().getPath();
        String filename = "no_sound_" + 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);
            if (mime.startsWith("video/")) { // mp4为"video/avc"
                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) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
    }
}

选择视频文件

java 复制代码
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);
    }
}
相关推荐
wang_peng33 分钟前
android studio 基础
android·ide·android studio
〆、风神2 小时前
EasyExcel 数据字典转换器实战:注解驱动设计
android·java·注解
stevenzqzq3 小时前
Android studio xml布局预览中 Automotive和Autotive Distant Display的区别
android·xml·android studio
QING6183 小时前
Kotlin commonPrefixWith用法及代码示例
android·kotlin·源码阅读
QING6183 小时前
Kotlin groupByTo用法及代码示例
android·kotlin·源码阅读
兰琛9 小时前
Compose组件转换XML布局
android·xml·kotlin
水w10 小时前
【Android Studio】解决报错问题Algorithm HmacPBESHA256 not available
android·开发语言·android studio
sqmeeting10 小时前
Linux NUC小主机化身视频会议服务器: 技术优势与部署实战
linux·服务器·windows·音视频·实时音视频
隐-梵12 小时前
Android studio进阶教程之(二)--如何导入高德地图
android·ide·android studio
Kika写代码13 小时前
【Android】界面布局-线性布局LinearLayout-例子
android·gitee