single.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 { listVideoResource } from '@/api/course/videoResource';
  121. import { listLiveVideo, getLiveVideo, delLiveVideo, addLiveVideo, updateLiveVideo, exportLiveVideo } from "@/api/live/liveVideo";
  122. export default {
  123. components: {
  124. Pagination
  125. },
  126. props: {
  127. videoUrl: {
  128. type: String,
  129. default: "",
  130. },
  131. fileKey: {
  132. type: String,
  133. default: "",
  134. },
  135. fileSize: {
  136. type: Number,
  137. default: null,
  138. },
  139. fileName: {
  140. type: String,
  141. default: "",
  142. },
  143. line_1: {
  144. type: String,
  145. default: "",
  146. },
  147. line_2: {
  148. type: String,
  149. default: "",
  150. },
  151. line_3: {
  152. type: String,
  153. default: "",
  154. },
  155. thumbnail: {
  156. type: String,
  157. default: "",
  158. },
  159. uploadType: {
  160. type: Number,
  161. default: null,
  162. },
  163. type: {
  164. type: Number,
  165. default: null,
  166. },
  167. isPrivate: {
  168. type: Number,
  169. default: 0,
  170. },
  171. // 使用一个变量控制显示,默认为true显示所有控制项
  172. showControl: {
  173. type: Boolean,
  174. default: true,
  175. }
  176. },
  177. data() {
  178. return {
  179. videoName:'',
  180. isHidden : false,
  181. localUploadType: this.uploadType,
  182. duration: 0,
  183. fileId: "",
  184. uploadTypeOptions: [
  185. { dictLabel: "线路一", dictValue: 1 }, // 腾讯pcdn
  186. { dictLabel: "线路二", dictValue: 2 }, // 华为云obs
  187. // { dictLabel: "华为云VOD", dictValue: 4 },
  188. // { dictLabel: "腾讯云VOD", dictValue: 5 },
  189. ],
  190. fileList: [],
  191. txProgress: 0,
  192. hwProgress: 0,
  193. uploadLoading: false,
  194. uploadKey: 0,
  195. // 视频库选择相关数据
  196. libraryOpen: false,
  197. libraryLoading: false,
  198. libraryTotal: 0,
  199. libraryList: [],
  200. selectedVideo: null,
  201. libraryQueryParams: {
  202. pageNum: 1,
  203. pageSize: 10,
  204. resourceName: null,
  205. liveId: -1,
  206. videoUrl: null,
  207. videoType: -1,
  208. sort: null,
  209. remark: null,
  210. finishStatus:1
  211. },
  212. liveQueryParams: {
  213. pageNum: 1,
  214. pageSize: 10,
  215. liveId: -1,
  216. videoUrl: null,
  217. videoType: -1,
  218. sort: null,
  219. remark: null,
  220. },
  221. };
  222. },
  223. mounted() {
  224. this.reset();
  225. },
  226. watch: {
  227. uploadType(newType) {
  228. this.localUploadType = newType;
  229. },
  230. },
  231. methods: {
  232. /** 查询直播视频列表 */
  233. getList() {
  234. this.libraryLoading = true;
  235. listLiveVideo(this.libraryQueryParams).then(response => {
  236. this.libraryList = response.rows;
  237. this.libraryTotal = response.total;
  238. this.libraryLoading = false;
  239. });
  240. },
  241. // 打开视频库对话框
  242. openVideoLibrary() {
  243. this.libraryOpen = true;
  244. this.selectedVideo = null;
  245. this.getList();
  246. },
  247. handleChange(file, fileList) {
  248. this.fileList = fileList;
  249. this.getVideoDuration(file.raw);
  250. },
  251. getVideoDuration(file) {
  252. const video = document.createElement("video");
  253. video.preload = "metadata";
  254. video.onloadedmetadata = () => {
  255. window.URL.revokeObjectURL(video.src);
  256. this.duration = parseInt(video.duration.toFixed(2));
  257. console.log("视频时长=========>",this.duration);
  258. this.$emit("video-duration", this.duration);
  259. console.log("文件大小=====>",file.size);
  260. this.$emit("update:fileSize", file.size);
  261. };
  262. video.src = URL.createObjectURL(file);
  263. },
  264. async submitUpload() {
  265. if (this.fileList.length < 1) {
  266. return this.$message.error("请先选取视频,再进行上传");
  267. }
  268. //同时上传个线路
  269. await this.uploadVideoToTxPcdn();
  270. this.$emit("update:fileName", this.fileList[0].name);
  271. },
  272. //获取第一帧封面
  273. async getFirstThumbnail(){
  274. const file = this.fileList[0].raw;
  275. getThumbnail(file).then(response => {
  276. console.log("获取到第一帧为封面======>",response.url)
  277. this.$emit("update:thumbnail", response.url);
  278. })
  279. },
  280. //更新华为线路进度条
  281. // updateHwProgress(progress) {
  282. // this.hwProgress = progress;
  283. // },
  284. //更新腾讯线路进度条
  285. updateTxProgress(progressData) {
  286. this.txProgress = Math.round(progressData.percent * 100);
  287. },
  288. //上传腾讯云Pcdn
  289. async uploadVideoToTxPcdn() {
  290. try {
  291. const file = this.fileList[0].raw;
  292. const data = await uploadObject(file, this.updateTxProgress,this.type);
  293. console.log("腾讯COS返回========>",data);
  294. console.log("isPrivate=======>",this.isPrivate)
  295. let line_1='' ;
  296. if (this.isPrivate===0){
  297. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  298. }else {
  299. line_1 = `${process.env.VUE_APP_VIDEO_LINE_1}${data.urlPath}`;
  300. }
  301. line_1 = line_1.replace(/\.mp4$/, '.m3u8');
  302. let urlPathWithoutFirstSlash = data.urlPath.substring(1);
  303. this.$emit("update:fileKey", urlPathWithoutFirstSlash);
  304. console.log("文件key",urlPathWithoutFirstSlash);
  305. console.log("组装URL========>",line_1);
  306. this.$emit("update:videoUrl", line_1);
  307. this.$emit("update:line_1", line_1);
  308. this.$emit("change", 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. liveId: -1,
  365. videoUrl: null,
  366. videoType: -1,
  367. sort: null,
  368. remark: null,
  369. finishStatus:1
  370. };
  371. this.handleLibraryQuery();
  372. },
  373. /** 视频库选择行点击 */
  374. handleLibrarySelect(row) {
  375. this.selectedVideo = row;
  376. },
  377. /** 格式化视频时长 */
  378. formatDuration(seconds) {
  379. if (!seconds) return '00:00';
  380. const minutes = Math.floor(seconds / 60);
  381. const remainingSeconds = seconds % 60;
  382. return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  383. },
  384. /** 确认选择视频 */
  385. confirmVideoSelection() {
  386. if (!this.selectedVideo) {
  387. this.$message.warning("请选择一个视频");
  388. return;
  389. }
  390. console.log("confirmVideoSelection")
  391. console.log(this.selectedVideo)
  392. // 更新组件内部数据
  393. this.$emit("update:fileName", this.selectedVideo.fileName);
  394. this.$emit("update:line_1", this.selectedVideo.lineOne);
  395. this.$emit("update:fileSize", this.selectedVideo.fileSize);
  396. this.$emit("update:fileKey", this.selectedVideo.fileKey);
  397. this.$emit("video-duration", this.selectedVideo.duration);
  398. // 设置预览URL
  399. this.$emit("update:videoUrl", this.selectedVideo.videoUrl);
  400. this.$emit("change", this.selectedVideo.videoUrl,this.selectedVideo.lineOne);
  401. this.libraryOpen = false;
  402. },
  403. /** 取消视频选择 */
  404. cancelVideoSelection() {
  405. this.libraryOpen = false;
  406. this.selectedVideo = null;
  407. }
  408. }
  409. };
  410. </script>
  411. <style>
  412. .progress-container {
  413. margin-bottom: 5px; /* 进度条之间的间距 */
  414. }
  415. .progress-label {
  416. display: block;
  417. font-weight: bold;
  418. font-size: 13px;
  419. color: #303331; /* 标签颜色,可以根据需要调整 */
  420. }
  421. /* 视频库选择对话框样式 */
  422. .library-search {
  423. margin-bottom: 15px;
  424. }
  425. .el-table .el-table__row:hover {
  426. cursor: pointer;
  427. }
  428. </style>