index.vue 15 KB

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