mc.compressVideo(Object object)
压缩视频接口。开发者可指定压缩质量 quality
进行压缩。当需要更精细的控制时,可指定 bitrate
、fps
、和 resolution
,当 quality
传入时,这三个参数将被忽略。
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
src | String | 是 | 视频文件路径,可以是临时文件路径也可以是永久文件路径 | |
quality | String | 是 | 压缩质量 | |
bitrate | Number | 是 | 码率,单位 kbps | |
fps | Number | 是 | 帧率 | |
resolution | Number | 是 | 相对于原视频的分辨率比例,取值范围(0, 1] | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
Object.quality 的合法值
值 | 说明 | 最低版本 |
---|---|---|
low | 低 | |
medium | 中 | |
high | 高 |
Object.success(Object res) 回调函数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
tempFilePath | String | 压缩后的临时文件地址 |
size | String | 压缩后的大小,单位 kB |
示例代码:
<view class="cssname">mc.compressVideo(Object object)</view>
<form bindsubmit="compressVideo">
<button form-type="submit">压缩视频</button>
<radio-group name="quality">
<radio value="low" checked="true">low</radio>
<radio value="medium">medium</radio>
<radio value="high">high</radio>
</radio-group>
</form>
<text space="nbsp" user-select="true" class="scrollDetail">{{compressVideoInfo}}</text>
<text style="color:blue;">压缩前尺寸: {{oldVideoRatio}} \n格式: {{oldVideoType}} \n方向: {{oldVideoOrientation}} \n大小: {{oldVideoSize}} \n时长: {{oldVideoDuration}} \nfps: {{oldVideoFPS}} \nbitrate: {{oldVideoBitRate}}</text>
<text style="color:red;">压缩后尺寸: {{newVideoRatio}} \n格式: {{newVideoType}} \n方向: {{newVideoOrientation}} \n大小: {{newVideoSize}} \n时长: {{newVideoDuration}} \nfps: {{newVideoFPS}} \nbitrate: {{newVideoBitRate}}</text>
<video id="video" enable-danmu="{{true}}" picture-in-picture-mode="{{['push', 'pop']}}" src="{{videoSrc}}" style="width:100%;"></video>
</view>
compressVideo: function(e){
var _this = this;
var quality = e.detail.value.quality;
mc.chooseVideo({
sourceType: ['album', 'camera'],
compressed: true,
maxDuration: 60,
camera: 'front',
compressed:false,
success(res) {
mc.showLoading({
title: '正在压缩中',
})
mc.getVideoInfo({
src: res.tempFilePath,
success: function(res){
_this.setData({
oldVideoRatio: res.width + '*' + res.height,
oldVideoType: res.type,
oldVideoOrientation: res.orientation,
oldVideoSize: res.size + 'KB',
oldVideoDuration: res.duration,
oldVideoFPS: res.fps,
oldVideoBitRate: res.bitrate
})
}
})
mc.compressVideo({
quality: quality,
bitrate: 0,
fps: 0,
resolution: 0,
src: res.tempFilePath,
success: function(res){
_this.setData({
videoSrc: res.tempFilePath,
})
mc.getVideoInfo({
src: res.tempFilePath,
success: function(res){
_this.setData({
newVideoRatio: res.width + '*' + res.height,
newVideoType: res.type,
newVideoOrientation: res.orientation,
newVideoSize: res.size + 'KB',
newVideoDuration: res.duration,
newVideoFPS: res.fps,
newVideoBitRate: res.bitrate
})
}
})
},
complete: function(res){
mc.hideLoading();
_this.setData({
compressVideoInfo: JSON.stringify(res, null, 2)
})
}
})
}
})
}