提取视频文件里的音频和无声视频

一、提取视频文件里的音频:

java 复制代码
    public static void generateMediaRadio(){
        // 视频提取器
        MediaExtractor extractor = new MediaExtractor();
        try {
            //本地视频文件
            extractor.setDataSource("/storage/emulated/0/mjyyfep/alpha.mp4");
            //网络视频文件
//                extractor.setDataSource(OwnApplicationLike.getInstance(),Uri.parse("http://zhl-file.xxfz.com.cn/group1/M08/07/B2/dr5jH1_Hd9CIbYreAAAAPBKTEOgAALdDQPnvw0AAABU754.mp4"),null);
        } catch (Exception e) {
            e.printStackTrace();
//            showShortToast("视频资源路径出错");
            LogUtil.eLog("视频资源路径出错");
        }
        // 获取通道数
        int trackCount = extractor.getTrackCount();
        // 视频轨道索引
        int radioTrackIndex = 0;
        // 视频格式
        MediaFormat radioMediaFormat = null;
        long radioFrameRate = 0;

        // 查找需要的视频轨道与音频轨道index
        for (int i = 0; i < trackCount; i++) { //遍历所以轨道
            MediaFormat itemMediaFormat = extractor.getTrackFormat(i);
            String itemMime = itemMediaFormat.getString(MediaFormat.KEY_MIME);
            if (itemMime.startsWith("audio")) { //获取音频轨道位置
                radioTrackIndex = i;
                radioMediaFormat = itemMediaFormat;
//                Log.d(TAG,"audio format start --- ");
//                Log.d(TAG,"audio format --> \r\n"+itemMediaFormat.toString());
//                Log.d(TAG,"audio format end --- ");

                extractor.selectTrack(i);
                long first_sampletime = extractor.getSampleTime();
                extractor.advance();
                long second_sampletime = extractor.getSampleTime();
                radioFrameRate = Math.abs(second_sampletime - first_sampletime);//时间戳
                extractor.unselectTrack(i);

            }
        }

//        File outVideoFile = new File(getFileOutPath() + File.separator + "out_audio.mp4");
        //提取出的音频文件最终存放路径
        File outVideoFile = new File("/storage/emulated/0/mjyyfep/alpha_audio_1.mp3");
        if(outVideoFile.exists()){
            outVideoFile.delete();
        }

        try{
            // 分离音频
            MediaMuxer mediaMuxer = new MediaMuxer(outVideoFile.getAbsolutePath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
            int mVideoTrackIndex  = mediaMuxer.addTrack(radioMediaFormat);
            mediaMuxer.start();
            MediaCodec.BufferInfo info =  new MediaCodec.BufferInfo();
            info.presentationTimeUs =  0;

            extractor.selectTrack(radioTrackIndex);
            int videoFlag = extractor.getSampleFlags();
            int length;
            ByteBuffer buffer = ByteBuffer.allocate(100 * 1024);
            while ((length = extractor.readSampleData(buffer,0)) != -1){
                info.offset = 0;
                info.size = length;
                info.flags = videoFlag;
                info.presentationTimeUs += radioFrameRate;
                mediaMuxer.writeSampleData(mVideoTrackIndex, buffer, info);
                // 预先加载后面的数据
                extractor.advance();
            }

            extractor.release();
            mediaMuxer.stop();
            mediaMuxer.release();

//            showShortToast("音频生成成功");
            LogUtil.eLog("音频生成成功");
        }catch (Exception e){
            e.printStackTrace();
            LogUtil.eLog("音频生成失败");
//            showShortToast("音频生成失败");
        }
    }

二、提取视频文件里的无声视频

java 复制代码
    public static void generateMediaVideo(){
        // 视频提取器
        MediaExtractor extractor = new MediaExtractor();
        try {
            //本地视频文件
//            extractor.setDataSource("/storage/emulated/0/mjyyfep/my.mp4");
            //网络视频文件
            extractor.setDataSource(OwnApplicationLike.getInstance(),Uri.parse("http://zhl-file.xxfz.com.cn/group1/M08/07/B2/dr5jH1_Hd9CIbYreAAAAPBKTEOgAALdDQPnvw0AAABU754.mp4"),null);
        } catch (IOException e) {
            e.printStackTrace();
            LogUtil.eLog("视频资源路径出错");
        }
        // 获取通道数
        int trackCount = extractor.getTrackCount();
        // 视频轨道索引
        int videoTrackIndex = 0;
        // 视频格式
        MediaFormat videoMediaFormat = null;

        // 查找需要的视频轨道与音频轨道index
        for (int i = 0; i < trackCount; i++) { //遍历所以轨道
            MediaFormat itemMediaFormat = extractor.getTrackFormat(i);
            String itemMime = itemMediaFormat.getString(MediaFormat.KEY_MIME);
            if (itemMime.startsWith("video")) { //获取视频轨道位置
                videoTrackIndex = i;
                videoMediaFormat = itemMediaFormat;
                LogUtil.eLog("video format start --- ");
                LogUtil.eLog("video format --> \r\n"+itemMediaFormat.toString());
                LogUtil.eLog("video format end --- ");

            }
        }

//        File outVideoFile = new File(getFileOutPath() + File.separator + "out_video.mp4");
        //提取出的无声视频最终存放路径
        File outVideoFile = new File("/storage/emulated/0/mjyyfep/my_video_4.mp4");
        if(outVideoFile.exists()){
            outVideoFile.delete();
        }

        try{
            // 分离视频
            MediaMuxer mediaMuxer = new MediaMuxer(outVideoFile.getAbsolutePath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
            int mVideoTrackIndex  = mediaMuxer.addTrack(videoMediaFormat);
            mediaMuxer.start();
            MediaCodec.BufferInfo info =  new MediaCodec.BufferInfo();
            info.presentationTimeUs =  0;

            extractor.selectTrack(videoTrackIndex);
            int videoFlag = extractor.getSampleFlags();
            long videoFrameRate = 1000 * 1000 /(videoMediaFormat.getInteger(MediaFormat.KEY_FRAME_RATE));
            int length;
            ByteBuffer buffer = ByteBuffer.allocate(100 * 1024);
            while ((length = extractor.readSampleData(buffer,0)) != -1){
                info.offset = 0;
                info.size = length;
                info.flags = videoFlag;
                info.presentationTimeUs += videoFrameRate;
                mediaMuxer.writeSampleData(mVideoTrackIndex, buffer, info);
                // 预先加载后面的数据
                extractor.advance();
            }

            extractor.release();
            mediaMuxer.stop();
            mediaMuxer.release();

            LogUtil.eLog("生成视频文件成功");
        }catch (Exception e){
            e.printStackTrace();
            LogUtil.eLog("视频生成失败");
        }
    }
相关推荐
ACP广源盛139246256731 小时前
GSV6127E(EA)#Type-C / 显示端口 1.4/HDMI 2.0 转 MIPI/LVDS 转换器(带音频提取和嵌入式微控制器)
单片机·嵌入式硬件·音视频
wangchen01123 小时前
基于视频识别的大模型项目实战心得
语言模型·音视频
北极糊的狐7 小时前
狸窝转换器将MP4格式视频转换为以下格式后的大小对比:RM、RMVB、AVI、MKV、WMV、VOB、MOV、FLV、ASF、DAT、3GP、MPG、MPEG
音视频
是Dream呀8 小时前
一个账号调用N个AI模型!从LLM到视频生成的丝滑解决方案
人工智能·大模型·aigc·音视频·deepseek
顾北川_野1 天前
播放PCM音频增益低+单独增强PCM解码的方案
音视频·pcm
Everbrilliant891 天前
FFmpeg解码音频数据AudioTrack/OpenSL播放
ffmpeg·音视频·audiotrack·opensl·ffmpeg音频解码播放·decodethread·opensl播放与解码同步
ivy159868377151 天前
JM20329是一款高性能、低功耗的USB桥接芯片,实现串行接口(如SATA、IDE)与USB接口之间的数据转换。
c语言·开发语言·ide·嵌入式硬件·eureka·音视频·视频编解码
温暖名字1 天前
调用qwen3-omni的api对本地文件生成视频文本描述(批量生成)
python·音视频·qwen·qa问答
太阳人7981 天前
MIPI D-PHY/C-PHY接收器压力眼图测试介绍
功能测试·嵌入式硬件·音视频·硬件工程
blackorbird1 天前
视频生成类大模型 Sora 2 系统提示提取技术研究
音视频