小程序压缩视频 使用微信的compressVideo进行压缩,计算分辨率和码率关系进行压缩效果挺好

            // 精密压缩
            upLoladVideoFn(src,img,bitrate,resolution){
                wx.showLoading({
                    title:'视频上传中...',
                    icon:'none',
                    mask:true
                })
                // 压缩并上传
                wx.compressVideo({
                    src,
                    bitrate,
                    fps: 30,
                    resolution,
                    success:(resp)=>{
                        console.log(resp,'------压缩成功')
                    },fail:(err)=>{
                        wx.hideLoading()
                    }
                })
            },
            // 调用本地视频
            addVideo(){
                let _this = this
                wx.chooseMedia({
                    count: 1,
                    mediaType: ['video'],
                    sourceType: ['album', 'camera'],
                    maxDuration: 10,
                    camera: 'back',
                    sizeType:['original'],
                    success(res) {
                        console.log(res,'----------视频')
                        wx.getNetworkType({
                            success (res) {
                                wx.setStorageSync('newsWork',res.networkType)
                            }
                        })
                        _this.mediaType = 2
                        _this.suffix = []
                        _this.photoList = []
                        _this.imgCount = 0
                        _this.duration  = {
                            time:res.tempFiles[0].duration,
                            width:res.tempFiles[0].width,
                            height:res.tempFiles[0].height
                        }
                        wx.getVideoInfo({
                            src:res.tempFiles[0].tempFilePath,
                            success:( videoInfo )=>{
                                console.log(videoInfo,'----res-getVideoInfo--------res')
                                // 视频上传最大限制
                                if(videoInfo.size > 100000){
                                    wx.showModal({
                                        content: '视频最大为100M',
                                        success (res) {
                                            if (res.confirm) {
                                                console.log('用户点击确定')
                                                // that.btn_mediaType()
                                            } else if (res.cancel) {
                                                console.log('用户点击取消')
                                            }
                                        }
                                    })
                                    return
                                }
                                // 压缩视频
                                var canvasWidth= videoInfo.width;
                                var canvasHeight = videoInfo.height;
                                var resolution = 0
                                var bitrate = 0
                                                              //压缩比例
                                // 最大尺寸限制
                                var maxWidth = 1280; //最大宽度
                                
                                var maxW = 576 //最小宽度
                                // 目标尺寸
                                var targetWidth = canvasWidth
                                var targetHeight = canvasHeight;
                                if( canvasHeight > canvasWidth && canvasWidth > maxW  ){  // 高视频
                                    targetHeight = Math.round( maxW * ( canvasHeight/ canvasWidth))
                                    targetWidth = maxW
                                }
                                if( canvasWidth > canvasHeight && canvasWidth > maxWidth ){ // 宽视频
                                    targetWidth = canvasWidth
                                    targetHeight = canvasHeight;

                                    targetWidth = maxWidth
                                    targetHeight = Math.round(maxWidth * (canvasHeight / canvasWidth))
                                }

                                _this.imgSize = targetWidth + ',' + targetHeight
                                _this.shangchuanhouduan(res.tempFiles[0].thumbTempFilePath,'0') //图片
                                 // 视频小于5M 直接上传
                                if(videoInfo.size<5000){
                                    _this.upLoladVideoFn1(res.tempFiles[0].tempFilePath,res.tempFiles[0].thumbTempFilePath) 
                                    return
                                }
                                // 计算码率和分辨率
                                if( targetHeight >= 720 && videoInfo.bitrate >= 1500 && canvasHeight > canvasWidth){
                                    bitrate = 1400
                                    resolution = (targetHeight / videoInfo.height).toFixed(2)
                                } else if( targetHeight >= 720 && videoInfo.bitrate >= 1500){
                                    bitrate = 1300
                                    resolution = (targetHeight / videoInfo.height).toFixed(2)
                                } else if( targetHeight >= 720 && videoInfo.bitrate < 1500 ){
                                    bitrate = 900
                                    resolution = (targetHeight / videoInfo.height).toFixed(2)
                                } else if( targetHeight < 720 && videoInfo.bitrate >= 900){
                                    bitrate = 700
                                    resolution = 1
                                } else if( targetHeight < 720 && videoInfo.bitrate < 900){
                                    bitrate = videoInfo.bitrate
                                    resolution = 1
                                } 
                                _this.upLoladVideoFn(res.tempFiles[0].tempFilePath,res.tempFiles[0].thumbTempFilePath ,bitrate,resolution) 


                            },fail:(err)=>{
                                console.log(err,'-000000000')
                            }
                        })
                    },fail:(err)=>{
                        console.log('取消上传')
					}
                })
            },
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值