compress-video.uvue 5.4 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2 3 4 5 6
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
7
      <video class="video" :src="beforeCompressPath" :controls="true"></video>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
8 9 10 11
      <view class="uni-title">
        <text class="uni-subtitle-text">压缩前视频信息</text>
      </view>
      <text>{{beforeCompressVideoInfo}}</text>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
12
      <video class="video" :src="afterCompressPath" :controls="true"></video>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
13 14 15 16 17 18 19 20 21 22
      <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>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
23
      <enum-data title="压缩质量" :items="qualityItemTypes" @change="onQualityChange"></enum-data>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
24 25 26 27
      <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>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
28 29 30 31 32 33 34
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
35
  import { ItemType } from '@/components/enum-data/enum-data';
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
36 37 38 39 40 41 42 43 44 45 46 47
  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,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
48
        qualityItemTypes: [{ "value": 0, "name": "low(低)" }, { "value": 1, "name": "medium(中)" }, { "value": 2, "name": "high(高)" }] as ItemType[],
49 50
        qualityItems: ["low", "medium", "high"],
        // 自动化测试
51 52
        videoInfoForTest: null,
        videoSrcForTest: '/static/test-video/10second-demo.mp4'
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
      }
    },
    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));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
73
            this.afterCompressPath = res.tempFilePath;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
74 75 76 77 78 79 80
            uni.showToast({
              title: "压缩成功",
              icon: null
            });
            uni.getVideoInfo({
              src: res.tempFilePath,
              success: (_res) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
81
                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`;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
              }
            });
          },
          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) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
106
                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`;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
107 108 109 110 111
              }
            });
          }
        });
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
112 113
      onQualityChange(value : number) {
        this.quality = this.qualityItems[value];
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
114
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
115 116
      onResolutionChange(event : UniSliderChangeEvent) {
        this.resolution = event.detail.value;
117 118
      },
      testCompressVideo() {
119
        let beforeCompressSize: number, afterComoressSize: number;
120
        uni.compressVideo({
121
          src: this.videoSrcForTest,
122 123 124
          quality: 'medium',
          success: (res) => {
            uni.getVideoInfo({
125
              src: this.videoSrcForTest,
126
              success: (_res) => {
127 128 129 130 131 132 133 134 135 136 137 138
                beforeCompressSize = _res.size.toInt();
                uni.getVideoInfo({
                  src: res.tempFilePath,
                  success: (__res) => {
                    afterComoressSize = __res.size.toInt();
                    this.videoInfoForTest = {
                      "width": __res.width,
                      "height": __res.height,
                      // "isSizeReduce": afterComoressSize < beforeCompressSize
                    };
                  }
                });
139 140 141 142 143 144 145
              }
            });
          },
          fail: (_) => {
            this.videoInfoForTest = null;
          }
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159
      }
    }
  }
</script>

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

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