美文网首页
视频压缩

视频压缩

作者: 小婷android | 来源:发表于2021-03-19 16:42 被阅读0次
    第一步:跟build配置
     maven { url 'https://jitpack.io' }
    
    第二步:build文件下引用VideoProcessor
        implementation 'com.github.yellowcath:VideoProcessor:2.3.0'
    
    第三步:写一个压缩视频的共用方法
        public static void compressVideo2(Context context, String path, Handler.Callback callback) {
            String compressPath = Constant.video_path + File.separator + "VID_" + System.currentTimeMillis() + ".mp4";
            new Thread(() -> {
                boolean success = true;
                try {
                    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                    retriever.setDataSource(path);
                    int originWidth = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
                    int originHeight = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
                    int bitrate = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
    
                    int outWidth = originWidth / 2;
                    int outHeight = originHeight / 2;
                    start = System.currentTimeMillis();
                    VideoProcessor.processor(context)
                            .input(path)
                            .output(compressPath)
                            .bitrate(bitrate / 2)//这个属性不加,有些小视频反而越压缩越大
                            //以下参数全部为可选
    //                        .outWidth(outWidth)
    //                        .outHeight(outHeight)
    
    //                            .startTimeMs(startTimeMs)//用于剪辑视频
    //                            .endTimeMs(endTimeMs)    //用于剪辑视频
    //                            .speed(speed)            //改变视频速率,用于快慢放
    //                            .changeAudioSpeed(changeAudioSpeed) //改变视频速率时,音频是否同步变化
    //                            .bitrate(bitrate)       //输出视频比特率
    //                            .frameRate(frameRate)   //帧率
    //                            .iFrameInterval(iFrameInterval)  //关键帧距,为0时可输出全关键帧视频(部分机器上需为-1)
    //                            .progressListener(listener)      //可输出视频处理进度
                            .process();
                } catch (Exception e) {
                    success = false;
                    e.printStackTrace();
                }
                if (success) {
                    end = System.currentTimeMillis();
                    Log.e("aaa", "压缩耗时:" + (end - start) / 1000 + "秒");
                    Log.e("aaa", "视频压缩后大小:" + new File(compressPath).length() / 1024 / 1024 + "MB");
                    Log.e("aaa", "视频大小:" + new File(path).length() / 1024 / 1024 + "MB");
                    Message message = Message.obtain();
                    message.obj = compressPath;
                    callback.handleMessage(message);
                } else {
                }
            }).start();
    
    
        }
    
    
    第四步:activity中调用
       CompressUtils.compressVideo2(this,filePath, msg -> {
                                    String path= (String) msg.obj;
                                    File compressFile = new File(path);
    //                                Log.e("aaa","拍照信息:"+compressFile.getAbsolutePath());
                                    Log.e("aaa", "视频" + compressFile.length());
                                    return false;
                                });
    
    第四步: minSdkVersion 必须是21
      minSdkVersion 21
    

    相关文章

      网友评论

          本文标题:视频压缩

          本文链接:https://www.haomeiwen.com/subject/irxbcltx.html