SpringBoot阿里云视频点播
LL.LEBRON 人气:01.准备工作
首先需要在阿里云开通视频点播服务:
1.首先,进入到阿里云视频点播平台,点击开通服务,选择按使用流量计费即可
2.开通之后点击进入管理控制台即可
视频点播有什么用?
视频点播(ApsaraVideo for VoD)是集音视频采集、编辑、上传、自动化转码处理、媒体资源管理、分发加速于一体的一站式音视频点播解决方案。
2.服务端SDK的使用
官方文档链接:Java SDK
SDK
的方式将api进行了进一步的封装,不用自己创建工具类。 我们可以基于服务端SDK
编写代码来调用点播API,实现对点播产品和服务的快速操作。
2.1 导入依赖
注意:环境必须是JDK6 及以上版本
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.1</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-vod</artifactId> <version>2.15.11</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-kms</artifactId> <version>2.10.1</version> </dependency>
2.2 初始化类
根据官方文档示例创建初始化类InitObject
:
/** * @author xppll * @date 2021/12/5 13:57 */ public class InitObject { //初始化:需要传入accessKeyId+accessKeySecret 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; } }
accessKeyId
和accessKeySecret
的获取:
这里的初始化方法需要传入账号Access Key
信息,可以在阿里云Access Key管理创建主账号Access Key
点播服务接入区域
regionId
的获取:
点击点播控制台的存储管理,查看自己所选的区域:
在查看接入区域标识对应API/SDK的RegionId
参数:
2.3 创建读取公共常量的工具类
首先在配置文件application.properties
添加:
aliyun.vod.file.keyid=LTAI5tGqf41adadaxxAa aliyun.vod.file.keysecret=zE4nEzmcU7GxxxxmTgWRk0tytJKD # 最大上传单个文件大小:默认1M spring.servlet.multipart.max-file-size=1024MB # 最大置总上传的数据大小 :默认10M spring.servlet.multipart.max-request-size=1024MB
在创建读取公共常量的工具类ConstantVodUtils
:
/** * @author xppll * @date 2021/12/2 19:11 */ @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; } }
2.4 获取视频播放地址
首先手动先在点播控制台上传一个视频用于测试:
步骤:
- 创建初始化对象
- 创建获取视频地址
request
和response
- 向
request
对象里面设置视频id
- 用初始化对象里面的方法
getAcsResponse
,传递request
,获取数据 - 打印信息
代码如下:
//获取视频播放地址 private static void getPlayUrl() throws ClientException { //1.创建初始化对象 DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET); //2.创建获取视频地址request和response GetPlayInfoRequest request = new GetPlayInfoRequest(); GetPlayInfoResponse response = new GetPlayInfoResponse(); //3.向request对象里面设置视频id request.setVideoId("ffe90bfaxxx94d0d722caad"); //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.5 获取视频播放凭证
步骤与获取视频播放地址差不多,区别是获取request
和response
方式不同:
//获取视频播放凭证 private static void getPlayAuth() throws ClientException { //1.创建初始化对象 DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET); //2.创建获取视频凭证的request和response GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest(); GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse(); //3.向request设置视频id request.setVideoId("ffe90bfaaadd4xxxx0d722caad"); //4.调用初始化对象的方法得到凭证 response = client.getAcsResponse(request); //播放凭证 System.out.println("playauth" + response.getPlayAuth()); }
测试成功:
2.6 上传视频到阿里云视频点播服务
可以参考:Java上传SDK
需要导入依赖:
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.1</version> </dependency> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-vod</artifactId> <version>2.15.11</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20170516</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency>
代码如下:
//上传视频到阿里云视频点播服务 private static void uploadVideo() { String accessKeyId = ConstantVodUtils.ACCESS_KEY_ID; String accessKeySecret = ConstantVodUtils.ACCESS_KEY_SECRET; //上传后视频的标题 String title = "xpp1"; //本地文件上传路径 String fileName = "D:/6 - What If I Want to Move Faster.mp4"; UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName); //可指定分片上传时每个分片的大小,默认为2M字节 request.setPartSize(2 * 1024 * 1024L); //可指定分片上传时的并发线程数,默认为1 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"); } }
测试成功:
3.springboot项目中实践
创建初始化类:
/** * @author xppll * @date 2021/12/5 13:57 */ public class InitVodCilent { //初始化:需要传入accessKeyId+accessKeySecret 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.1 上传视频到阿里云
controller
层:
@Autowired private VodService vodService; //上传视频到阿里云 @PostMapping("uploadAlyiVideo") public R uploadAlyVideo(MultipartFile file){ //返回上传视频id String videoId=vodService.uploadVideoAly(file); //将视频`id`返回给前端 return R.ok().data("videoId",videoId); }
service
层:
//上传视频到阿里云 @Override public String uploadVideoAly(MultipartFile file) { try { //fileName:上传文件原始名称 String fileName = file.getOriginalFilename(); //title:上传之后显示名称(例子:01.mp4=>01) String title = fileName.substring(0, fileName.lastIndexOf(".")); //inputStream:上传文件输入流 InputStream inputStream = file.getInputStream(); //获得request UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream); UploadVideoImpl uploader = new UploadVideoImpl(); //上传获得response UploadStreamResponse response = uploader.uploadStream(request); String videoId = null; if (response.isSuccess()) { //得到视频id videoId = response.getVideoId(); } else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 videoId = response.getVideoId(); } return videoId; } catch (IOException e) { e.printStackTrace(); return null; } }
3.2 根据视频id删除视频
controller
层:
//根据视频id删除视频 @DeleteMapping("removeAlyVideo/{id}") public R removeAlyVideo(@PathVariable String id){ vodService.deleteAlyVideo(id); return R.ok(); }
service
层:
//根据视频id删除视频 @Override public void deleteAlyVideo(String id) { try { //1.初始化对象 DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET); //2.创建删除视频的request对象 DeleteVideoRequest request=new DeleteVideoRequest(); //3.向request设置视频id request.setVideoIds(id); //4.调用初始化对象的方法实现删除 client.getAcsResponse(request); } catch (Exception e) { e.printStackTrace(); throw new GuliException(20001,"删除视频失败"); } }
加载全部内容