index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <div class="wrap">
  3. <h1>视频帧截图(canvas)</h1>
  4. <n-button
  5. :loading="loading"
  6. type="primary"
  7. @click.stop="handleVideoFrame"
  8. >
  9. 选择视频
  10. <input
  11. ref="uploadRef"
  12. type="file"
  13. class="input-upload"
  14. @change="uploadChange"
  15. />
  16. </n-button>
  17. <span>
  18. 进度:{{
  19. currentDuation ? ((currentDuation / videoDuration) * 100).toFixed() : 0
  20. }}%
  21. </span>
  22. <n-button
  23. v-if="currentDuation && currentDuation - videoDuration === 0"
  24. type="success"
  25. @click="handleDownload"
  26. >
  27. 下载
  28. </n-button>
  29. <div
  30. ref="listRef"
  31. class="frame-list"
  32. :style="{ height: height + 'px' }"
  33. >
  34. <div
  35. v-for="(item, index) in imgList"
  36. :key="index"
  37. class="item"
  38. >
  39. <img ref="imgListRef" />
  40. <div class="time">{{ item }}</div>
  41. </div>
  42. </div>
  43. </div>
  44. </template>
  45. <script lang="ts" setup>
  46. import JSZip from 'jszip';
  47. import { nextTick, onMounted, ref } from 'vue';
  48. import { createVideo, formatDownTime2, generateBase64 } from '@/utils';
  49. const uploadRef = ref<HTMLInputElement>();
  50. const loading = ref(false);
  51. const currentDuation = ref(0);
  52. const videoDuration = ref(0);
  53. const height = ref(0);
  54. const fileList = ref<{ name: string; data: string }[]>([]);
  55. const imgList = ref<any[]>([]);
  56. const imgListRef = ref<HTMLImageElement[]>([]);
  57. const listRef = ref<HTMLDivElement>();
  58. function handleDownload() {
  59. // 初始化一个zip打包对象
  60. const zip = new JSZip();
  61. // 创建一个被用来打包的名为Hello.txt的文件
  62. fileList.value.forEach((file) => {
  63. zip.file(file.name, file.data, { base64: true });
  64. });
  65. // 把打包内容异步转成blob二进制格式
  66. zip.generateAsync({ type: 'blob' }).then(function (content) {
  67. // 创建隐藏的可下载链接
  68. const eleLink = document.createElement('a');
  69. eleLink.download = '视频帧截图.zip';
  70. eleLink.style.display = 'none';
  71. // 下载内容转变成blob地址
  72. eleLink.href = URL.createObjectURL(content);
  73. // 触发点击
  74. document.body.appendChild(eleLink);
  75. eleLink.click();
  76. // 然后移除
  77. document.body.removeChild(eleLink);
  78. });
  79. }
  80. function uploadChange() {
  81. if (loading.value) return;
  82. loading.value = true;
  83. imgList.value = [];
  84. currentDuation.value = 0;
  85. videoDuration.value = 0;
  86. nextTick(() => {
  87. const file = uploadRef.value?.files?.[0];
  88. if (!file) return;
  89. const url = URL.createObjectURL(file);
  90. const videoEl = createVideo({
  91. appendChild: false,
  92. });
  93. videoEl.src = url;
  94. let currentTime = 0;
  95. function captureFrame() {
  96. const res = formatDownTime2({
  97. startTime: +new Date(),
  98. endTime: +new Date() + (currentTime + 1) * 1000,
  99. addZero: true,
  100. });
  101. let time = '';
  102. if (res.d) {
  103. time = `${res.d}天${res.h}:${res.m}:${res.s}`;
  104. } else {
  105. time = `${res.h}:${res.m}:${res.s}`;
  106. }
  107. imgList.value.push(time);
  108. nextTick(() => {
  109. // 确保视频已足够加载以获取当前帧
  110. const img = imgListRef.value[imgListRef.value.length - 1];
  111. if (img) {
  112. const str = generateBase64(videoEl);
  113. img.src = str;
  114. fileList.value.push({
  115. name: `${currentTime}.webp`,
  116. data: str.split(';base64,')[1],
  117. });
  118. currentDuation.value = currentDuation.value + 1;
  119. if (videoDuration.value > currentTime) {
  120. // 移动到下一帧
  121. videoEl.currentTime += 1;
  122. currentTime += 1;
  123. }
  124. }
  125. });
  126. }
  127. videoEl.onloadeddata = () => {
  128. currentTime = videoEl.currentTime;
  129. videoDuration.value = Math.ceil(videoEl.duration);
  130. captureFrame();
  131. };
  132. videoEl.onseeked = () => {
  133. if (currentTime < videoDuration.value) {
  134. captureFrame();
  135. } else {
  136. loading.value = false;
  137. if (uploadRef.value) {
  138. uploadRef.value.value = '';
  139. }
  140. }
  141. };
  142. });
  143. }
  144. function handleVideoFrame() {
  145. uploadRef.value?.click();
  146. }
  147. function getHeight() {
  148. const h =
  149. document.documentElement.clientHeight -
  150. (listRef.value?.getBoundingClientRect().top || 0);
  151. height.value = h;
  152. }
  153. onMounted(() => {
  154. getHeight();
  155. });
  156. </script>
  157. <style lang="scss" scoped>
  158. .wrap {
  159. padding-top: 10px;
  160. padding-left: 30px;
  161. .input-upload {
  162. width: 0;
  163. height: 0;
  164. opacity: 0;
  165. }
  166. .frame-list {
  167. display: flex;
  168. overflow: scroll;
  169. align-content: baseline;
  170. flex-wrap: wrap;
  171. margin-top: 10px;
  172. @extend %customScrollbar;
  173. .item {
  174. position: relative;
  175. margin-right: 10px;
  176. margin-bottom: 10px;
  177. padding: 3px;
  178. width: 200px;
  179. height: fit-content;
  180. border: 1px solid black;
  181. border-radius: 5px;
  182. .time {
  183. position: absolute;
  184. right: 3px;
  185. bottom: 3px;
  186. padding: 3px 4px;
  187. border-radius: 3px;
  188. background-color: rgba($color: #000000, $alpha: 0.5);
  189. color: white;
  190. font-size: 13px;
  191. }
  192. img {
  193. width: 100%;
  194. height: 100%;
  195. }
  196. }
  197. }
  198. }
  199. </style>