1 内容介绍
- 课程列表
- 课程列表显示
- 课程删除
- 阿里云视频点播服务
- 添加小节实现视频上传
2 课程列表功能
类似讲师列表
3 阿里云视频点播功能
-
转码模板组
-
文档&SDK
-
服务端SDK
-
上传SDK
-
API:阿里云提供固定地址,只需要调用这个地址,向地址传递参数,即可实现功能
固定地址:http(s)://vod.cn-shanghai.aliyuncs.com/ 参数:?Action=CreateUploadVideo &FileName=xxx
httpclient技术可以调用api地址
-
SDK:对api的封装,更方便使用;调用阿里云提供的类或者接口里面的方法实现视频功能
-
-
-
操作
- 依赖包
- 初始化
实例
ad1d3fe58e094f01828752e7a4741314
- 获取视频播放地址
- 获取视频播放凭证
- 上传视频到阿里云视频点播服务
注:因为上传视频可以进行加密,使用加密之后的地址不能进行视频播放,所以在数据库存储的不是视频的地址,而是视频的id
1 创建子模块
在service下创建子模块service_vod,并引入依赖
2 初始化
test/java/com.mys.vodtest/Init.java
public class Init {
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
String regionId = "cn-shanghai"; // 点播服务接入地域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
}
3 实现功能
-
根据视频id获取视频播放地址
// 1 根据视频id,获取视频播放地址 public static void getPlayUrl(String accessKeyId, String accessKeySecret) throws Exception{ // 1) 创建初始化对象 DefaultAcsClient client = Init.initVodClient(accessKeyId, accessKeySecret); // 2) 创建获取视频地址request和response GetPlayInfoRequest request = new GetPlayInfoRequest(); GetPlayInfoResponse response = new GetPlayInfoResponse(); // 3) 向request对象里面设置视频id request.setVideoId("ad1d3fe58e094f01828752e7a4741314"); // 4) 调用初始化对象里面的方法,传递request,获取数据 response = client.getAcsResponse(request); // 调用 List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList(); //播放地址 for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) { System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n"); } //Base信息 System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n"); }
-
获取视频播放凭证
// 2 根据视频id,获取视频的播放凭证 public static void getPlayAuth(String accessKeyId, String accessKeySecret) throws Exception { // 1) 创建初始化对象 DefaultAcsClient client = Init.initVodClient(accessKeyId, accessKeySecret); // 2) 创建获取视频地址request和response GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest(); GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse(); // 3) 向request对象里面设置视频id request.setVideoId("ad1d3fe58e094f01828752e7a4741314"); // 4) 调用初始化对象里面的方法,传递request,获取数据 response = client.getAcsResponse(request); System.out.println("PlayAuth" + response.getPlayAuth()); }
-
上传视频
public static void main(String[] args){ String accessKeyId = "LTAI5tDgYwJnddqgv7eseKUp"; String accessKeySecret = "dI3LnkakIe70taSwDTw6wspXQHN0xq"; String title = "mmm"; // 上传之后文件名称 String fileName = "D:\\BaiduNetdiskDownload\\项目资料\\1-阿里云上传测试视频\\6 - What If I Want to Move Faster.mp4"; // 本地文件路径和名称 // 3 上传视频 UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName); /* 可指定分片上传时每个分片的大小,默认为2M字节 */ request.setPartSize(2 * 1024 * 1024L); /* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/ request.setTaskNum(1); UploadVideoImpl uploader = new UploadVideoImpl(); UploadVideoResponse response = uploader.uploadVideo(request); if (response.isSuccess()) { System.out.print("VideoId=" + response.getVideoId() + "\n"); } else { /* 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */ System.out.print("VideoId=" + response.getVideoId() + "\n"); System.out.print("ErrorCode=" + response.getCode() + "\n"); System.out.print("ErrorMessage=" + response.getMessage() + "\n"); } }
4 添加小节上传视频
1 引入依赖
2 配置文件application.properties
# 服务端口
server.port=8003
# 服务名
spring.application.name=service-vod
# 环境设置:dev、test、prod
spring.profiles.active=dev
#阿里云 vod
#不同的服务器,地址不同
aliyun.vod.file.keyid=LTAI5tDgYwJnddqgv7eseKUp
aliyun.vod.file.keysecret=dI3LnkakIe70taSwDTw6wspXQHN0xq
# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB
3 创建启动类
// 启动类,默认不加载数据库相关内容
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages = {"com.mys"})
public class VodApplication {
public static void main(String[] args) {
SpringApplication.run(VodApplication.class, args);
}
}
4 创建controller service
controller
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {
@Autowired
private VodService vodService;
// 上传视频到阿里云
@PostMapping("uploadAlyVideo")
public R uploadAlyVideo(MultipartFile file) {
// 返回上传视频的id值
String videoId = vodService.uploadAlyVideo(file);
return R.ok().data("videoId", videoId);
}
}
utils
@Component
public class ConstantVodUtils implements InitializingBean {
@Value("${aliyun.vod.file.keyid}")
private String keyId;
@Value("${aliyun.vod.file.keysecret}")
private String keySecret;
//定义公开静态常量
public static String ACCESS_KEY_ID;
public static String ACCESS_KEY_SECRET;
@Override
public void afterPropertiesSet() throws Exception {
ACCESS_KEY_ID = keyId;
ACCESS_KEY_SECRET = keySecret;
}
}
service
@Service
public class VodServiceImpl implements VodService {
// 上传视频到阿里云
@Override
public String uploadAlyVideo(MultipartFile file) {
try {
//fileName:上传文件原始名称
String fileName = file.getOriginalFilename();
//title:上传之后显示名称
String title = fileName.substring(0, fileName.lastIndexOf("."));
//inputStream:上传文件输入流
InputStream inputStream = file.getInputStream();
UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
String videoId = response.getVideoId();
return videoId;
}catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
5 前端实现
-
nginx中配置
http: # 设置文件最大值 client_max_body_size 1024m; server: # 配置端口 location ~ /eduvod/ { proxy_pass http://localhost:8003; }
停止:
nginx.exe -s stop
-
chapter.vue
<el-form-item label="上传视频"> <el-upload :on-success="handleVodUploadSuccess" :on-remove="handleVodRemove" :before-remove="beforeVodRemove" :on-exceed="handleUploadExceed" :file-list="fileList" :action="BASE_API+'/eduvod/video/uploadAlyVideo'" :limit="1" class="upload-demo"> <el-button size="small" type="primary">上传视频</el-button> <el-tooltip placement="right-end"> <div slot="content">最大支持1G,<br> 支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br> GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br> MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br> SWF、TS、VOB、WMV、WEBM 等视频格式上传</div> <i class="el-icon-question"/> </el-tooltip> </el-upload> </el-form-item>
// 上传视频成功
handleVodUploadSuccess(response, file, fileList) {
this.video.videoSourceId = response.data.videoId
},
5 完善:添加小节删除视频
删除阿里云视频
-
后端接口
// 根据视频id删除阿里云的视频 @DeleteMapping("removeAlyVideo/{id}") public R removeAlyVideo(@PathVariable String id) { try { // 1 创建初始化对象 DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET); // 2 创建request对象 DeleteVideoRequest request = new DeleteVideoRequest(); // 3 设置视频id request.setVideoIds(id); // 4 调用删除视频 client.getAcsResponse(request); return R.ok(); } catch (Exception e) { e.printStackTrace(); throw new GuliException(20001, "删除视频失败"); } }
-
点击 x 删除
-
// 点击x时调用 beforeVodRemove(file, fileList) { return this.$confirm(`确认移除 ${file.name}?`) },
-
点击弹框确认,调用接口删除
// 点击确认调用 handleVodRemove() { // 调用接口中删除视频的方法 video.deleteAlyVideo(this.video.videoSourceId) .then(response => { // 提示信息 this.$message({ type: 'success', message: '删除视频成功!' }) // 情况文件列表 this.fileList = [] // 把video视频id和视频名称清空 this.video.videoSourceId = '' this.video.videoOriginName = '' }) }