微信小程序上传视频
微信开发者工具需要安装ffmpeg环境才能正常使用下面的官方方法。
1,调用官方提供的方法(wx.chooseMedia)
choosevideo(){
let that=this
console.log("上传视频的方法")
wx.chooseMedia({
count: 1,
mediaType:['video'],
sourceType:['album', 'camera'],
maxDuration: 58,
camera: 'back',
success:function(res){
let tempFilePath=res.tempFiles[0].tempFilePath
let duration=res.tempFiles[0].duration
console.log("视频播放时间为"+duration)
let size=parseFloat(res.tempFiles[0].size/1024/1024).toFixed(1)
console.log("视频大小为"+size)
let height=res.tempFiles[0].height
console.log("视频高度为"+height)
let width=res.tempFiles[0].width
console.log("视频宽度为"+width)
if(size>20){
let beyongSize=size-20
Toast("上传的视频大小超限,超出"+beyongSize+"MB,请重新上传!")
return
}else{
console.log("开始上传!!!")
that.uploadvideo({
url: api.uploadfiletofastdfs,
path: tempFilePath,
});
}
},
})
},
uploadvideo: function (data) {
wx.showLoading({
title: '上传中...',
mask: true,
})
var that = this
wx.compressVideo({
quality: 'high',
src: data.path,
success:function(res){
let size=parseFloat(res.size/1024/1024).toFixed(1)
console.log("压缩后视频大小为"+size)
that.setData({
sptemp:res.tempFilePath
})
wx.uploadFile({
url: data.url,
filePath: res.tempFilePath,
name: 'uploadFile',
header: {
"X-Access-Token":wx.getStorageSync('token')
},
success: (resp) => {
wx.hideLoading();
console.log(resp)
},
});
},
})
},
2,页面中用标签将上传的视频显示出来
<video src="{{sptemp}}" ></video>
3,效果展示
|