Android音视频类常用

不定期更新

视频压缩

硬解压缩(非FFMpeg压缩)
https://github.com/wuxiaoqiang625/VideoCompress
优点:轻量化,支持百分比回调,压缩时间较短,接入快(无需学习FFMpeg命令)
缺点:低端手机硬解可能存在兼容性差

使用方法:


源码内是按比例压缩(例如压缩至原大小的1/7),会导致低画质视频还进行压缩,最终压缩后分辨率极低的问题。可修改源码部分方法,实现具体压缩需求。例如:高分辨视频压缩至720p,低于720p压缩至一半,修改如下:

case COMPRESS_QUALITY_LOW:
//    原压缩逻辑
//                resultWidth = originalWidth / 2;
//                resultHeight = originalHeight / 2;
//                bitrate = (resultWidth/2) * (resultHeight/2) * 10;

               //压缩优化,高分辨视频压缩至720p,低于720p压缩至一半
              double scale = 1;
               if (originalWidth <= 720 || originalHeight <= 720) {
                   resultWidth = originalWidth / 2;
                   resultHeight = originalHeight / 2;
               } else {
                   double scaleW = 1.0 * originalWidth / 720;
                   double scaleH = 1.0 * originalHeight / 720;
                   if (scaleH < scaleW) {
                       scale = scaleH;
                   } else {
                       scale = scaleW;
                   }
                   resultWidth =(int)(originalWidth / scale);
                   resultHeight = (int)(originalHeight / scale);
                   scale = scale * 2;
               }
               bitrate =(int)((resultWidth/scale) * (resultHeight/scale) * 10);

详细修改代码片段所在类
VideoController.java#convertVideo

 @TargetApi(16)
    public boolean  convertVideo(final String sourcePath, String destinationPath, int quality, CompressProgressListener listener) {
        this.path=sourcePath;

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(path);
        String width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
        String height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
        String rotation = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
        long duration = Long.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)) * 1000;

        long startTime = -1;
        long endTime = -1;

        int rotationValue = Integer.valueOf(rotation);
        int originalWidth = Integer.valueOf(width);
        int originalHeight = Integer.valueOf(height)

        int resultWidth;
        int resultHeight;
        int bitrate;
        switch (quality) {
            default:
            case COMPRESS_QUALITY_HIGH:
//                resultWidth = originalWidth * 2 / 3;
//                resultHeight = originalHeight * 2 / 3;
//                bitrate = resultWidth * resultHeight * 30;
                resultWidth = originalWidth / 2;
                resultHeight = originalHeight / 2;
                bitrate = resultWidth * resultHeight * 10;
                break;
            case COMPRESS_QUALITY_MEDIUM:
//                resultWidth = originalWidth / 2;
//                resultHeight = originalHeight / 2;
//                bitrate = resultWidth * resultHeight * 10;
                resultWidth = originalWidth / 2;
                resultHeight = originalHeight / 2;
                bitrate = (resultWidth/2) * (resultHeight/2) * 10;
                break;
            case COMPRESS_QUALITY_LOW:
//    原压缩逻辑
//                resultWidth = originalWidth / 2;
//                resultHeight = originalHeight / 2;
//                bitrate = (resultWidth/2) * (resultHeight/2) * 10;

                //压缩优化,高分辨视频压缩至720p,低于720p压缩至一半
               double scale = 1;
                if (originalWidth <= 720 || originalHeight <= 720) {
                    resultWidth = originalWidth / 2;
                    resultHeight = originalHeight / 2;
                } else {
                    double scaleW = 1.0 * originalWidth / 720;
                    double scaleH = 1.0 * originalHeight / 720;
                    if (scaleH < scaleW) {
                        scale = scaleH;
                    } else {
                        scale = scaleW;
                    }
                    resultWidth =(int)(originalWidth / scale);
                    resultHeight = (int)(originalHeight / scale);
                    scale = scale * 2;
                }
                bitrate =(int)((resultWidth/scale) * (resultHeight/scale) * 10);
                break;
        }
        .........

}

你可能感兴趣的:(Android音视频类常用)