阿里云视频点播
前言
- 参考
- 参考
实现代码
-
搜索视频点播并开通服务
-
启用服务
-
获取AccessKeyId和AccessKey Secret
-
下载sdk
-
解压后再lib文件夹打开cmd执行如下命令
mvn install:install-file -Dfile=aliyun-java-vod-upload-1.4.14.jar -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.14 -Dpackaging=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
# 注意,版本需要与自己的jar版本一致
- 构建1个spring boot项目,导入依赖
com.aliyun
aliyun-java-sdk-core
4.5.1
com.aliyun.oss
aliyun-sdk-oss
3.10.2
com.aliyun
aliyun-java-sdk-vod
2.15.11
com.alibaba
fastjson
1.2.28
org.json
json
20170516
com.google.code.gson
gson
2.8.2
com.aliyun
aliyun-sdk-vod-upload
1.4.11
joda-time
joda-time
2.10.10
org.springframework.boot
spring-boot-starter-web
2.6.1
org.projectlombok
lombok
1.18.20
provided
org.springframework.boot
spring-boot-starter-test
2.6.1
test
- 编写配置文件
# 服务端口
server.port=8003
# 服务名
spring.application.name=service-vod
#阿里云 vod
#不同的服务器,地址不同
aliyun.vod.file.keyid=LTAI5trEnbqdU69Pd9cge8uc61de2A
aliyun.vod.file.keysecret=WoR326ScwdHY345s44zO3jgKg2N5zXs8RGK
# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB
- 编写启动类
@SpringBootApplication
@ComponentScan(basePackages={"com.ychen"})
public class Demo02Application {
public static void main(String[] args) {
SpringApplication.run(Demo02Application.class, args);
}
}
- 编写工具类
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConstantPropertiesUtil 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;
}
}
- 编写工具类
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
public class AliyunVodSDKUtils {
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;
}
}
- 编写业务层接口
public interface VodService {
String uploadVideo(MultipartFile file);
void removeVideo(String videoId);
}
- 编写业务层实现类
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.utils.StringUtils;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.ychen.vod.util.AliyunVodSDKUtils;
import com.ychen.vod.util.ConstantPropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
@Service
public class VideoServiceImpl implements VodService {
@Override
public String uploadVideo(MultipartFile file) {
try {
InputStream inputStream = file.getInputStream();
String originalFilename = file.getOriginalFilename();
String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
UploadStreamRequest request = new UploadStreamRequest(
ConstantPropertiesUtil.ACCESS_KEY_ID,
ConstantPropertiesUtil.ACCESS_KEY_SECRET,
title, originalFilename, inputStream);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
//如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。
// 其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
String videoId = response.getVideoId();
if (!response.isSuccess()) {
String errorMessage = "阿里云上传错误:" + "code:" + response.getCode() + ", message:" + response.getMessage();
if(StringUtils.isEmpty(videoId)){
System.out.println("错误");
// throw new GuliException(20001, errorMessage);
}
}
return videoId;
} catch (IOException e) {
System.out.println("错误");
// throw new GuliException(20001, "guli vod 服务上传失败");
}
return null;
}
@Override
public void removeVideo(String videoId) {
try{
DefaultAcsClient client = AliyunVodSDKUtils.initVodClient("LTAI5tEnsbqU69Pdd9ce8u61e2A", "WoR26ScwdHY34s5s4zOe3jegK2Nd5zwX8RGK");
DeleteVideoRequest request = new DeleteVideoRequest();
request.setVideoIds(videoId);
DeleteVideoResponse response = client.getAcsResponse(request);
System.out.print("RequestId = " + response.getRequestId() + "\n");
}catch (ClientException e){
System.out.println("错误");
// throw new GuliException(20001, "视频删除失败");
}
}
}
- 控制层
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.ychen.vod.service.VodService;
import com.ychen.vod.util.AliyunVodSDKUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@RestController
public class VodController {
@Autowired
private VodService videoService;
/**
* 上传
* @param file
* @return
* @throws Exception
*/
@PostMapping("upload")
public String uploadVideo(
@RequestParam("file") MultipartFile file) throws Exception {
String videoId = videoService.uploadVideo(file);
return videoId;
}
/**
* 获取播放凭证
*/
@GetMapping("testGetVideoPlayAuth")
public String testGetVideoPlayAuth() throws Exception {
//初始化客户端、请求对象和相应对象
DefaultAcsClient client = AliyunVodSDKUtils.initVodClient("LTAI5tEnsbqU69Pdd9ce8u61e2A", "WoR26ScwdHY34s5s4zOe3jegK2Nd5zwX8RGK");
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
try {
//设置请求参数 1632ec0f8d524412bb81c43caaa085b0
request.setVideoId("1632ec0f8d524412bb81c43caaa085b0");
//获取请求响应
response = client.getAcsResponse(request);
//输出请求结果
//播放凭证
System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
//VideoMeta信息
System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
return "success";
}
/**
* 获取播放地址
* @return
* @throws Exception
*/
@GetMapping("testGetPlayInfo")
public String testGetPlayInfo() throws Exception {
//初始化客户端、请求对象和相应对象
DefaultAcsClient client = AliyunVodSDKUtils.initVodClient("LTAI5tEnsbqU69Pdd9ce8u61e2A", "WoR26ScwdHY34s5s4zOe3jegK2Nd5zwX8RGK");
GetPlayInfoRequest request = new GetPlayInfoRequest();
GetPlayInfoResponse response = new GetPlayInfoResponse();
try {
//设置请求参数
//注意:这里只能获取非加密视频的播放地址
request.setVideoId("1632ec0f8d524412bb81c43caaa085b0");
//获取请求响应
response = client.getAcsResponse(request);
//输出请求结果
List 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");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
return "success";
}
@DeleteMapping("{videoId}")
public String removeVideo(@PathVariable String videoId){
videoService.removeVideo(videoId);
return "success";
}
}
-
测试上传
-
测试获取视频播放凭证
# 控制台打印如下
PlayAuth = eyJTZWN1cml0eVRva2VuIjo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 女 主
RequestId = 53499E5F-54EA-551B-BEA9-3211AE80259A
- 测试获取视频播放地址
# 控制台打印如下
PlayInfo.PlayURL = https://outin-29ce34d5bec411ec935300163e1c7426.oss-cn-shanghai.aliyuncs.com/sv/4e814426-1803b2d1940/4e814426-1803b2d1940.flv?Expires=1650264075&OSSAccessKeyId=LTAI8bKSZ6dKjf44&Signature=DZs%2F4BvcTxjz4LNIUEEFuqbqmaw%3D
VideoBase.Title = 路 人 女 主
RequestId = E46B4BE4-CF41-58CE-8DEB-8242F80C9D65
- 测试删除视频