single.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. <!-- &lt;!&ndash; 线路一 &ndash;&gt;-->
  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 { listVideoResource } from '@/api/course/videoResource';
  121. import { listLiveVideo, getLiveVideo, delLiveVideo, addLiveVideo, updateLiveVideo, exportLiveVideo } from "@/api/live/liveVideo";
  122. import Hls from 'hls.js';
  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. hls:null,
  206. libraryQueryParams: {
  207. pageNum: 1,
  208. pageSize: 10,
  209. resourceName: null,
  210. liveId: -1,
  211. videoUrl: null,
  212. videoType: -1,
  213. sort: null,
  214. remark: null,
  215. finishStatus:1
  216. },
  217. liveQueryParams: {
  218. pageNum: 1,
  219. pageSize: 10,
  220. liveId: -1,
  221. videoUrl: null,
  222. videoType: -1,
  223. sort: null,
  224. remark: null,
  225. },
  226. };
  227. },
  228. mounted() {
  229. this.reset();
  230. if (this.videoUrl.endsWith(".m3u8")) {
  231. this.$nextTick(() => {
  232. this.initPlayer()
  233. })
  234. }
  235. },
  236. watch: {
  237. videoUrl(newVal,oldVal){
  238. console.log("触发数据改变")
  239. if (this.videoUrl.endsWith(".m3u8")) {
  240. this.$nextTick(() => {
  241. this.initPlayer()
  242. })
  243. }
  244. },
  245. uploadType(newType) {
  246. this.localUploadType = newType;
  247. },
  248. },
  249. methods: {
  250. initPlayer() {
  251. this.hls?.destroy();
  252. if (Hls.isSupported()) {
  253. const videoElement = this.$refs.myM3u8Video
  254. if (!videoElement) {
  255. console.error('找不到 video 元素')
  256. return
  257. }
  258. this.hls = new Hls();
  259. this.hls.attachMedia(videoElement);
  260. this.hls.on(Hls.Events.MEDIA_ATTACHED, () => {
  261. this.hls.loadSource(this.videoUrl);
  262. this.hls.on(Hls.Events.STREAM_LOADED, (event, data) => {
  263. videoElement.play();
  264. });
  265. });
  266. this.hls.on(Hls.Events.ERROR, (event, data) => {
  267. console.error('HLS 错误:', data);
  268. });
  269. } else {
  270. console.error('浏览器不支持 HLS')
  271. }
  272. },
  273. /** 查询直播视频列表 */
  274. getList() {
  275. this.libraryLoading = true;
  276. listLiveVideo(this.libraryQueryParams).then(response => {
  277. this.libraryList = response.rows;
  278. this.libraryTotal = response.total;
  279. this.libraryLoading = false;
  280. });
  281. },
  282. // 打开视频库对话框
  283. openVideoLibrary() {
  284. this.libraryOpen = true;
  285. this.selectedVideo = null;
  286. this.getList();
  287. },
  288. handleChange(file, fileList) {
  289. this.fileList = fileList;
  290. this.getVideoDuration(file.raw);
  291. },
  292. getVideoDuration(file) {
  293. const video = document.createElement("video");
  294. video.preload = "metadata";
  295. video.onloadedmetadata = () => {
  296. window.URL.revokeObjectURL(video.src);
  297. this.duration = parseInt(video.duration.toFixed(2));
  298. console.log("视频时长=========>",this.duration);
  299. this.$emit("video-duration", this.duration);
  300. console.log("文件大小=====>",file.size);
  301. this.$emit("update:fileSize", file.size);
  302. };
  303. video.src = URL.createObjectURL(file);
  304. },
  305. async submitUpload() {
  306. if (this.fileList.length < 1) {
  307. return this.$message.error("请先选取视频,再进行上传");
  308. }
  309. //同时上传个线路
  310. await this.uploadVideoToTxPcdn();
  311. this.$emit("update:fileName", this.fileList[0].name);
  312. },
  313. //获取第一帧封面
  314. async getFirstThumbnail(){
  315. const file = this.fileList[0].raw;
  316. getThumbnail(file).then(response => {
  317. console.log("获取到第一帧为封面======>",response.url)
  318. this.$emit("update:thumbnail", response.url);
  319. })
  320. },
  321. //更新华为线路进度条
  322. // updateHwProgress(progress) {
  323. // this.hwProgress = progress;
  324. // },
  325. //更新腾讯线路进度条
  326. updateTxProgress(progressData) {
  327. this.txProgress = Math.round(progressData.percent * 100);
  328. },
  329. //上传腾讯云Pcdn
  330. async uploadVideoToTxPcdn() {
  331. try {
  332. const file = this.fileList[0].raw;
  333. const data = await uploadObject(file, this.updateTxProgress,this.type);
  334. console.log("腾讯COS返回========>",data);
  335. console.log("isPrivate=======>",this.isPrivate)
  336. let line_1='' ;
  337. if (this.isPrivate===0){
  338. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  339. }else {
  340. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  341. }
  342. line_1 = line_1.replace(/\.mp4$/, '.m3u8');
  343. let urlPathWithoutFirstSlash = data.urlPath.substring(1);
  344. this.$emit("update:fileKey", urlPathWithoutFirstSlash);
  345. console.log("文件key",urlPathWithoutFirstSlash);
  346. console.log("组装URL========>",line_1);
  347. this.$emit("update:videoUrl", line_1);
  348. this.$emit("update:line_1", line_1);
  349. this.$emit("change", line_1,line_1);
  350. // this.$emit("update:line_2", line_2);
  351. this.$message.success("线路一上传成功");
  352. } catch (error) {
  353. this.$message.error("线路一上传失败");
  354. }
  355. },
  356. //上传华为云Obs
  357. async uploadVideoToHwObs() {
  358. try {
  359. const file = this.fileList[0].raw;
  360. const data = await uploadToOBS(file, this.updateHwProgress,this.type);
  361. console.log("华为OBS返回========>",data);
  362. let line_2='' ;
  363. if (this.isPrivate===0){
  364. line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
  365. }else {
  366. line_2 = `${process.env.VUE_APP_VIDEO_LINE_2}/${data.urlPath}`;
  367. }
  368. // this.$emit("update:videoUrl", data);
  369. this.$emit("update:line_2", line_2);
  370. this.$message.success("线路二上传成功");
  371. } catch (error) {
  372. this.$message.error("线路二上传失败");
  373. }
  374. },
  375. handleRemove(file, fileList) {
  376. console.log(file, fileList.length);
  377. },
  378. reset() {
  379. // 重置内部状态
  380. this.txProgress = 0;
  381. this.fileList = [];
  382. // this.$refs.upload.clearFiles();
  383. this.uploadKey++;
  384. },
  385. /** 查询视频库列表 */
  386. getLibraryList() {
  387. this.libraryLoading = true;
  388. // listVideoResource(this.libraryQueryParams).then(response => {
  389. // this.libraryList = response.rows;
  390. // this.libraryTotal = response.total;
  391. // this.libraryLoading = false;
  392. // });
  393. },
  394. /** 搜索视频库按钮操作 */
  395. handleLibraryQuery() {
  396. this.libraryQueryParams.pageNum = 1;
  397. this.getList();
  398. },
  399. /** 重置视频库查询按钮操作 */
  400. resetLibraryQuery() {
  401. this.libraryQueryParams = {
  402. pageNum: 1,
  403. pageSize: 10,
  404. resourceName: null,
  405. liveId: -1,
  406. videoUrl: null,
  407. videoType: -1,
  408. sort: null,
  409. remark: null,
  410. finishStatus:1
  411. };
  412. this.handleLibraryQuery();
  413. },
  414. /** 视频库选择行点击 */
  415. handleLibrarySelect(row) {
  416. this.selectedVideo = row;
  417. },
  418. /** 格式化视频时长 */
  419. formatDuration(seconds) {
  420. if (!seconds) return '00:00';
  421. const minutes = Math.floor(seconds / 60);
  422. const remainingSeconds = seconds % 60;
  423. return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  424. },
  425. /** 确认选择视频 */
  426. confirmVideoSelection() {
  427. if (!this.selectedVideo) {
  428. this.$message.warning("请选择一个视频");
  429. return;
  430. }
  431. console.log("confirmVideoSelection")
  432. console.log(this.selectedVideo)
  433. // 更新组件内部数据
  434. this.$emit("update:fileName", this.selectedVideo.fileName);
  435. this.$emit("update:line_1", this.selectedVideo.lineOne);
  436. this.$emit("update:fileSize", this.selectedVideo.fileSize);
  437. this.$emit("update:fileKey", this.selectedVideo.fileKey);
  438. this.$emit("video-duration", this.selectedVideo.duration);
  439. // 设置预览URL
  440. this.$emit("update:videoUrl", this.selectedVideo.videoUrl);
  441. this.$emit("change", this.selectedVideo.videoUrl,this.selectedVideo.lineOne);
  442. if (this.videoUrl.endsWith(".m3u8")) {
  443. this.$nextTick(() => {
  444. this.initPlayer()
  445. })
  446. }
  447. this.libraryOpen = false;
  448. },
  449. /** 取消视频选择 */
  450. cancelVideoSelection() {
  451. this.libraryOpen = false;
  452. this.selectedVideo = null;
  453. }
  454. }
  455. };
  456. </script>
  457. <style>
  458. .progress-container {
  459. margin-bottom: 5px; /* 进度条之间的间距 */
  460. }
  461. .progress-label {
  462. display: block;
  463. font-weight: bold;
  464. font-size: 13px;
  465. color: #303331; /* 标签颜色,可以根据需要调整 */
  466. }
  467. /* 视频库选择对话框样式 */
  468. .library-search {
  469. margin-bottom: 15px;
  470. }
  471. .el-table .el-table__row:hover {
  472. cursor: pointer;
  473. }
  474. </style>