视频压缩(支持图片,视频)

https://github.com/Tourenathan-G5organisation/SiliCompressor

 

// 大于20m自动压缩
CompressVideoUtils.getCompressVideoTask(this, 450, 20, 1000, 400, new CompressVideoUtils.OnCompressListener() {
    @Override
    public void onFinishCompress(String path,boolean isCompressed) {
        

    }
}).execute(filePath  // 源文件文件夹
        , "/storage/emulated/0/mubiaowenjianjia");//目标文件夹
/**
 * created by  sqj  on 2018/12/27 11:24
 * 视频压缩工具类
 */
public class CompressVideoUtils {


    static int mTargetMaxSize = 450;// 目标最大控制size 超过就按照这个比例压缩
    static int mTargetMinSize = 20;// 目标最小控制size 超过这个值才进行压缩
    static int mMaxBit = 1000; //目标最小控制size的压缩率
    static int mMinBit = 400;//目标最大控制size的压缩率

    static Context mContext;
    private static String filePath;
    private static RefreshVideoDialog handler;
    private static boolean isCompressed;


    public static CompressVideoTask getCompressVideoTask(Context context, int targetMaxSize
            , int targetMinSize, int maxBit, int minBit, OnCompressListener compressListener) {
        mTargetMaxSize = 450;
        mTargetMinSize = 20;
        mMaxBit = 1000;
        mMinBit = 400;
        getHandler();
        return new CompressVideoTask(context,targetMaxSize
                , targetMinSize,maxBit,minBit, compressListener);
    }

    private static void getHandler(){
        if (handler == null){
            handler = new RefreshVideoDialog();
        }
    }

    private static ProgressDialog mVideoCompressProgressDialog;

    public static class RefreshVideoDialog extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    mVideoCompressProgressDialog = ProgressDialog.show(mContext, "", "视频压缩中...", true);
                    break;
                case 1:
                    if (mVideoCompressProgressDialog != null) {
                        mVideoCompressProgressDialog.dismiss();
                        mVideoCompressProgressDialog = null;
                    }
                    break;



            }
        }
    }


    public static class CompressVideoTask extends AsyncTask<String, String, String> {





        public CompressVideoTask(Context context) {
            mContext = context;
        }

        public CompressVideoTask(Context context, int targetMaxSize
                , int targetMinSize, int maxBit, int minBit, OnCompressListener compressListener) {
            mOnCompressListener = compressListener;
            mTargetMaxSize = targetMaxSize == 0 ? mTargetMaxSize : targetMaxSize;
            mTargetMinSize = targetMinSize == 0 ? mTargetMinSize : targetMinSize;
            mMaxBit = maxBit == 0 ? mMaxBit : maxBit;
            mMinBit = minBit == 0 ? mMinBit : minBit;
            mContext = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
//            imageView.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_photo_camera_white_48px));
//            compressionMsg.setVisibility(View.VISIBLE);
//            picDescription.setVisibility(View.GONE);
            Log.d("VideoCompressAsyncTask", "stask" + "onPreExecute");
        }

        @Override
        protected String doInBackground(String... paths) {
            File imageFile = new File(paths[0]);
            float length = imageFile.length() / 1024f / 1024f; // kb
            filePath = null;
            if (length > mTargetMinSize) {
                isCompressed = true;
                handler.sendEmptyMessage(0);

                // 大于限制大小才压缩

                try {
                    // 450m 最高使用400*1000 比特率
                    // 0m使用1000 *1000 比特率
                    // 1000 - (600  / 450)*size
                    int bitrate = (int) (mMaxBit - ((mMaxBit - mMinBit) / mTargetMaxSize) *
                            (new File(paths[0]).length() / 1024 / 1024));
                    if (bitrate < mMinBit) {
                        bitrate = mMinBit;
                    }
                    filePath = SiliCompressor.with(mContext)
                            .compressVideo(paths[0], paths[1], 0, 0, bitrate * 1000);

                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            } else {
                filePath = paths[0];
                isCompressed = false;
            }

            return filePath;

        }


        @Override
        protected void onPostExecute(String compressedFilePath) {
            super.onPostExecute(compressedFilePath);
            File imageFile = new File(compressedFilePath);
            float length = imageFile.length() / 1024f; // Size in KB
            String value;
            if (length >= 1024)
                value = length / 1024f + " MB";
            else
                value = length + " KB";
//            String text = String.format(Locale.US, "%s\nName: %s\nSize: %s", getString(R.string.video_compression_complete), imageFile.getName(), value);
//            compressionMsg.setVisibility(View.GONE);
//            picDescription.setVisibility(View.VISIBLE);
//            picDescription.setText(text);


//        mVideoCompressProgressDialog.dismiss();
//        sendVideo(compressedFilePath);
            handler.sendEmptyMessage(1);

            mOnCompressListener.onFinishCompress(compressedFilePath,isCompressed);
            LogUtils.i("Silicompressor", "Path: " + compressedFilePath);
        }

        private OnCompressListener mOnCompressListener;



    }
    public interface OnCompressListener {
        void onFinishCompress(String path,boolean isCompressed);
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值