index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <template>
  2. <div>
  3. <el-form-item label="视频上传">
  4. <div class="upload_video" id="upload_video">
  5. <el-upload
  6. class="upload-demo"
  7. ref="upload"
  8. action="#"
  9. :http-request="uploadVideoToTxPcdn"
  10. accept=".mp4"
  11. :limit="1"
  12. :on-remove="handleRemove"
  13. :on-change="handleChange"
  14. :auto-upload="false"
  15. :key="uploadKey"
  16. >
  17. <el-button slot="trigger" size="small" type="primary" >本地选取</el-button>
  18. <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">点击上传</el-button>
  19. <!-- 仅当showControl为true时显示视频库选取按钮 -->
  20. <el-button v-if="showControl" style="margin-left: 10px;" size="small" type="warning" @click="openVideoLibrary">素材库选取</el-button>
  21. <!-- 线路一 -->
  22. <div class="progress-container">
  23. <span class="progress-label">上传进度</span>
  24. <el-progress
  25. style="margin-top: 5px;"
  26. class="progress"
  27. :text-inside="true"
  28. :stroke-width="18"
  29. :percentage="txProgress"
  30. status="success">
  31. </el-progress>
  32. </div>
  33. <!-- 线路二 -->
  34. <div class="progress-container">
  35. <span class="progress-label">线路二</span>
  36. <el-progress
  37. style="margin-top: 5px;"
  38. class="progress"
  39. :text-inside="true"
  40. :stroke-width="18"
  41. :percentage="hwProgress"
  42. status="success">
  43. </el-progress>
  44. </div>
  45. <div v-if="uploadLoading" class="el-upload__tip" style="color:#E6A23C">处理中,请稍候…</div>
  46. <div slot="tip" class="el-upload__tip">支持本地上传 mp4(≤500M)或从素材库选取</div>
  47. </el-upload>
  48. </div>
  49. </el-form-item>
  50. <el-form-item label="视频播放">
  51. <video v-if="videoUrl" ref="myvideo" :src="videoUrl" id="video" width="100%" height="300px" controls></video>
  52. <div v-if="fileName">视频文件名: {{ fileName }}</div>
  53. <div v-if="fileKey">文件Key: {{ fileKey }}</div>
  54. <div v-if="fileSize">文件大小(MB): {{ (fileSize / (1024 * 1024)).toFixed(2) }} MB</div>
  55. </el-form-item>
  56. <!-- 仅当showControl为true时显示播放线路选择器 -->
  57. <el-form-item v-if="showControl" label="播放线路">
  58. <el-radio-group v-model="localUploadType">
  59. <el-radio :label="1" >线路一</el-radio>
  60. <el-radio :label="2" >线路二</el-radio>
  61. <!-- <el-radio :label="3" >线路三</el-radio>-->
  62. </el-radio-group>
  63. </el-form-item>
  64. <!-- 视频库选择对话框 -->
  65. <el-dialog title="视频库选择" :visible.sync="libraryOpen" width="900px" append-to-body>
  66. <!-- 搜索条件 -->
  67. <el-form :inline="true" :model="libraryQueryParams" class="library-search">
  68. <el-form-item label="素材名称">
  69. <el-input
  70. v-model="libraryQueryParams.resourceName"
  71. placeholder="请输入素材名称"
  72. clearable
  73. size="small"
  74. @keyup.enter.native="handleLibraryQuery"
  75. />
  76. </el-form-item>
  77. <el-form-item>
  78. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleLibraryQuery">搜索</el-button>
  79. <el-button icon="el-icon-refresh" size="mini" @click="resetLibraryQuery">重置</el-button>
  80. </el-form-item>
  81. </el-form>
  82. <!-- 视频列表 -->
  83. <el-table v-loading="libraryLoading" :data="libraryList" @row-click="handleLibrarySelect" highlight-current-row>
  84. <el-table-column label="素材名称" align="center" prop="resourceName" />
  85. <el-table-column label="文件名称" align="center" prop="fileName" />
  86. <el-table-column label="缩略图" align="center">
  87. <template slot-scope="scope">
  88. <el-popover
  89. placement="right"
  90. title=""
  91. trigger="hover"
  92. >
  93. <img alt="" slot="reference" :src="scope.row.thumbnail" style="width: 80px; height: 50px" />
  94. <img alt="" :src="scope.row.thumbnail" style="max-width: 150px;" />
  95. </el-popover>
  96. </template>
  97. </el-table-column>
  98. <el-table-column label="视频时长" align="center">
  99. <template slot-scope="scope">
  100. <span>{{ formatDuration(scope.row.duration) }}</span>
  101. </template>
  102. </el-table-column>
  103. </el-table>
  104. <!-- 分页 -->
  105. <pagination
  106. v-show="libraryTotal>0"
  107. :total="libraryTotal"
  108. :page.sync="libraryQueryParams.pageNum"
  109. :limit.sync="libraryQueryParams.pageSize"
  110. @pagination="getLibraryList"
  111. />
  112. <div slot="footer" class="dialog-footer">
  113. <el-button type="primary" @click="confirmVideoSelection">确 定</el-button>
  114. <el-button @click="cancelVideoSelection">取 消</el-button>
  115. </div>
  116. </el-dialog>
  117. </div>
  118. </template>
  119. <script>
  120. import {getSignature, uploadHuaWeiObs, uploadHuaWeiVod} from "@/api/common";
  121. // import {getThumbnail} from "@/api/course/userVideo";
  122. import TcVod from "vod-js-sdk-v6";
  123. import { uploadObject } from "@/utils/cos.js";
  124. import { uploadToOBS } from "@/utils/obs.js";
  125. import Pagination from "@/components/Pagination";
  126. // import { listVideoResource } from '@/api/course/videoResource';
  127. export default {
  128. components: {
  129. Pagination
  130. },
  131. props: {
  132. videoUrl: {
  133. type: String,
  134. default: "",
  135. },
  136. fileKey: {
  137. type: String,
  138. default: "",
  139. },
  140. fileSize: {
  141. type: Number,
  142. default: null,
  143. },
  144. fileName: {
  145. type: String,
  146. default: "",
  147. },
  148. line_1: {
  149. type: String,
  150. default: "",
  151. },
  152. line_2: {
  153. type: String,
  154. default: "",
  155. },
  156. line_3: {
  157. type: String,
  158. default: "",
  159. },
  160. thumbnail: {
  161. type: String,
  162. default: "",
  163. },
  164. uploadType: {
  165. type: Number,
  166. default: null,
  167. },
  168. type: {
  169. type: Number,
  170. default: null,
  171. },
  172. isPrivate: {
  173. type: Number,
  174. default: 0,
  175. },
  176. // 使用一个变量控制显示,默认为true显示所有控制项
  177. showControl: {
  178. type: Boolean,
  179. default: true,
  180. }
  181. },
  182. data() {
  183. return {
  184. videoName:'',
  185. isHidden : false,
  186. localUploadType: this.uploadType,
  187. duration: 0,
  188. fileId: "",
  189. uploadTypeOptions: [
  190. { dictLabel: "线路一", dictValue: 1 }, // 腾讯pcdn
  191. { dictLabel: "线路二", dictValue: 2 }, // 华为云obs
  192. // { dictLabel: "华为云VOD", dictValue: 4 },
  193. // { dictLabel: "腾讯云VOD", dictValue: 5 },
  194. ],
  195. fileList: [],
  196. txProgress: 0,
  197. hwProgress: 0,
  198. uploadLoading: false,
  199. uploadKey: 0,
  200. // 视频库选择相关数据
  201. libraryOpen: false,
  202. libraryLoading: false,
  203. libraryTotal: 0,
  204. libraryList: [],
  205. selectedVideo: null,
  206. libraryQueryParams: {
  207. pageNum: 1,
  208. pageSize: 10,
  209. resourceName: null
  210. }
  211. };
  212. },
  213. watch: {
  214. localUploadType(newType) {
  215. this.$emit("update:uploadType", newType);
  216. this.$emit("change");
  217. },
  218. uploadType(newType) {
  219. this.localUploadType = newType;
  220. },
  221. },
  222. methods: {
  223. // 打开视频库对话框
  224. openVideoLibrary() {
  225. this.libraryOpen = true;
  226. this.selectedVideo = null;
  227. this.getLibraryList();
  228. },
  229. handleChange(file, fileList) {
  230. this.fileList = fileList;
  231. this.getVideoDuration(file.raw);
  232. },
  233. getVideoDuration(file) {
  234. const video = document.createElement("video");
  235. video.preload = "metadata";
  236. video.onloadedmetadata = () => {
  237. window.URL.revokeObjectURL(video.src);
  238. this.duration = parseInt(video.duration.toFixed(2));
  239. this.$emit("video-duration", this.duration);
  240. this.$emit("update:fileSize", file.size);
  241. };
  242. video.src = URL.createObjectURL(file);
  243. },
  244. async submitUpload() {
  245. if (this.fileList.length < 1) {
  246. return this.$message.error("请先选取视频,再进行上传");
  247. }
  248. this.uploadLoading = true;
  249. this.txProgress = 0;
  250. try {
  251. await this.getFirstThumbnail();
  252. await this.uploadVideoToTxPcdn();
  253. try {
  254. await this.uploadVideoToHwObs();
  255. } catch (e) {
  256. console.warn('线路二上传失败', e);
  257. }
  258. this.$emit("update:fileName", this.fileList[0].name);
  259. this.$emit("video-ready");
  260. } finally {
  261. this.uploadLoading = false;
  262. }
  263. },
  264. //获取第一帧封面
  265. async getFirstThumbnail(){
  266. const file = this.fileList[0].raw;
  267. // getThumbnail(file).then(response => {
  268. // console.log("获取到第一帧为封面======>",response.url)
  269. // this.$emit("update:thumbnail", response.url);
  270. // })
  271. },
  272. //更新华为线路进度条
  273. updateHwProgress(progress) {
  274. this.hwProgress = progress;
  275. },
  276. //更新腾讯线路进度条
  277. updateTxProgress(progressData) {
  278. this.txProgress = Math.round(progressData.percent * 100);
  279. },
  280. //上传腾讯云Pcdn
  281. async uploadVideoToTxPcdn() {
  282. try {
  283. const file = this.fileList[0].raw;
  284. const data = await uploadObject(file, this.updateTxProgress,this.type);
  285. let line_1='' ;
  286. if (this.isPrivate===0){
  287. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  288. }else {
  289. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  290. }
  291. let urlPathWithoutFirstSlash = data.urlPath.substring(1);
  292. this.$emit("update:fileKey", urlPathWithoutFirstSlash);
  293. this.$emit("update:videoUrl", line_1);
  294. this.$emit("update:line_1", line_1);
  295. // this.$emit("update:line_2", line_2);
  296. this.$emit("change");
  297. this.$message.success("线路一上传成功");
  298. } catch (error) {
  299. this.$message.error("线路一上传失败");
  300. throw error;
  301. }
  302. },
  303. //上传华为云Obs
  304. async uploadVideoToHwObs() {
  305. try {
  306. const file = this.fileList[0].raw;
  307. const data = await uploadToOBS(file, this.updateHwProgress,this.type);
  308. let line_2='' ;
  309. if (this.isPrivate===0){
  310. line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
  311. }else {
  312. line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
  313. }
  314. // this.$emit("update:videoUrl", data);
  315. this.$emit("update:line_2", line_2);
  316. this.$message.success("线路二上传成功");
  317. } catch (error) {
  318. this.$message.error("线路二上传失败");
  319. throw error;
  320. }
  321. },
  322. handleRemove(file, fileList) {
  323. },
  324. resetUpload() {
  325. // 重置内部状态
  326. this.txProgress = 0;
  327. this.hwProgress = 0;
  328. this.fileList = [];
  329. this.uploadKey++;
  330. },
  331. /** 查询视频库列表 */
  332. getLibraryList() {
  333. this.libraryLoading = true;
  334. // listVideoResource(this.libraryQueryParams).then(response => {
  335. // this.libraryList = response.rows;
  336. // this.libraryTotal = response.total;
  337. // this.libraryLoading = false;
  338. // });
  339. },
  340. /** 搜索视频库按钮操作 */
  341. handleLibraryQuery() {
  342. this.libraryQueryParams.pageNum = 1;
  343. this.getLibraryList();
  344. },
  345. /** 重置视频库查询按钮操作 */
  346. resetLibraryQuery() {
  347. this.libraryQueryParams = {
  348. pageNum: 1,
  349. pageSize: 10,
  350. resourceName: null
  351. };
  352. this.handleLibraryQuery();
  353. },
  354. /** 视频库选择行点击 */
  355. handleLibrarySelect(row) {
  356. this.selectedVideo = row;
  357. },
  358. /** 格式化视频时长 */
  359. formatDuration(seconds) {
  360. if (!seconds) return '00:00';
  361. const minutes = Math.floor(seconds / 60);
  362. const remainingSeconds = seconds % 60;
  363. return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  364. },
  365. /** 确认选择视频 */
  366. confirmVideoSelection() {
  367. if (!this.selectedVideo) {
  368. this.$message.warning("请选择一个视频");
  369. return;
  370. }
  371. // 更新组件内部数据
  372. this.$emit("update:fileName", this.selectedVideo.fileName);
  373. this.$emit("update:thumbnail", this.selectedVideo.thumbnail);
  374. this.$emit("update:line_1", this.selectedVideo.line1);
  375. this.$emit("update:line_2", this.selectedVideo.line2);
  376. this.$emit("update:line_3", this.selectedVideo.line3);
  377. this.$emit("update:fileSize", this.selectedVideo.fileSize);
  378. this.$emit("update:fileKey", this.selectedVideo.fileKey);
  379. this.$emit("update:uploadType", this.selectedVideo.uploadType);
  380. this.$emit("video-duration", this.selectedVideo.duration);
  381. // 设置预览URL
  382. this.$emit("update:videoUrl", this.selectedVideo.videoUrl);
  383. // 题目
  384. this.$emit("selectProjects", this.selectedVideo.projectIds)
  385. this.$emit("video-ready");
  386. this.libraryOpen = false;
  387. },
  388. /** 取消视频选择 */
  389. cancelVideoSelection() {
  390. this.libraryOpen = false;
  391. this.selectedVideo = null;
  392. }
  393. }
  394. };
  395. </script>
  396. <style>
  397. .progress-container {
  398. margin-bottom: 5px; /* 进度条之间的间距 */
  399. }
  400. .progress-label {
  401. display: block;
  402. font-weight: bold;
  403. font-size: 13px;
  404. color: #303331; /* 标签颜色,可以根据需要调整 */
  405. }
  406. /* 视频库选择对话框样式 */
  407. .library-search {
  408. margin-bottom: 15px;
  409. }
  410. .el-table .el-table__row:hover {
  411. cursor: pointer;
  412. }
  413. </style>