get-video-info.uvue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex:1">
  4. <!-- #endif -->
  5. <page-head :title="title"></page-head>
  6. <view class="uni-padding-wrap">
  7. <view class="uni-title">
  8. <text class="uni-subtitle-text">获取本地绝对路径视频信息</text>
  9. </view>
  10. <video class="video" :src="absoluteVideoPath" :controls="true" :poster="absoluteCoverImagePath"></video>
  11. <text class="margin-top-10">{{absoluteVideoInfo}}</text>
  12. <view class="uni-btn-v">
  13. <button type="primary" @click="chooseVideo">拍摄视频或从相册中选择视频</button>
  14. </view>
  15. </view>
  16. <!-- #ifndef MP -->
  17. <view class="uni-padding-wrap">
  18. <view class="uni-title">
  19. <text class="uni-subtitle-text">获取本地相对路径视频信息</text>
  20. </view>
  21. <video class="video" :src="relativeVideoPath" :controls="true" :poster="relativeCoverImagePath"></video>
  22. <text class="margin-top-10">{{relativeVideoInfo}}</text>
  23. </view>
  24. <!-- #endif -->
  25. <!-- #ifdef APP -->
  26. </scroll-view>
  27. <!-- #endif -->
  28. </template>
  29. <script>
  30. export default {
  31. data() {
  32. return {
  33. title: "getVideoInfo",
  34. relativeVideoPath: "/static/test-video/10second-demo.mp4",
  35. relativeVideoInfo: "",
  36. relativeCoverImagePath: "",
  37. absoluteVideoPath: "",
  38. absoluteVideoInfo: "",
  39. absoluteCoverImagePath: "",
  40. // 自动化测试
  41. videoInfoForTest: null as UTSJSONObject | null
  42. }
  43. },
  44. onReady() {
  45. // #ifndef MP
  46. uni.getVideoInfo({
  47. src: this.relativeVideoPath,
  48. success: (res) => {
  49. console.log("getVideoInfo success", JSON.stringify(res));
  50. 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`;
  51. // #ifdef APP-ANDROID || APP-IOS
  52. this.relativeVideoInfo = this.relativeVideoInfo + `\n视频字节大小: ${res.byteSize}B\n视频首帧图片路径: ${res.thumbTempFilePath}`
  53. if(res.thumbTempFilePath != null) {
  54. this.relativeCoverImagePath = res.thumbTempFilePath!;
  55. }
  56. // #endif
  57. },
  58. fail: (err) => {
  59. uni.showModal({
  60. title: "获取视频信息失败",
  61. content: JSON.stringify(err),
  62. showCancel: false
  63. });
  64. }
  65. });
  66. // #endif
  67. },
  68. methods: {
  69. chooseVideo() {
  70. uni.chooseVideo({
  71. compressed: false,
  72. success: (res) => {
  73. this.absoluteVideoPath = res.tempFilePath;
  74. uni.getVideoInfo({
  75. src: res.tempFilePath,
  76. success: (_res) => {
  77. console.log("getVideoInfo success", JSON.stringify(_res));
  78. 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`;
  79. // #ifdef APP-ANDROID || APP-IOS
  80. this.absoluteVideoInfo = this.absoluteVideoInfo + `\n视频字节大小: ${_res.byteSize}B\n视频首帧图片路径: ${_res.thumbTempFilePath}`
  81. if(_res.thumbTempFilePath != null) {
  82. this.absoluteCoverImagePath = _res.thumbTempFilePath!
  83. }
  84. // #endif
  85. },
  86. fail: (err) => {
  87. uni.showModal({
  88. title: "获取视频信息失败",
  89. content: JSON.stringify(err),
  90. showCancel: false
  91. });
  92. }
  93. });
  94. }
  95. });
  96. },
  97. testGetVideoInfo() {
  98. uni.getVideoInfo({
  99. src: '/static/test-video/10second-demo.mp4',
  100. success: (res) => {
  101. this.videoInfoForTest = {
  102. "orientation": res.orientation,
  103. "type": res.type,
  104. "duration": Math.trunc(res.duration),
  105. "size": res.size,
  106. "width": res.width,
  107. "height": res.height,
  108. "fps": res.fps,
  109. "bitrate": res.bitrate
  110. };
  111. },
  112. fail: (_) => {
  113. this.videoInfoForTest = null;
  114. }
  115. });
  116. }
  117. }
  118. }
  119. </script>
  120. <style>
  121. .video {
  122. width: 100%;
  123. }
  124. .margin-top-10 {
  125. margin-top: 10px;
  126. }
  127. </style>