# uni.compressVideo(options)

压缩视频

# 参数

名称 类型 必填 默认值 兼容性 描述
options CompressVideoOptions - - -
名称 类型 必备 默认值 兼容性 描述
src string.VideoURIString - - 视频文件路径,可以是临时文件路径也可以是永久文件路径
quality string - - 压缩质量
bitrate number -
码率,单位 kbps
fps number -
帧率
resolution number - - 相对于原视频的分辨率比例,取值范围(0, 1]
success (callback: CompressVideoSuccess) => void - - 接口调用成功的回调函数
fail (callback: IMediaError) => void - - 接口调用失败的回调函数
complete (callback: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# CompressVideoSuccess 的属性值

名称 类型 必备 默认值 兼容性 描述
tempFilePath string - - 压缩后的临时文件地址
size number - - 压缩后的大小,单位 kB

# IMediaError 的属性值

名称 类型 必备 默认值 兼容性 描述
errCode number - - 错误码
合法值 兼容性 描述
1101001 - 用户取消
1101002 - urls至少包含一张图片地址
1101003 - 文件不存在
1101004 - 图片加载失败
1101005 - 未获取权限
1101006 - 图片或视频保存失败
1101007 - 图片裁剪失败
1101008 - 拍照或录像失败
1101009 - 图片压缩失败
1101010 - 其他错误
errSubject string - - 统一错误主题(模块)名称
data any - - 错误信息中包含的数据
cause Error - - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - - -

# compressVideo 兼容性

Web Android iOS
x 4.18 4.25

# 参见

# 示例

hello uni-app x

该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验

扫码体验(手机浏览器跳转到App直达页)
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view>
      <view class="uni-padding-wrap">
        <video class="video" :src="beforeCompressPath" :controls="true"></video>
        <view class="uni-title">
          <text class="uni-subtitle-text">压缩前视频信息</text>
        </view>
        <text>{{beforeCompressVideoInfo}}</text>
        <video class="video" :src="afterCompressPath" :controls="true"></video>
        <view class="uni-title">
          <text class="uni-subtitle-text">压缩后视频信息</text>
        </view>
        <text>{{afterCompressVideoInfo}}</text>
        <view class="uni-btn-v">
          <button type="primary" @click="chooseVideo">从相册中选取待压缩的视频</button>
        </view>
        <view class="uni-btn-v">
          <button type="primary" @click="compressVideo">压缩视频</button>
        </view>
        <enum-data title="压缩质量" :items="qualityItemTypes" @change="onQualityChange"></enum-data>
        <view class="uni-common-mt">
          <text class="uni-title uni-title-text">相对于原视频的分辨率比例,取值范围(0, 1]</text>
          <slider :min="0.1" :max="1" :step="0.1" :show-value="true" @change="onResolutionChange"></slider>
        </view>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  import { ItemType } from '@/components/enum-data/enum-data-types';
  export default {
    data() {
      return {
        title: "compressVideo",
        beforeCompressVideoInfo: "",
        afterCompressVideoInfo: "",
        beforeCompressPath: "",
        afterCompressPath: "",
        quality: null as string | null,
        bitrate: null as number | null,
        fps: null as number | null,
        resolution: null as number | null,
        qualityItemTypes: [{ "value": 0, "name": "low(低)" }, { "value": 1, "name": "medium(中)" }, { "value": 2, "name": "high(高)" }] as ItemType[],
        qualityItems: ["low", "medium", "high"],
        // 自动化测试
        videoInfoForTest: null,
        videoSrcForTest: '/static/test-video/10second-demo.mp4'
      }
    },
    methods: {
      compressVideo() {
        if (this.beforeCompressPath == "") {
          uni.showToast({
            title: "请先选择视频",
            icon: "error"
          });
          return;
        }
        uni.showLoading({
          title: "视频压缩中"
        });
        uni.compressVideo({
          src: this.beforeCompressPath,
          quality: this.quality,
          resolution: this.resolution,
          success: (res) => {
            console.log("compressVideo success", JSON.stringify(res));
            this.afterCompressPath = res.tempFilePath;
            uni.showToast({
              title: "压缩成功",
              icon: null
            });
            uni.getVideoInfo({
              src: res.tempFilePath,
              success: (_res) => {
                this.afterCompressVideoInfo = `视频画面方向: ${_res.orientation}\n视频格式: ${_res.type}\n视频长度: ${_res.duration}s\n视频大小: ${_res.size}KB\n视频宽度: ${_res.width}\n视频高度: ${_res.height}\n视频帧率: ${_res.fps}fps\n视频码率: ${_res.bitrate}kbps`;
              }
            });
          },
          fail: (err) => {
            uni.showModal({
              title: "压缩视频失败",
              content: JSON.stringify(err),
              showCancel: false
            });
          },
          complete: (_) => {
            uni.hideLoading();
          }
        });
      },
      chooseVideo() {
        uni.chooseVideo({
          sourceType: ["album"],
          compressed: false,
          success: (res) => {
            this.beforeCompressPath = res.tempFilePath;
            uni.getVideoInfo({
              src: res.tempFilePath,
              success: (_res) => {
                this.beforeCompressVideoInfo = `视频画面方向: ${_res.orientation}\n视频格式: ${_res.type}\n视频长度: ${_res.duration}s\n视频大小: ${_res.size}KB\n视频宽度: ${_res.width}\n视频高度: ${_res.height}\n视频帧率: ${_res.fps}fps\n视频码率: ${_res.bitrate}kbps`;
              }
            });
          }
        });
      },
      onQualityChange(value : number) {
        this.quality = this.qualityItems[value];
      },
      onResolutionChange(event : UniSliderChangeEvent) {
        this.resolution = event.detail.value;
      },
      testCompressVideo() {
        let beforeCompressSize : number, afterComoressSize : number;
        uni.compressVideo({
          src: this.videoSrcForTest,
          quality: 'medium',
          success: (res) => {
            uni.getVideoInfo({
              src: this.videoSrcForTest,
              success: (_res) => {
                beforeCompressSize = Math.trunc(_res.size);
                uni.getVideoInfo({
                  src: res.tempFilePath,
                  success: (__res) => {
                    afterComoressSize = Math.trunc(__res.size);
                    this.videoInfoForTest = {
                      "width": __res.width,
                      "height": __res.height,
                      "isSizeReduce": afterComoressSize < beforeCompressSize
                    };
                  }
                });
              }
            });
          },
          fail: (_) => {
            this.videoInfoForTest = null;
          }
        });
      }
    }
  }
</script>

<style>
  .video {
    align-self: center;
  }

  .image-container {
    flex-direction: row;
  }
</style>

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string - - 错误信息