官网:阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台
依赖
版本:
<aliyun-java-sdk-core.version>4.3.3</aliyun-java-sdk-core.version>
<aliyun-sdk-oss.version>3.1.0</aliyun-sdk-oss.version>
<aliyun-java-sdk-vod.version>2.15.2</aliyun-java-sdk-vod.version>
<aliyun-java-vod-upload.version>1.4.11</aliyun-java-vod-upload.version>
<aliyun-sdk-vod-upload.version>1.4.11</aliyun-sdk-vod-upload.version>
//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
//将jar包down下来,然后在jar包的路径打开终端,输入命令,进行安装
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-kms</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
</dependencies>
#最大上传单个文件大小, 默认1M
spring.servlet.multipart.max-file-size=1024MB
#最大置总上传的数据大小, 默认10M
spring.servlet.multipart.max-request-size=1024MB文章来源:https://www.toymoban.com/news/detail-506234.htmlnginx支持上传大小也有限制,需要在配置文件中也需要添加大小设置
client_max_body_size 1024m;文章来源地址https://www.toymoban.com/news/detail-506234.html
//初始化对象
public class InitVodClient {
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;
}
}
根据视频ID删除视频
//初始化对象
DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.KEY_ID, ConstantVodUtils.KEY_SECRET);
//创建删除视频request对象
DeleteVideoRequest request = new DeleteVideoRequest();
//如果传的是list,就用这个方法变成一个字符串,用逗号隔开
//String videoIds = StringUtils.join(videoIdList.toArray(), ",");
//向request设置视频id(可以设置多个,删除多个,用逗号隔开) "VideoId1,VideoId2"
request.setVideoIds(id);
//调用初始化对象的方法实现
client.getAcsResponse(request);
流式上传
//通过前端传过来对应的文件进行上传
String accessKeyId = ConstantVodUtils.KEY_ID;
String accessKeySecret = ConstantVodUtils.KEY_SECRET;
String fileName = file.getOriginalFilename();
String title = fileName.substring(0,fileName.lastIndexOf("."));
InputStream inputStream = file.getInputStream();
UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
if (response.isSuccess()) {
System.out.print("VideoId=" + response.getVideoId() + "\n"); //视频id
} 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");
}
return response.getVideoId();
本地文件上传
String accessKeyId = "LTAI5tHwP4cop826HDn3WA9n";
String accessKeySecret = "SFWOWjMWJAGGQbPtPI147pmgsC29Ft";
String title = "测试本地上传"; //上传之后文件名称
String fileName = "C:\\Users\\lenovo\\Desktop\\学习linux\\在线教育--谷粒学院\\项目资料\\1-阿里云上传测试视频\\6 - What If I Want to Move Faster.mp4"; //本地文件路径和名称
//上传视频的方法
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"); //视频id
} 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");
}
根据视频ID获取视频播放凭证
//根据视频iD获取视频播放凭证
public static void getPlayAuth() throws ClientException {
String accessKeyId = "LTAI5tHwP4cop826HDn3WA9n";
String accessKeySecret = "SFWOWjMWJAGGQbPtPI147pmgsC29Ft";
//创建初始化对象
DefaultAcsClient defaultAcsClient = InitVodClient
.initVodClient(accessKeyId, accessKeySecret);
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
request.setVideoId("408c6d284da04844bd014c06a648bf76");
response = defaultAcsClient.getAcsResponse(request);
System.out.println("playAuth: " + response.getPlayAuth()); //播放凭证
}
根据视频id获取视频的播放地址
//根据视频id获取视频的播放地址
public static void getPlayUrl() throws ClientException {
String accessKeyId = "LTAI5tHwP4cop826HDn3WA9n";
String accessKeySecret = "SFWOWjMWJAGGQbPtPI147pmgsC29Ft";
//创建初始化对象
DefaultAcsClient defaultAcsClient = InitVodClient
.initVodClient(accessKeyId, accessKeySecret);
//创建获取视频地址request和response
GetPlayInfoRequest request = new GetPlayInfoRequest();
GetPlayInfoResponse response = new GetPlayInfoResponse();
//向request对象里面设置视频id
request.setVideoId("408c6d284da04844bd014c06a648bf76");
//调用初始化对象里面的方法,传递request,获取数据
response = defaultAcsClient.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");
System.out.print("RequestId = " + response.getRequestId() + "\n");
}
阿里云视频播放前端
<template>
<div>
<!-- 阿里云视频播放器样式 -->
<link rel="stylesheet" href="https://g.alicdn.com/de/prismplayer/2.8.1/skins/default/aliplayer-min.css" >
<!-- 阿里云视频播放器脚本 -->
<script charset="utf-8" type="text/javascript" src="https://g.alicdn.com/de/prismplayer/2.8.1/aliplayer-min.js" />
<!-- 定义播放器dom -->
<div id="J_prismPlayer" class="prism-player" />
</div>
</template>
<script>
import vod from '@/api/vod' //引入调用接口的js文件
export default {
layout: 'video',//应用video布局
asyncData({ params, error }) {
return vod.getPlayAuth(params.vid) //调用后端方法,根据视频id得到对应的视频播放凭证
.then(response => {
return {
playAuth: response.data.data.playAuth,
vid: params.vid
}
})
},
mounted() { //页面渲染之后 created
new Aliplayer({
id: 'J_prismPlayer',
vid: this.vid, // 视频id
playauth: this.playAuth, // 播放凭证
encryptType: '1', // 如果播放加密视频,则需设置encryptType=1,非加密视频无需设置此项
width: '100%',
height: '500px',
// 以下可选设置
cover: 'http://guli.shop/photo/banner/1525939573202.jpg', // 封面
qualitySort: 'asc', // 清晰度排序
mediaType: 'video', // 返回音频还是视频
autoplay: false, // 自动播放
isLive: false, // 直播
rePlay: false, // 循环播放
preload: true,
controlBarVisibility: 'hover', // 控制条的显示方式:鼠标悬停
useH5Prism: true, // 播放器类型:html5
}, function(player) {
console.log('播放器创建成功')
})
}
}
</script>
到了这里,关于阿里云视频点播相关代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!