Python 官方文档:入门教程 => 点击学习
目录1.准备工作2.服务端SDK的使用2.1 导入依赖2.2 初始化类2.3 创建读取公共常量的工具类2.4 获取视频播放地址2.5 获取视频播放凭证2.6 上传视频到阿里云视频点播
首先需要在阿里云开通视频点播服务:
1.首先,进入到阿里云视频点播平台,点击开通服务,选择按使用流量计费即可
2.开通之后点击进入管理控制台即可
视频点播有什么用?
视频点播(ApsaraVideo for VoD)是集音视频采集、编辑、上传、自动化转码处理、媒体资源管理、分发加速于一体的一站式音视频点播解决方案。
官方文档链接:Java SDK
SDK
的方式将api进行了进一步的封装,不用自己创建工具类。 我们可以基于服务端SDK
编写代码来调用点播API,实现对点播产品和服务的快速操作。
注意:环境必须是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>
根据官方文档示例创建初始化类InitObject
:
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
参数:
首先在配置文件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
:
@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;
}
}
首先手动先在点播控制台上传一个视频用于测试:
步骤:
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");
}
测试成功:
步骤与获取视频播放地址差不多,区别是获取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());
}
测试成功:
可以参考: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 {
System.out.print("VideoId=" + response.getVideoId() + "\n");
System.out.print("ErrorCode=" + response.getCode() + "\n");
System.out.print("ErrORMessage=" + response.getMessage() + "\n");
}
}
测试成功:
创建初始化类:
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;
}
}
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;
}
}
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,"删除视频失败");
}
}
到此这篇关于SpringBoot整合阿里云视频点播的文章就介绍到这了,更多相关SpringBoot阿里云视频点播内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBoot整合阿里云视频点播的过程详解
本文链接: https://lsjlt.com/news/159215.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0