# uni.getVideoInfo(options)

获取视频详细信息

# getVideoInfo 兼容性

Web 微信小程序 Android iOS
4.0 4.41 4.18 4.25

# 参数

名称 类型 必填 默认值 兼容性 描述
options GetVideoInfoOptions - - -
名称 类型 必备 默认值 兼容性 描述
src string.VideoURIString -
视频文件路径,可以是临时文件路径也可以是永久文件路径
success (callback: GetVideoInfoSuccess) => void -
接口调用成功的回调函数
fail (callback: IMediaError) => void -
接口调用失败的回调函数
complete (callback: any) => void -
接口调用结束的回调函数(调用成功、失败都会执行)

# GetVideoInfoSuccess 的属性值

名称 类型 必备 默认值 兼容性 描述
orientation string -
画面方向
合法值 兼容性 描述
up - -
down - -
left - -
right - -
up-mirrored - -
down-mirrored - -
left-mirrored - -
right-mirrored - -
type string -
视频格式
duration number -
视频长度
size number -
视频大小,单位 kB
height number -
视频的长,单位 px
width number -
视频的宽,单位 px
fps number -
视频帧率
bitrate number -
视频码率,单位 kbps

# IMediaError 的属性值

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

# 参见

# 示例

hello uni-app x

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

<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"></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"></video>
      <text class="margin-top-10">{{relativeVideoInfo}}</text>
    </view>
    <!-- #endif -->
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        title: "getVideoInfo",
        relativeVideoPath: "/static/test-video/10second-demo.mp4",
        relativeVideoInfo: "",
        absoluteVideoPath: "",
        absoluteVideoInfo: "",
        // 自动化测试
        videoInfoForTest: null as UTSJSONObject | null
      }
    },
    onReady() {
      // #ifndef MP
      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
          });
        }
      });
      // #endif
    },
    methods: {
      chooseVideo() {
        uni.chooseVideo({
          compressed: false,
          success: (res) => {
            this.absoluteVideoPath = res.tempFilePath;
            uni.getVideoInfo({
              src: res.tempFilePath,
              success: (_res) => {
                console.log("getVideoInfo success", JSON.stringify(_res));
                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;
  }
</style>

# 通用类型

# GeneralCallbackResult

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