1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| private boolean isCompressing; public static final String LOCAL_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.example.qinius/"; private float percentage; private long startTime; private String videoPath; private static MainActivity activity;
//开始压缩视频 private void startCompressed() { if (isCompressing) { return; } isCompressing = true;
Log.e("!!!!!!!!!!!!","LOCAL_PATH:" + LOCAL_PATH); Log.e("!!!!!!!!!!!!","videoPath:" + videoPath); //PLShortVideoTranscoder初始化,三个参数,第一个context,第二个要压缩文件的路径,第三个视频压缩后输出的路径 PLShortVideoTranscoder mShortVideoTranscoder = new PLShortVideoTranscoder(this, videoPath, LOCAL_PATH + "compress/transcoded.mp4"); MediaMetadataRetriever retr = new MediaMetadataRetriever(); retr.setDataSource(videoPath); 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 = 3; startTime = System.currentTimeMillis(); mShortVideoTranscoder.transcode(Integer.parseInt(width), Integer.parseInt(height), getEncodingBitrateLevel(transcodingBitrateLevel), false, new PLVideoSaveListener() { @Override public void onSaveVideoSuccess(String s) { handler.sendEmptyMessage(103); Log.e("!!!!!!!!!!!!","onSaveVideoSuccess:" + s); runOnUiThread(new Runnable() { @Override public void run() { ToastUtils.l(activity,"onSaveVideoSuccess======== "); } }); }
@Override public void onSaveVideoFailed(final int errorCode) { isCompressing = false; Log.e("!!!!!!!!!!!!", "save failed: " + errorCode); runOnUiThread(new Runnable() { @Override public void run() { ToastUtils.l(activity,"save failed======== "); } }); }
@Override public void onSaveVideoCanceled() { handler.sendEmptyMessage(101); Log.e("!!!!!!!!!!!!", "onSaveVideoCanceled"); }
@Override public void onProgressUpdate(float percentag) { percentage = percentag; handler.sendEmptyMessage(100); Log.e("!!!!!!!!!!!!", "onProgressUpdate:" + percentage); } }); }
Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { Log.e("!!!!!!!!!!!!", "msg.what:" + msg.what); switch (msg.what) { case 100: // textView.setText("" + percentage); break; case 101: // textView.setText("压缩取消了"); break; case 102: break; case 103: String compressedFilePath = LOCAL_PATH + "compress/transcoded.mp4"; // tv_endSize.setText("压缩后视频大小:" + getFormatSize(getFileSize(new File(compressedFilePath)))); // int time = (int) ((System.currentTimeMillis() - startTime) / 1000); // tv_time.setText("压缩用时:" + time + "秒"); // textView.setText("压缩完成"); break; } return false; } });
/** * 设置压缩质量 * * @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, };
/** * 获取指定文件大小 * * @return * @throws Exception */ private static long getFileSize(File file) { long size = 0; try { if (file.exists()) { FileInputStream fis = null; fis = new FileInputStream(file); size = fis.available(); } else { file.createNewFile(); Log.e("获取文件大小", "文件不存在!"); } return size; } catch (Exception e) { e.printStackTrace(); } return size; }
/** * 格式化单位 * * @param size */ public String getFormatSize(double size) { double kiloByte = size / 1024; if (kiloByte < 1) { return size + "MB"; } double megaByte = kiloByte / 1024; double gigaByte = megaByte / 1024; if (gigaByte < 1) { BigDecimal result2 = new BigDecimal(Double.toString(megaByte)); return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB"; } double teraBytes = gigaByte / 1024; if (teraBytes < 1) { BigDecimal result3 = new BigDecimal(Double.toString(gigaByte)); return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB"; } BigDecimal result4 = new BigDecimal(teraBytes); return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB"; }
/** * 从相册中选择视频 */ private void choiceVideo() { Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, 66); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 66 && resultCode == RESULT_OK && null != data) { Uri selectedVideo = data.getData(); String[] filePathColumn = {MediaStore.Video.Media.DATA};
Cursor cursor = getContentResolver().query(selectedVideo, filePathColumn, null, null, null); cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); videoPath = cursor.getString(columnIndex); cursor.close(); ToastUtils.l(activity, "videoPath:" + videoPath); } if (resultCode != Activity.RESULT_OK) { return; } }
|