Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

mc.compressVideo(Object object)

压缩视频接口。开发者可指定压缩质量 quality 进行压缩。当需要更精细的控制时,可指定 bitrate、fps、和 resolution,当 quality 传入时,这三个参数将被忽略。

参数

Object object

属性类型默认值必填说明
srcString是视频文件路径,可以是临时文件路径也可以是永久文件路径
qualityString是压缩质量
bitrateNumber是码率,单位 kbps
fpsNumber是帧率
resolutionNumber是相对于原视频的分辨率比例,取值范围(0, 1]
successFunction否接口调用成功的回调函数
failFunction否接口调用失败的回调函数
completeFunction否接口调用结束的回调函数(调用成功、失败都会执行)

Object.quality 的合法值

值说明最低版本
low低
medium中
high高 

Object.success(Object res) 回调函数

参数

Object res

属性类型说明
tempFilePathString压缩后的临时文件地址
sizeString压缩后的大小,单位 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)
            })
          }
        })
      }
    })
  }