3.3.2 定时添加
packagecom.czxy.zx.vod.schedule; importcom.czxy.zx.vod.service.VodTemplateService; importcom.czxy.zx.vod.utils.VideoUtils; importcom.czxy.zx.vod.utils.VodTemplate; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.scheduling.annotation.Scheduled; importorg.springframework.stereotype.Component; importjavax.annotation.Resource; importjava.util.List; /*** @author 桐叔* @email liangtong@itcast.cn*/publicclassTemplateSchedule { privateVideoUtilsvideoUtils; privateVodTemplateServicevodTemplateService; privateLoggerLOGGER=LoggerFactory.getLogger(TemplateSchedule.class); /*** 每月1号零点更新*/cron="0 0 0 1 * ?") (publicvoidhandlerTemplate() { // 从阿里云获得所有的转码模板List<VodTemplate>transcodeList=videoUtils.getTranscodeList(); if(LOGGER.isInfoEnabled()) { LOGGER.info("处理:"+transcodeList.size()); } // 保存数据for (VodTemplatevodTemplate : transcodeList) { // 查询VodTemplatefindTemplate=vodTemplateService.getById(vodTemplate.getTemplateId()); if(findTemplate==null) { vodTemplateService.save(vodTemplate); if(LOGGER.isInfoEnabled()) { LOGGER.info("添加:"+vodTemplate.getTemplateId()); } } else { // TODO 更新 } } } }
3.3.3 查询所有转码模板
packagecom.czxy.zx.vod.controller; importcom.czxy.zx.vo.BaseResult; importcom.czxy.zx.vod.service.VodCategoryService; importcom.czxy.zx.vod.service.VodTemplateService; importcom.czxy.zx.vod.utils.VodCategory; importcom.czxy.zx.vod.utils.VodTemplate; importorg.springframework.web.bind.annotation.GetMapping; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; importjavax.annotation.Resource; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; /*** @author 桐叔* @email liangtong@itcast.cn*/"/vodtemplate") (publicclassVodTemplateController { privateVodTemplateServicevodTemplateService; publicBaseResultfindAll() { //1 查询所有List<VodTemplate>list=vodTemplateService.list(); //3 返回returnBaseResult.ok("查询成功", list); } }
3.4 整合分类和转码模板
3.4.1 需求
- 级联菜单支持选择任意项的功能
- 转码模板,查询结果为“NoTranscode”,显示处理成“无加密”
3.4.2 ajax操作
- 编写ajax
importaxiosfrom'@/utils/request'// 查询所有vod分类exportfunctionfindAllVodCate() { returnaxios.get('/vod-service/vodcategory'); } // 保存课程exportfunctionfindAllvodTemp(courseVo) { returnaxios.get('/vod-service/vodtemplate'); }
import { findAllVodCate, findAllvodTemp } from '@/api/edu/vod'
调用ajax:编写函数声明变量data() { return { vodCategoryList: [], vodTemplateList: [], } } 编写函数asyncfindAllVodCategory() { let { data } =awaitfindAllVodCate() this.vodCategoryList=data }, asyncfindAllvodTemplate() { let { data } =awaitfindAllvodTemp() this.vodTemplateList=data }
asyncnextChapter() { // 保存let { data,message } =awaitsaveCourse(this.course) debuggerthis.$message.success(message) // 保存idthis.course.id=datathis.active=2// 查询所有章节this.findAllChapter() // 查询vod分类this.findAllVodCategory() // 查询vod转码模板this.findAllvodTemplate() },
3.4.3 数据展示
- 声明变量
data() { return { vodCategoryProps: { // vod分类与级联选择对应关系value: 'cateId', label: 'cateName', checkStrictly: true }, vodVideo: { // vod数据封装对象 } } } 展示数据<el-form-itemlabel="上传视频"label-width="80px"><!--分类级联--><el-row><el-col :span="4"style="text-align:right">分类:</el-col><el-col :span="20"><el-cascaderv-model="vodVideo.categoryId" :options="vodCategoryList" :props="vodCategoryProps"style="width:300px"></el-cascader></el-col></el-row><!--转码模板单选按钮组--><el-row><el-col :span="4"style="text-align:right">转码模板:</el-col><el-col :span="20"><el-radio-group><el-radiov-for="(temp,index) in vodTemplateList" :key="index"v-model="vodVideo.templateId" :label="temp.templateId"> {{temp.name=='NoTranscode'?'无加密' : temp.name}} {{temp.isDefault=='Default'?'(默认)' : ''}} </el-radio></el-radio-group></el-col></el-row><!--上传视频--><el-row><el-col :span="4"style="text-align:right">选择视频:</el-col><el-col :span="20"><!--上传组件--></el-col></el-row></el-form-item>
3.5 视频上传
3.5.1 需求
3.5.2 后端实现
- 上传视频,并返回视频信息
- 问题:视频上传成功后,阿里云需要一个处理视频的视频,随意不能实时观看。
- 采取的方案:先上传,再查询视频信息
packagecom.czxy.zx.vod.controller; importcom.czxy.zx.vo.BaseResult; importcom.czxy.zx.vod.utils.VideoUtils; importcom.czxy.zx.vod.utils.VodVideoInfo; importorg.springframework.web.bind.annotation.*; importorg.springframework.web.multipart.MultipartFile; importjavax.annotation.Resource; /*** @author 桐叔* @email liangtong@itcast.cn*/"/vodvideo") (publicclassVodVideoController { privateVideoUtilsvideoUtils; /*** 上传视频* @param categoryId* @param templateId* @param file* @return*/"/upload/{categoryId}/{templateId}") (publicBaseResultupload( "categoryId") LongcategoryId, ("templateId") StringtemplateId, (MultipartFilefile) { // 1 上传StringvideoId=videoUtils.uploadVideo(categoryId, templateId, file); returnBaseResult.ok("上传成功", videoId); } /*** 查询视频信息* @param videoId* @return*/"/{videoId}") (publicBaseResultgetInfo( ("videoId") StringvideoId) { // 2 获得商品信息VodVideoInfoplayInfo=videoUtils.getPlayInfo(videoId); //returnBaseResult.ok("查询成功", playInfo); } }
3.5.3 前端实现
- 编写ajax
编写上传组件<el-col :span="20"><!--上传组件--><el-uploadclass="upload-demo" :action="uploadUrl" :before-upload="beforeVideoUpload" :on-success="videoUploadSuccess" :file-list="videoFileList" :limit="1"><el-buttonsize="small"type="primary">点击上传</el-button><divslot="tip"class="el-upload__tip">只能上传视频文件</div></el-upload><el-buttontype="primary"="findVodVideoInfo">同步数据</el-button><el-avatarshape="square" :size="50" :src="video.coverURL"></el-avatar><br/> {{video}} </el-col>声明变量data() { return { videoFileList: [], // 上传文件列表videoId: '0fc9c1413b334b21b1ea48413c4ab35b', // 视频id } } 声明函数methods() { beforeVideoUpload() { if(!this.vodVideo.categoryId) { this.$message.warning('必须选择分类') returnfalse } if(!this.vodVideo.templateId) { this.$message.warning('必须选择模板') returnfalse } }, videoUploadSuccess(response, file, fileList) { if(response.code==0) { this.$message.error(response.message) this.videoFileList= [] return } // 成功this.$message.success(response.message) // 查询详情this.videoId=response.datasetTimeout(async ()=> { this.findVodVideoInfo() } , 1000) }, asyncfindVodVideoInfo() { let { data } =awaitfindVideoInfo(this.videoId) // 设置视频信息this.video.coverURL=data.coverURLthis.video.videoSourceId=data.playURLthis.video.videoOriginalName=data.titleconsole.info(this.video) } } 动态设置上传路径computed: { uploadUrl() { // 上传路径// 如果是数组if(Array.isArray(this.vodVideo.categoryId)) { // 处理id 获取最后一个分类letcategoryId=this.vodVideo.categoryId[this.vodVideo.categoryId.length-1] // 拼凑上传地址return`${process.env.VUE_APP_BASE_API}/vod-service/vodvideo/upload/${categoryId}/${this.vodVideo.templateId}` } return"" } }
- 视频上传成功后,保存video信息时异常,
video_source_id
长度太短
ALTER TABLE edu_video MODIFY COLUMN video_source_id VARCHAR(500)