1.简介:视频点播(ApsaraVideo for VoD)是集音视频采集、编辑、上传、自动化转码处理、媒体资源管理、分发加速于一体的一站式音视频点播解决方案。
2.视频点播的使用:
2.1引入相关依赖:阿里云上有也可以进行直接使用
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.6.0</version>
</dependency>
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-vod</artifactId><version>2.16.5</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.2AccessKey初始化
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;//填入AccessKey信息
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;
}
2.3进行视频上传功能,引入依赖,添加阿里云Java SDK、OSS SDK、视频点播服务端SDK、视频点播服务端上传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><dependency><groupId>com.aliyun.vod</groupId><artifactId>upload</artifactId><version>1.4.14</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/aliyun-java-vod-upload-1.4.14.jar</systemPath></dependency>
注:对于上传的jar包maven仓库中暂时还没有开放,所以需要自己加入maven仓库中,阿里云上可以免费下载。
2.4代码实现
public String uploadVideo(MultipartFile file) {//fileName:上传文件之前文件的名字String fileName = file.getOriginalFilename();//title:上传到阿里云后的视频名字String title = fileName.substring(0, fileName.lastIndexOf("."));//根据文件获取输入流InputStream inputStream = null;try {inputStream = file.getInputStream();} catch (IOException e) {e.printStackTrace();}UploadStreamRequest request = new UploadStreamRequest(keyId,keySecret, title, fileName, inputStream);// request.setEcsRegionId("cn-shanghai");UploadVideoImpl uploader = new UploadVideoImpl();UploadStreamResponse response = uploader.uploadStream(request);String videoId=null;if (response.isSuccess()) {videoId=response.getVideoId();} 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 videoId;}
2.5根据视频id远程删除阿里云中的视频
public void removeVideo(String videoId) {DeleteVideoRequest request = new DeleteVideoRequest();//支持传入多个视频ID,多个用逗号分隔request.setVideoIds(videoId);DefaultAcsClient defaultAcsClient = null;try {defaultAcsClient = InitObject.initVodClient(keyId,keySecret);} catch (ClientException e) {e.printStackTrace();}DeleteVideoResponse response = new DeleteVideoResponse();try {response = defaultAcsClient.getAcsResponse(request);} catch (Exception e) {System.out.print("ErrorMessage = " + e.getLocalizedMessage());}System.out.print("RequestId = " + response.getRequestId() + "\n");}