138 lines
4.0 KiB
JavaScript
138 lines
4.0 KiB
JavaScript
|
const videoTools = {
|
|||
|
/**
|
|||
|
* @description 视频选择,并返回
|
|||
|
*/
|
|||
|
chooseVideo() {
|
|||
|
return new Promise((resolve,reject)=>{
|
|||
|
let videoObj = {};
|
|||
|
uni.chooseVideo({
|
|||
|
sourceType: ['camera', 'album'],
|
|||
|
compressed:true,//是否压缩所选的视频源文件,默认值为 true,需要压缩。
|
|||
|
maxDuration:60,//拍摄视频最长拍摄时间,单位秒。最长支持 60 秒。
|
|||
|
camera: 'back',//'front'、'back',默认'back'
|
|||
|
success: (res)=> {
|
|||
|
console.log(res,'视频');
|
|||
|
// #ifdef H5
|
|||
|
videoObj = {
|
|||
|
size:res.size,//视频大小
|
|||
|
path:res.name,//视频名称
|
|||
|
lastModified:res.tempFile.lastModified,//最后修改时间
|
|||
|
type:res.tempFile.type,//视频类型
|
|||
|
}
|
|||
|
// #endif
|
|||
|
|
|||
|
// #ifdef APP-PLUS || MP-WEIXIN
|
|||
|
videoObj = {
|
|||
|
size:res.size,//视频大小
|
|||
|
path:res.tempFilePath,//视频名称
|
|||
|
duration:res.duration,//播放总时长单位秒
|
|||
|
width:res.width,//视频宽
|
|||
|
height:res.height,//视频高
|
|||
|
}
|
|||
|
// #endif
|
|||
|
resolve(videoObj)
|
|||
|
}
|
|||
|
});
|
|||
|
})
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 选择视频并压缩视频,不支持H5
|
|||
|
*/
|
|||
|
chooseCompressVideo() {
|
|||
|
return new Promise((resolve, reject)=>{
|
|||
|
uni.chooseVideo({
|
|||
|
sourceType: ['camera', 'album'],
|
|||
|
compressed:true,//是否压缩所选的视频源文件,默认值为 true,需要压缩。
|
|||
|
maxDuration:60,//拍摄视频最长拍摄时间,单位秒。最长支持 60 秒。
|
|||
|
camera: 'back',//'front'、'back',默认'back'
|
|||
|
success: (res)=> {
|
|||
|
let exit = res.tempFilePath.split('.').includes('mp4');
|
|||
|
if(exit) {
|
|||
|
// #ifdef APP-PLUS || MP-WEIXIN
|
|||
|
uni.compressVideo({
|
|||
|
src:res.tempFilePath,
|
|||
|
quality:'medium',//low:低 medium:中 high:高
|
|||
|
bitrate: 100000,//码率,单位 kbps
|
|||
|
fps: 100,//帧率
|
|||
|
resolution:1,//相对于原视频的分辨率比例,取值范围(0, 1]
|
|||
|
success: rescomp => {
|
|||
|
resolve({path:rescomp.tempFilePath,size:rescomp.size})
|
|||
|
}
|
|||
|
})
|
|||
|
// #endif
|
|||
|
} else {
|
|||
|
uni.showToast({title:'请选择mp4格式的视频',icon:'none'})
|
|||
|
}
|
|||
|
}
|
|||
|
});
|
|||
|
})
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 预览视频,仅仅微信小程序生效
|
|||
|
* @param {Number} current 0
|
|||
|
* @param {Array} videoArr [{url:'',type:'video',poster:''}]
|
|||
|
*/
|
|||
|
previewVideo(current=0,videoArr=[]) {
|
|||
|
// #ifdef MP-WEIXIN
|
|||
|
uni.previewMedia({
|
|||
|
current:current,
|
|||
|
sources:videoArr
|
|||
|
})
|
|||
|
// #endif
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 保存视频,不支持H5
|
|||
|
* @param {String} src
|
|||
|
*/
|
|||
|
saveVideo(src) {
|
|||
|
uni.saveVideoToPhotosAlbum({
|
|||
|
filePath: src,
|
|||
|
success: ()=> {}
|
|||
|
});
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 压缩视频,不支持H5
|
|||
|
* @param {String} src
|
|||
|
*/
|
|||
|
compressVideo(src) {
|
|||
|
return new Promise((resolve, reject)=>{
|
|||
|
// #ifdef APP-PLUS || MP-WEIXIN
|
|||
|
uni.compressVideo({
|
|||
|
src,
|
|||
|
quality:'medium',//low:低 medium:中 high:高
|
|||
|
bitrate: 100000,//码率,单位 kbps
|
|||
|
fps: 100,//帧率
|
|||
|
resolution:1,//相对于原视频的分辨率比例,取值范围(0, 1]
|
|||
|
success: rescomp => {
|
|||
|
resolve({path:rescomp.tempFilePath,size:rescomp.size})
|
|||
|
}
|
|||
|
})
|
|||
|
// #endif
|
|||
|
})
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 获取视频信息
|
|||
|
* @param {String} src
|
|||
|
*/
|
|||
|
getVideoInfo(src) {
|
|||
|
return new Promise((resolve, reject)=>{
|
|||
|
uni.getVideoInfo({
|
|||
|
src,
|
|||
|
success: (video)=> {
|
|||
|
let obj = {
|
|||
|
orientation:video.orientation,//画面方向 微信小程序、App(3.1.14+)
|
|||
|
type:video.type,//视频格式 微信小程序、App(3.1.14+)
|
|||
|
duration:video.duration,//视频长度 微信小程序、App(3.1.10+)、H5
|
|||
|
size:video.size,//视频大小,单位 kB 微信小程序、App(3.1.10+)、H5
|
|||
|
height:video.height,//视频的长,单位 px 微信小程序、App(3.1.10+)、H5
|
|||
|
width:video.width,//视频的宽,单位 px 微信小程序、App(3.1.10+)、H5
|
|||
|
fps:video.fps,//视频帧率 微信小程序、App(3.1.14+)
|
|||
|
bitrate:video.bitrate,//视频码率,单位 kbps 微信小程序、App(3.1.14+)
|
|||
|
}
|
|||
|
resolve(obj)
|
|||
|
}
|
|||
|
});
|
|||
|
})
|
|||
|
}
|
|||
|
}
|
|||
|
export default videoTools;
|