get-video-info.uvue 3.7 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
6 7 8 9 10 11 12
    <view class="uni-padding-wrap">
      <view class="uni-title">
        <text class="uni-subtitle-text">获取本地相对路径视频信息</text>
      </view>
      <video class="video" :src="relativeVideoPath" :controls="true"></video>
      <text class="margin-top-10">{{relativeVideoInfo}}</text>
    </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    <view class="uni-padding-wrap">
      <view class="uni-title">
        <text class="uni-subtitle-text">获取本地绝对路径视频信息</text>
      </view>
      <video class="video" :src="absoluteVideoPath" :controls="true"></video>
      <text class="margin-top-10">{{absoluteVideoInfo}}</text>
      <view class="uni-btn-v">
        <button type="primary" @click="chooseVideo">拍摄视频或从相册中选择视频</button>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        title: "getVideoInfo",
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
33 34
        relativeVideoPath: "/static/test-video/10second-demo.mp4",
        relativeVideoInfo: "",
DCloud-WZF's avatar
DCloud-WZF 已提交
35 36 37 38 39 40
        absoluteVideoPath: "",
        absoluteVideoInfo: "",
        // 自动化测试
        videoInfoForTest: null as UTSJSONObject | null
      }
    },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    onReady() {
      uni.getVideoInfo({
        src: this.relativeVideoPath,
        success: (res) => {
          console.log("getVideoInfo success", JSON.stringify(res));
          this.relativeVideoInfo = `视频画面方向: ${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
          });
        }
      });
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
57 58 59 60 61 62 63 64 65
    methods: {
      chooseVideo() {
        uni.chooseVideo({
          compressed: false,
          success: (res) => {
            this.absoluteVideoPath = res.tempFilePath;
            uni.getVideoInfo({
              src: res.tempFilePath,
              success: (_res) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
66
                console.log("getVideoInfo success", JSON.stringify(_res));
DCloud-WZF's avatar
DCloud-WZF 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                this.absoluteVideoInfo = `视频画面方向: ${_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
                });
              }
            });
          }
        });
      },
      testGetVideoInfo() {
        uni.getVideoInfo({
          src: '/static/test-video/10second-demo.mp4',
          success: (res) => {
            this.videoInfoForTest = {
              "orientation": res.orientation,
              "type": res.type,
              "duration": Math.trunc(res.duration),
              "size": res.size,
              "width": res.width,
              "height": res.height,
              "fps": res.fps,
              "bitrate": res.bitrate
            };
          },
          fail: (_) => {
            this.videoInfoForTest = null;
          }
        });
      }
    }
  }
</script>

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

  .margin-top-10 {
    margin-top: 10px;
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
112
</style>