Back to Uni App

Get Video Info

docs/api/get-video-info.md

2.3.312.3 KB
Original Source

uni.getVideoInfo(options) @getVideoInfo

获取视频详细信息

getVideoInfo 兼容性

Web微信小程序AndroidiOSHarmonyOSHarmonyOS(Vapor)
4.04.414.184.254.615.0

参数

名称类型必填默认值兼容性描述
optionsGetVideoInfoOptions-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -

options 的属性描述

名称类型必备默认值兼容性描述
srcstring.VideoURIString-Web: -; 微信小程序: -; Android: 4.18; iOS: 4.25; HarmonyOS: -视频文件路径,可以是临时文件路径也可以是永久文件路径
success(callback: GetVideoInfoSuccess) => void-Web: -; 微信小程序: -; Android: 4.18; iOS: 4.25; HarmonyOS: -接口调用成功的回调函数
fail(callback: GetVideoInfoFail) => void-Web: -; 微信小程序: -; Android: 4.18; iOS: 4.25; HarmonyOS: -接口调用失败的回调函数
complete(callback: any) => void-Web: -; 微信小程序: -; Android: 4.18; iOS: 4.25; HarmonyOS: -接口调用结束的回调函数(调用成功、失败都会执行)

GetVideoInfoSuccess 的属性值 @getvideoinfosuccess-values

名称类型必备默认值兼容性描述
orientationstring-Web: x; 微信小程序: -; Android: 4.18; iOS: 4.25; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0画面方向
typestring-Web: x; 微信小程序: 4.41; Android: 4.18; iOS: 4.25; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0视频格式
durationnumber-Web: -; 微信小程序: -; Android: 4.18; iOS: 4.25; HarmonyOS: -视频长度
sizenumber-Web: -; 微信小程序: -; Android: 4.18; iOS: 4.25; HarmonyOS: -视频大小,单位 kB
heightnumber-Web: -; 微信小程序: -; Android: 4.18; iOS: 4.25; HarmonyOS: -视频的长,单位 px
widthnumber-Web: -; 微信小程序: -; Android: 4.18; iOS: 4.25; HarmonyOS: -视频的宽,单位 px
fpsnumber-Web: x; 微信小程序: 4.41; Android: 4.18; iOS: 4.25; HarmonyOS: x视频帧率
bitratenumber-Web: x; 微信小程序: 4.41; Android: 4.18; iOS: 4.25; HarmonyOS: x视频码率,单位 kbps
thumbTempFilePathstring-Web: x; 微信小程序: 4.41; Android: 4.61; iOS: 4.61; HarmonyOS: x视频缩略图临时文件路径
byteSizenumber-Web: x; 微信小程序: 4.41; Android: 4.61; iOS: 4.61; HarmonyOS: x视频文件的字节大小

orientation 的属性描述

合法值兼容性描述
upWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
downWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
leftWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
rightWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
up-mirroredWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
down-mirroredWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
left-mirroredWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
right-mirroredWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --

GetVideoInfoFail 的属性值 @getvideoinfofail-values

名称类型必备默认值兼容性描述
errCodenumber-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -错误码
errSubjectstring-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -统一错误主题(模块)名称
dataany-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -错误信息中包含的数据
causeError-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -源错误信息,可以包含多个错误,详见SourceError
errMsgstring-Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -

errCode 的属性描述

合法值兼容性描述
1101001Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -用户取消
1101002Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -urls至少包含一张图片地址
1101003Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -文件不存在
1101004Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -图片加载失败
1101005Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -未获取权限
1101006Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -图片或视频保存失败
1101007Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -图片裁剪失败
1101008Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -拍照或录像失败
1101009Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -图片压缩失败
1101010Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: -其他错误

参见

示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/get-video-info/get-video-info

appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/get-video-info/get-video-info

示例

vue
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <view class="uni-title">
        <text class="uni-subtitle-text">获取本地绝对路径视频信息</text>
      </view>
      <video class="video" :src="absoluteVideoPath" :controls="true" :poster="absoluteCoverImagePath"></video>
      <text class="margin-top-10">{{absoluteVideoInfo}}</text>
      <view class="uni-btn-v">
        <button type="primary" @click="chooseVideo">拍摄视频或从相册中选择视频</button>
      </view>
    </view>
    <!-- #ifndef MP -->
    <view class="uni-padding-wrap">
      <view class="uni-title">
        <text class="uni-subtitle-text">获取本地相对路径视频信息</text>
      </view>
      <video class="video" :src="relativeVideoPath" :controls="true" :poster="relativeCoverImagePath"></video>
      <text class="margin-top-10">{{relativeVideoInfo}}</text>
    </view>
    <!-- #endif -->
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script setup lang="uts">
  type TestStateType = {
    videoInfoForTest: UTSJSONObject | null
  }

  const title = ref("getVideoInfo")
  const relativeVideoPath = ref("/static/test-video/10second-demo.mp4")
  const relativeVideoInfo = ref("")
  const relativeCoverImagePath = ref("")
  const absoluteVideoPath = ref("")
  const absoluteVideoInfo = ref("")
  const absoluteCoverImagePath = ref("")
  // 自动化测试
  const testState = reactive({
    videoInfoForTest: null as UTSJSONObject | null
  } as TestStateType)
  const needLoadOnReady = ref(true)

  // #ifdef APP-ANDROID
  onLoad((event : OnLoadOptions)=>{
    needLoadOnReady.value = (event["is_debug"] ?? "1") == "1";
  })
  // #endif

  onReady(() => {
    // #ifndef MP
    if(!needLoadOnReady.value) return;
    uni.getVideoInfo({
      src: relativeVideoPath.value,
      success: (res) => {
        console.log("getVideoInfo success", JSON.stringify(res));
        relativeVideoInfo.value = `视频画面方向: ${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`;
        // #ifdef APP-ANDROID || APP-IOS
        relativeVideoInfo.value = relativeVideoInfo.value + `\n视频字节大小: ${res.byteSize}B\n视频首帧图片路径: ${res.thumbTempFilePath}`
        if(res.thumbTempFilePath != null) {
        relativeCoverImagePath.value = res.thumbTempFilePath!;
        }
        // #endif

      },
      fail: (err) => {
        uni.showModal({
          title: "获取视频信息失败",
          content: JSON.stringify(err),
          showCancel: false
        });
      }
    });
    // #endif
  })

  const chooseVideo = () => {
    uni.chooseVideo({
      compressed: false,
      success: (res) => {
        absoluteVideoPath.value = res.tempFilePath;
        uni.getVideoInfo({
          src: res.tempFilePath,
          success: (_res) => {
            console.log("getVideoInfo success", JSON.stringify(_res));
            absoluteVideoInfo.value = `视频画面方向: ${_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`;
            // #ifdef APP-ANDROID || APP-IOS
            absoluteVideoInfo.value = absoluteVideoInfo.value + `\n视频字节大小: ${_res.byteSize}B\n视频首帧图片路径: ${_res.thumbTempFilePath}`
            if(_res.thumbTempFilePath != null) {
            absoluteCoverImagePath.value = _res.thumbTempFilePath!
            }
            // #endif
          },
          fail: (err) => {
            uni.showModal({
              title: "获取视频信息失败",
              content: JSON.stringify(err),
              showCancel: false
            });
          }
        });
      }
    });
  }

  const testGetVideoInfo = () => {
    uni.getVideoInfo({
      src: '/static/test-video/10second-demo.mp4',
      success: (res) => {
        testState.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: (_) => {
        testState.videoInfoForTest = null;
      }
    });
  }
  defineExpose({
    testState,
    testGetVideoInfo
  })
</script>

<style>
  .video {
    width: 100%;
  }

  .margin-top-10 {
    margin-top: 10px;
  }
</style>

:::

通用类型

GeneralCallbackResult

名称类型必备默认值兼容性描述
errMsgstring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -错误信息

Tips

  • 从HBuilderX4.61版起,GetVideoInfoSuccess中的duration、size精度统一调整为小数点后3位数