一个肯定可以满足你需求的视频压缩方案

  主要采用七牛短视频压缩算法,jar包下载链接

  jar包使用:

  1.将jar包放到项目libs目录下并作为依赖包使用:
一个肯定可以满足你需求的视频压缩方案_第1张图片

  2.将对应so文件放到项目jniLibs目录下:
一个肯定可以满足你需求的视频压缩方案_第2张图片

  调用压缩

public class VideoCompressUtil {

    public static final int VIDEO_COMPRESS_SUCCESS = 1;
    public static final int VIDEO_COMPRESS_FAIL = 2;
    public static final int VIDEO_COMPRESS_CANCLE = 3;
    public static final String COMPRESS_FAIL_MSG = "failMsg";
    public static final String COMPRESS_ORIGINAL_PATH = "originalPath";

    private static volatile VideoCompressUtil mInstance;

    private VideoCompressUtil() {
    }

    public static VideoCompressUtil newInstance() {
        if (mInstance == null) {
            synchronized (VideoCompressUtil.class) {
                if (mInstance == null) {
                    mInstance = new VideoCompressUtil();
                }
            }
        }
        return mInstance;
    }

    /**
     * 压缩视频
     *
     * @param mContext 源文件路径
     * @param filepath
     */
    public void compressVideoResouce(Context mContext, String filepath, Handler handler) {
        if (TextUtils.isEmpty(filepath)) {
            ToastUtils.show(mContext, "请先选择转码文件!");
            return;
        }

        DialogCompressVideoBinding dataBinding = DataBindingUtil.inflate
                (LayoutInflater.from(mContext), R.layout.dialog_compress_video,
                        null, false);
        dataBinding.progress.setMax(100);
        AlertDialog alertDialog = new AlertDialog.Builder(mContext,R.style.BDAlertDialog)
                .setCancelable(false)
                .setView(dataBinding.getRoot())
                .create();
        alertDialog.show();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        File originalVideo=new File(filepath);
        Logger.e("视频压缩开始时间======"+simpleDateFormat.format(new Date())+"===视频大小==="+
                Formatter.formatFileSize(mContext,originalVideo.length()));

        //PLShortVideoTranscoder初始化,三个参数,第一个context,第二个要压缩文件的路径,第三个视频压缩后输出的路径
        PLShortVideoTranscoder mShortVideoTranscoder = new PLShortVideoTranscoder(mContext,
                filepath, mContext.getExternalCacheDir().getAbsolutePath()+File.pathSeparator+ "compress/transcoded" +
                ".mp4");
        MediaMetadataRetriever retr = new MediaMetadataRetriever();
        retr.setDataSource(filepath);
        String height = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
        // 视频高度
        String width = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); //
        // 视频宽度
        String rotation = retr.extractMetadata(MediaMetadataRetriever
                .METADATA_KEY_VIDEO_ROTATION); // 视频旋转方向
        int transcodingBitrateLevel = 0;//我这里选择的2500*1000压缩,这里可以自己选择合适的压缩比例
        mShortVideoTranscoder.transcode(Integer.parseInt(width), Integer.parseInt(height),
                getEncodingBitrateLevel(transcodingBitrateLevel), false, new PLVideoSaveListener() {
            @Override
            public void onSaveVideoSuccess(String s) {
                alertDialog.dismiss();
                Logger.e("视频压缩结束时间======"+simpleDateFormat.format(new Date())+"===视频大小==="+
                        Formatter.formatFileSize(mContext,new File(s).length()));
                Message message = Message.obtain();
                message.what = VIDEO_COMPRESS_SUCCESS;
                message.obj = s;
                handler.sendMessage(message);
            }

            @Override
            public void onSaveVideoFailed(final int errorCode) {
                alertDialog.dismiss();
                String failMsg;
                switch (errorCode) {
                    case ERROR_NO_VIDEO_TRACK:
                        failMsg = "该文件没有视频信息!";
                        break;
                    case ERROR_SRC_DST_SAME_FILE_PATH:
                        failMsg = "源文件路径和目标路径不能相同";
                        break;
                    case ERROR_LOW_MEMORY:
                        failMsg = "手机内存不足,无法对该视频进行时光倒流!";
                        break;
                    default:
                        failMsg = "压缩失败:" + errorCode;
                        break;
                }

                Message message = Message.obtain();
                message.what = VIDEO_COMPRESS_FAIL;
                Bundle bundle = new Bundle();
                bundle.putString(COMPRESS_FAIL_MSG, failMsg);
                bundle.putString(COMPRESS_ORIGINAL_PATH, filepath);
                message.setData(bundle);
                handler.sendMessage(message);
            }

            @Override
            public void onSaveVideoCanceled() {
                Message message = Message.obtain();
                message.what = VIDEO_COMPRESS_CANCLE;
                handler.sendMessage(message);
            }

            @Override
            public void onProgressUpdate(float percentage) {
                dataBinding.progress.setProgress((int) (percentage * 100));
            }
        });

        dataBinding.tvCancle.setOnClickListener(view -> {
            mShortVideoTranscoder.cancelTranscode();
            alertDialog.dismiss();
        });
    }

    /**
     * 设置压缩质量
     *
     * @param position
     * @return
     */
    private int getEncodingBitrateLevel(int position) {
        return ENCODING_BITRATE_LEVEL_ARRAY[position];
    }

    /**
     * 选的越高文件质量越大,质量越好
     */
    public static final int[] ENCODING_BITRATE_LEVEL_ARRAY = {
            500 * 1000,
            800 * 1000,
            1000 * 1000,
            1200 * 1000,
            1600 * 1000,
            2000 * 1000,
            2500 * 1000,
            4000 * 1000,
            8000 * 1000,
    };
}

你可能感兴趣的:(安卓基础)