美文网首页
安卓视频压缩

安卓视频压缩

作者: 不略 | 来源:发表于2022-10-31 01:35 被阅读0次
1.依赖导入
 implementation 'com.github.yellowcath:VideoProcessor:2.3.0'
2.
public class CompressVideoUtils {

    public static void compressVideo2(Context context, String path, final CallBack callBack) {
        String compressPath = Constant.video_path + File.separator + "VID_" + System.currentTimeMillis() + ".mp4";
        new Thread(() -> {
            boolean success = true;
            long start = 0;
            long end;
            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)
                        .outHeight(outHeight)
                        .outWidth(outWidth)
                        .output(compressPath)
                        .bitrate(bitrate / 2)//这个属性不加,有些小视频反而越压缩越大
                        //以下参数全部为可选
//                        .outWidth(outWidth)
//                        .outHeight(outHeight)

//                            .startTimeMs(startTimeMs)//用于剪辑视频
//                            .endTimeMs(endTimeMs)    //用于剪辑视频
//                            .speed(2f)            //改变视频速率,用于快慢放
//                            .changeAudioSpeed(true) //改变视频速率时,音频是否同步变化
//                            .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("MAIN", "压缩耗时:" + (end - start) / 1000 + "秒");
                Log.e("MAIN", "视频压缩后大小:" + new File(compressPath).length() / 1024 / 1024 + "MB");
                File file = new File(path);
                long length = file.length();//B -1024->kb =1024=MB
                long kb = length / 1024;
                long mb = kb / 1024;
                int compressFileSize =  (int)new File(compressPath).length()/1024/1024;
//                new File(path).length()
                Log.e("MAIN", "视频大小:" + new File(path).length() / 1024 / 1024 + "MB");
                if(compressFileSize>=50){
                    //再次进行压缩
                    compressVideo2(context,compressPath,callBack);
                }else{
                    Log.w("TAG","onResult === " + compressPath);
                    callBack.onResult(compressPath);
                }
            } else {
            }
        }).start();


    }

    public interface CallBack{
        void onResult(String path);
    }

相关文章

网友评论

      本文标题:安卓视频压缩

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