index.vue 15 KB

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