谷粒学院-14-视频点播
API和SDK调用方式:
API:阿里云提供固定的地址,只需要调用这个固定的地址,向地址传递参数,实现功能(需要用到httpClient技术)
SDK:sdk是在服务层调用对应的源码,调用阿里云提供的类和接口里面的方法就可以实现视频功能(实际上就是对api进行了封装
获取播放地址(新):
因为上传视频可以进行加密,加密之后,使用加密地址是不能进行视频播放的,所以在数据库中不存储地址而是存储 视频id,每次上传视频之后,都可以获取到视频id,这个视频id可以获取到视频的播放地址和播放凭证
导入依赖
<dependencies>
<!--旧版SDK-->
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->
<!-- <artifactId>aliyun-java-sdk-vod</artifactId>-->
<!-- <version>2.15.5</version>-->
<!-- </dependency>-->
<!--新版SDK-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>vod20170321</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
初始化client对象
public static com.aliyun.vod20170321.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "vod.cn-shenzhen.aliyuncs.com";
return new com.aliyun.vod20170321.Client(config);
}
获取视频url
public static void main(String[] args) throws Exception {
//1、先得到client
com.aliyun.vod20170321.Client client = testVod2.createClient("LTAI4G499pxUfMrAJ2TXuCJz", "et6OsY7VZMfZ84oKbJIjqP3TT7KTCO");
//2、根据需要创建对应的request
GetPlayInfoRequest getPlayInfoRequest = new GetPlayInfoRequest()
.setVideoId("33fab662eec64e29826c36ef18eee017");
//3、根据创建的request,在client上使用不同的方法去调用这个request
GetPlayInfoResponse playInfo = client.getPlayInfo(getPlayInfoRequest);
//4、然后我们得到的就是一个类似与json的数据
System.out.println(playInfo.body.playInfoList.playInfo.get(0).playURL);
}
playInfo的数据
{
"VideoBase": {
"Status": "Normal",
"VideoId": "33fab662eec64e29826c36ef18eee017",
"TranscodeMode": "NoTranscode",
"CreationTime": "2021-08-06T02:59:09Z",
"Title": "6 - What If I Want to Move Faster.mp4",
"MediaType": "video",
"CoverURL": "http://outin-db30bbecf65111eb875e00163e021072.oss-cn-shenzhen.aliyuncs.com/33fab662eec64e29826c36ef18eee017/snapshots/af3b97324d544595a61356aefa78c01c-00001.jpg?Expires=1628224206&OSSAccessKeyId=LTAI4FocoL6tuCdYhuvug6Ee&Signature=YY4WQgCt%2BabEh3Jozaak3VXRmXc%3D",
"Duration": "16.2767",
"OutputType": "oss"
},
"RequestId": "0702E0D3-6DF8-5EAF-9628-DCED3BC48111",
"PlayInfoList": {
"PlayInfo": [
{
"Status": "Normal",
"StreamType": "video",
"Size": 2211663,
"Definition": "OD",
"Fps": "29.97003",
"Duration": "16.2767",
"ModificationTime": "2021-08-06T02:59:09Z",
"Specification": "Original",
"Bitrate": "1087.034",
"Encrypt": 0,
"PreprocessStatus": "UnPreprocess",
"Format": "mp4",
"PlayURL": "https://outin-db30bbecf65111eb875e00163e021072.oss-cn-shenzhen.aliyuncs.com/sv/37a4eff0-17b19667327/37a4eff0-17b19667327.mp4?Expires=1628224206&OSSAccessKeyId=LTAI4FocoL6tuCdYhuvug6Ee&Signature=gV46WMR2LP7Lhia22IV93M4HPJg%3D",
"NarrowBandType": "0",
"CreationTime": "2021-08-06T02:59:09Z",
"Height": 480,
"Width": 852,
"JobId": "33fab662eec64e29826c36ef18eee01702"
}
]
}
}
获取上传凭证
跟上图一致:就是创建的request不是GetPlayInfoRequest了,而是GetVideoPlayAuthRequest
public static void main(String[] args) throws Exception {
com.aliyun.vod20170321.Client client = testVod2.createClient("LTAI4G499pxUfMrAJ2TXuCJz", "et6OsY7VZMfZ84oKbJIjqP3TT7KTCO");
System.out.println("-------------------------------");
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest().setVideoId("33fab662eec64e29826c36ef18eee017").setAuthInfoTimeout(3000L);
GetVideoPlayAuthResponse videoPlayAuth = client.getVideoPlayAuth(request);
System.out.println(videoPlayAuth.body.playAuth);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Z2lvbiI6ImNuLXNoZW56aGVuIiwiQ3VzdG9tZXJJZCI6MTQyNDA4MjY1MjI1NjI1NH0=",
"VideoMeta": {
"Status": "Normal",
"VideoId": "33fab662eec64e29826c36ef18eee017",
"Title": "6 - What If I Want to Move Faster.mp4",
"CoverURL": "http://outin-db30bbecf65111eb875e00163e021072.oss-cn-shenzhen.aliyuncs.com/33fab662eec64e29826c36ef18eee017/snapshots/af3b97324d544595a61356aefa78c01c-00001.jpg?Expires=1628225103&OSSAccessKeyId=LTAI4FocoL6tuCdYhuvug6Ee&Signature=V%2BnLyFcvcGi0rYyEo86VpJzhF6g%3D",
"Duration": 16.27669906616211
},
"RequestId": "19933336-2CDD-5BEC-BFFD-CEB8A1C52B7E"
}
获取播放地址(旧):
public static void getInfoVideo() throws ClientException {
DefaultAcsClient client = initVodClient("LTAI4G499pxUfMrAJ2TXuCJz", "et6OsY7VZMfZ84oKbJIjqP3TT7KTCO");
GetPlayInfoRequest request = new GetPlayInfoRequest();
request.setVideoId("33fab662eec64e29826c36ef18eee017");
GetPlayInfoResponse response = client.getAcsResponse(request);
// 开始解析response内的内容
List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
for (GetPlayInfoResponse.PlayInfo infoResponse : playInfoList){
System.out.println(infoResponse.getPlayURL()+"-----------------");
}
}
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
String regionId = "cn-shenzhen"; // 点播服务接入区域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
返回参数见:
上传播放视频:
流程:
导入依赖
<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.aliyun</groupId>
<artifactId>aliyun-java-vod-upload</artifactId>
<version>1.4.14</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>
测试
package com.wang.vodservice;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.*;
import com.aliyun.vod.upload.resp.*;
public class UploadVideo {
public static void main(String[] args_){
testUploadVideo();
}
private static void testUploadVideo() {
String accessKey = "LTAI4G499pxUfMrAJ2TXuCJz";
String sccessKeySecret = "et6OsY7VZMfZ84oKbJIjqP3TT7KTCO";
String fileTitle = "java入门";
String filename = "D:/11.mp4";
UploadVideoRequest request = new UploadVideoRequest(accessKey, sccessKeySecret, fileTitle, filename);
/* 可指定分片上传时每个分片的大小,默认为2M字节 */
request.setPartSize(2 * 1024 * 1024L);
/* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
request.setTaskNum(1);
/* 是否开启断点续传, 默认断点续传功能关闭。当网络不稳定或者程序崩溃时,再次发起相同上传请求,可以继续未完成的上传任务,适用于超时3000秒仍不能上传完成的大文件。
注意: 断点续传开启后,会在上传过程中将上传位置写入本地磁盘文件,影响文件上传速度,请您根据实际情况选择是否开启*/
//request.setEnableCheckpoint(false);
/* OSS慢请求日志打印超时时间,是指每个分片上传时间超过该阈值时会打印debug日志,如果想屏蔽此日志,请调整该阈值。单位: 毫秒,默认为300000毫秒*/
//request.setSlowRequestsThreshold(300000L);
/* 可指定每个分片慢请求时打印日志的时间阈值,默认为300s*/
//request.setSlowRequestsThreshold(300000L);
/* 是否显示水印(可选),指定模板组ID时,根据模板组配置确定是否显示水印*/
//request.setIsShowWaterMark(true);
/* 自定义消息回调设置(可选),参数说明参考文档 https://help.aliyun.com/document_detail/86952.html#UserData */
// request.setUserData("{\"Extend\":{\"test\":\"www\",\"localId\":\"xxxx\"},\"MessageCallback\":{\"CallbackURL\":\"http://test.test.com\"}}");
/* 视频分类ID(可选) */
request.setCateId(2559L);
/* 视频标签,多个用逗号分隔(可选) */
//request.setTags("标签1,标签2");
/* 视频描述(可选) */
//request.setDescription("视频描述");
/* 封面图片(可选) */
//request.setCoverURL("http://cover.sample.com/sample.jpg");
/* 模板组ID(可选) */
request.setTemplateGroupId("1e0d94edba7faf5f0c10764b81a0e380");
/* 工作流ID(可选) */
//request.setWorkflowId("d4430d07361f0*be1339577859b0177b");
/* 存储区域(可选) */
// request.setStorageLocation("vod.cn-shenzhen.aliyuncs.com");
/* 开启默认上传进度回调 */
//request.setPrintProgress(false);
/* 设置自定义上传进度回调 (必须继承 VoDProgressListener) */
//request.setProgressListener(new PutObjectProgressListener());
/* 设置您实现的生成STS信息的接口实现类*/
// request.setVoDRefreshSTSTokenListener(new RefreshSTSTokenImpl());
/* 设置应用ID*/
//request.setAppId("app-1000000");
/* 点播服务接入点 */
request.setApiRegionId("cn-shenzhen");
/* ECS部署区域*/
// request.setEcsRegionId("cn-shenzhen");
UploadVideoImpl uploader = new UploadVideoImpl();
UploadVideoResponse response = uploader.uploadVideo(request);
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
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");
}
}
}
手动上传maven仓库:
找到案例,打开lib里面的jar包
现在需要把aliyun-java-vod-upload-1.4.11.jar传到maven仓库中
在这个文件夹位置使用命令行
mvn install:install-file
-DgroupId=com.aliyun
-DartifactId=aliyun-sdk-vod-upload
-Dversion=1.4.11
-Dpackaging=jar
-Dfile=aliyun-java-vod-upload-1.4.11.jar
--------------------------------------------------------
mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.11 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.11.jar
删除播放视频:
public static void deleteVideo() throws ClientException {
DefaultAcsClient client = initVodClient("LTAI4G499pxUfMrAJ2TXuCJz", "et6OsY7VZMfZ84oKbJIjqP3TT7KTCO");
DeleteVideoRequest request = new DeleteVideoRequest();
request.setVideoIds("6a5529f8a4f14bb5b6b048e98b02f227,92f8c951d3e1401b9a50056e57a6faeb");
DeleteVideoResponse response = client.getAcsResponse(request);
System.out.print("RequestId = " + response.getRequestId() + "\n");
}