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);
}
}