index.vue 15 KB

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