使用七牛短视频SDK
https://blog.csdn.net/qq_31796651/article/details/79154072
/**
* 压缩视频
*
* @param filepath
*/
public void compressVideoResouce( String filepath) {
if (TextUtils.isEmpty(filepath)) {
showToast("请先选择转码文件!");
return;
}
String path = Environment.getExternalStorageDirectory() + "/sounds/" + DateUtil.getNowString("yyyy_MM_dd_HH_mm_ss") + "vv.mp4";
//PLShortVideoTranscoder初始化,三个参数,第一个context,第二个要压缩文件的路径,第三个视频压缩后输出的路径
PLShortVideoTranscoder mShortVideoTranscoder = new PLShortVideoTranscoder(k.app(), filepath, path);
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 = 6;//我这里选择的2500*1000压缩,这里可以自己选择合适的压缩比例
mShortVideoTranscoder.transcode(Integer.parseInt(width), Integer.parseInt(height),
ENCODING_BITRATE_LEVEL_ARRAY[transcodingBitrateLevel], false, new PLVideoSaveListener() {
@Override
public void onSaveVideoSuccess(String s) {
LogUtil.i("压缩成功==" + s);
//通知相册刷新
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(FileUtil.getFileByPath(s));
mediaScanIntent.setData(contentUri);
k.app().sendBroadcast(mediaScanIntent);
}
@Override
public void onSaveVideoFailed(final int errorCode) {
runOnUiThread(new Runnable() {
@Override
public void run() {
switch (errorCode) {
case ERROR_NO_VIDEO_TRACK:
showToast("该文件没有视频信息!");
break;
case ERROR_SRC_DST_SAME_FILE_PATH:
showToast("源文件路径和目标路径不能相同!");
break;
case ERROR_LOW_MEMORY:
showToast("手机内存不足,无法对该视频进行时光倒流!");
break;
default:
showToast("transcode failed: " + errorCode);
}
}
});
}
@Override
public void onSaveVideoCanceled() {
LogUtil.i("onSaveVideoCanceled");
}
@Override
public void onProgressUpdate(float percentage) {
LogUtil.i("onProgressUpdate==========" + percentage);
}
});
}
/**
* 选的越高文件质量越大,质量越好
*/
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,
};