視頻壓縮

第一步:跟build配置
 maven { url 'https://jitpack.io' }
第二步:build文件下引用VideoProcessor
    implementation 'com.github.yellowcath:VideoProcessor:2.3.0'
第三步:寫一個(gè)壓縮視頻的共用方法
    public static void compressVideo2(Context context, String path, Handler.Callback callback) {
        String compressPath = Constant.video_path + File.separator + "VID_" + System.currentTimeMillis() + ".mp4";
        new Thread(() -> {
            boolean success = true;
            try {
                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                retriever.setDataSource(path);
                int originWidth = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
                int originHeight = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
                int bitrate = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));

                int outWidth = originWidth / 2;
                int outHeight = originHeight / 2;
                start = System.currentTimeMillis();
                VideoProcessor.processor(context)
                        .input(path)
                        .output(compressPath)
                        .bitrate(bitrate / 2)//這個(gè)屬性不加,有些小視頻反而越壓縮越大
                        //以下參數(shù)全部為可選
//                        .outWidth(outWidth)
//                        .outHeight(outHeight)

//                            .startTimeMs(startTimeMs)//用于剪輯視頻
//                            .endTimeMs(endTimeMs)    //用于剪輯視頻
//                            .speed(speed)            //改變視頻速率,用于快慢放
//                            .changeAudioSpeed(changeAudioSpeed) //改變視頻速率時(shí),音頻是否同步變化
//                            .bitrate(bitrate)       //輸出視頻比特率
//                            .frameRate(frameRate)   //幀率
//                            .iFrameInterval(iFrameInterval)  //關(guān)鍵幀距,為0時(shí)可輸出全關(guān)鍵幀視頻(部分機(jī)器上需為-1)
//                            .progressListener(listener)      //可輸出視頻處理進(jìn)度
                        .process();
            } catch (Exception e) {
                success = false;
                e.printStackTrace();
            }
            if (success) {
                end = System.currentTimeMillis();
                Log.e("aaa", "壓縮耗時(shí):" + (end - start) / 1000 + "秒");
                Log.e("aaa", "視頻壓縮后大?。? + new File(compressPath).length() / 1024 / 1024 + "MB");
                Log.e("aaa", "視頻大小:" + new File(path).length() / 1024 / 1024 + "MB");
                Message message = Message.obtain();
                message.obj = compressPath;
                callback.handleMessage(message);
            } else {
            }
        }).start();


    }

第四步:activity中調(diào)用
   CompressUtils.compressVideo2(this,filePath, msg -> {
                                String path= (String) msg.obj;
                                File compressFile = new File(path);
//                                Log.e("aaa","拍照信息:"+compressFile.getAbsolutePath());
                                Log.e("aaa", "視頻" + compressFile.length());
                                return false;
                            });
第四步: minSdkVersion 必須是21
  minSdkVersion 21
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容