// 精密压缩
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('取消上传')
}
})
},
小程序压缩视频 使用微信的compressVideo进行压缩,计算分辨率和码率关系进行压缩效果挺好
于 2022-03-25 11:30:39 首次发布