index.vue 14 KB

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