一、阿里云视频点播技术能力盘点
参考文章:
https://blog.csdn.net/qq_33857573/article/details/79564255
视频点播(ApsaraVideo for VoD)是集音视频采集、编辑、上传、自动化转码处理、媒体资源管理、分发加速于一体的一站式音视频点播解决方案。
1、应用场景
- 音视频网站:无论是初创视频服务企业,还是已拥有海量视频资源,可定制化的点播服务帮助客户快速搭建拥有极致观看体验、安全可靠的视频点播应用。
- 短视频:集音视频拍摄、特效编辑、本地转码、高速上传、自动化云端转码、媒体资源管理、分发加速、播放于一体的完整短视频解决方案。目前已帮助1000+APP快速实现手机短视频功能。
- 直播转点播:将直播流同步录制为点播视频,用于回看。并支持媒资管理、媒体处理(转码及内容审核/智能首图等AI处理)、内容制作(云剪辑)、CDN分发加速等一系列操作。
- 在线教育:为在线教育客户提供简单易用、安全可靠的视频点播服务。可通过控制台/API等多种方式上传教学视频,强大的转码能力保证视频可以快速发布,覆盖全网的加速节点保证学生观看的流畅度。防盗链、视频加密等版权保护方案保护教学内容不被窃取。
- 视频生产制作:提供在线可视化剪辑平台及丰富的OpenAPI,帮助客户高效处理、制作视频内容。除基础的剪切拼接、混音、遮标、特效、合成等一系列功能外,依托云剪辑及点播一体化服务还可实现标准化、智能化剪辑生产,大大降低视频制作的槛,缩短制作时间,提升内容生产效率。
- 内容审核:应用于短视频平台、传媒行业审核等场景,帮助客户从从语音、文字、视觉等多维度精准识别视频、封面、标题或评论的违禁内容进行AI智能审核与人工审核。
2、功能介绍
二、视频点播概念
三、视频点播SDK使用
可以参考:https://help.aliyun.com/document_detail/57756.html?spm=a2c4g.11186623.6.1037.10917c44lVFZgG
前提条件
- 确认已注册阿里云账号,完成实名认证,并开通了视频点播服务。
- 准备好访问点播服务使用的Access Key。可以在阿里云Access Key管理创建主账号Access Key,也可以在RAM访问控制台创建RAM用户,并授予VOD权限后进行相应操作,请参见创建RAM用户并授权。
1、导入maven坐标
<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.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>
2、初始化操作,创建DefaultAcsClient对象
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;
}
3、获取视频播放地址 (根据视频id获取到)
因为上传视频可以进行加密,加密之后,使用加密之后地址不能进行视频播放,在数据库存储不存地址,而是存储 视频id
package com.atguigu.vodtest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import java.util.List;
/**
* @Author panghl
* @Date 2021/1/26 23:03
**/
public class TestMain {
public static void main(String[] args) throws ClientException {
//1.根据视频ID 获取视频播放地址
//创建初始化对象
DefaultAcsClient defaultAcsClient = InitObject.initVodClient(accessKeyId,accessKeySecret);
//创建获取视频地址request和response
GetPlayInfoRequest request = new GetPlayInfoRequest();
GetPlayInfoResponse response = new GetPlayInfoResponse();
//向request对象里面设置视频id
request.setVideoId("94838e220a114ae5b3982a7f87cc6623");
//调用初始化对象里面的方法,传递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");
}
}
4、获取视频播放凭证(根据视频id获取到)
public static void main(String[] args) throws ClientException {
//根据视频id获取视频播放凭证
//创建初始化对象
DefaultAcsClient defaultAcsClient = InitObject.initVodClient(accessKeyId, accessKeySecret);
//创建获取视频凭证request和response
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
//向request设置视频id
request.setVideoId("94838e220a114ae5b3982a7f87cc6623");
//调用初始化对象的方法得到凭证
response = defaultAcsClient.getAcsResponse(request);
System.out.println("playauth: "+response.getPlayAuth());
}
5、上传视频到阿里云视频点播服务
依赖引入(重点,因为maven中央仓库是没有的!)
mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.11 -Dpackaging -Dfile=aliyun-java-vod-upload-1.4.11.jar
下载SDK:
可参考我另一篇文章:https://blog.csdn.net/qq_45441466/article/details/111658420
我在这个链接里面分享了jar包和视频下载的示例代码:
链接:https://pan.baidu.com/s/1aO3wtpqkjwbsCXj2Eu3K4A
提取码:y4ap
下载jar到本地仓库另一种方式:
在下载的lib文件夹下开启cmd终端执行(示例代码中也有lib文件夹一模一样的):
mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.11 -Dpackaging -Dfile=aliyun-java-vod-upload-1.4.11.jar
上传视频测试代码:
package com.atguigu.vodtest;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
/**
* @Author panghl
* @Date 2021/1/27 8:26
* @Description TODO
**/
public class TestVod {
public static void main(String[] args) {
String accessKeyId = "LTAI4Fy4B5qEoJAz2u3xgCdn";
String accessKeySecret = "7GBKLg0gzBhF37zH9o6KtrrFbqQxPa";
String title = "6 - What If I Want to Move Faster - upload by sdk";//上传之后的文件名称
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,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
request.setTaskNum(1);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadVideoResponse response = uploader.uploadVideo(request);
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");
}
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
}
}
6、整合后端代码
1、导入对应的依赖(前面有提到)
2、创建application配置文件
#服务器端口
server:
port: 8003
#服务名
spring:
application:
name: service-vod
#环境设置: dev 、test、 prod
profiles:
active: dev
#阿里云 vod
aliyun:
vod:
file:
keyid: your keyId
keysecret: your keysecret
3、创建启动类
package com.atguigu.vod;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* @Author panghl
* @Date 2021/1/27 23:41
**/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages = {"com.atguigu"})
public class VodApplication {
public static void main(String[] args) {
SpringApplication.run(VodApplication.class,args);
}
}
4、创建controller 、service
package com.atguigu.vod.controller;
import com.atguigu.common.R;
import com.atguigu.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* @Author panghl
* @Date 2021/1/27 23:46
* @Description TODO
**/
@RestController
@RequestMapping("/eduvod/video/")
@Api(tags = {"阿里云vod"})
public class VodController {
@Autowired
private VodService vodService;
@ApiOperation(value = "上传视频到阿里云")
@PostMapping("uploadAlyVideo")
public R uploadAlyVideo(MultipartFile file){
//返回上传视频id
String videoId = vodService.uploadVideoAly(file);
return R.ok().data("videoId",videoId);
}
}
package com.atguigu.vod.service;
import org.springframework.web.multipart.MultipartFile;
/**
* @Author panghl
* @Date 2021/1/27 23:47
* @Version 1.0
* @Description TODO
**/
public interface VodService {
/**
* 上传视频
* @param file
* @return
*/
String uploadVideoAly(MultipartFile file);
}
package com.atguigu.vod.service.impl;
import com.aliyun.vod.upload.impl.UploadImageImpl;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadImageRequest;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadImageResponse;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantPropertiesUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
/**
* @Author panghl
* @Date 2021/1/27 23:47
* @Description TODO
**/
@Service
public class VodServiceImpl implements VodService {
@Override
public String uploadVideoAly(MultipartFile file) {
//accessKeyId, accessKeySecret
//title : 上传之后显示名称
//fileName : 上传文件原始名称
//inputStream : 文件上传输入流
String fileName = file.getOriginalFilename();
String title = fileName.substring(0,fileName.lastIndexOf("."));
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
UploadStreamRequest request =
new UploadStreamRequest(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET,
title, fileName, inputStream);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
String videoId = null;
if (response.isSuccess()) {
videoId = response.getVideoId();
} else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
videoId = response.getVideoId();
}
return videoId;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
package com.atguigu.vod.utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Author panghl
* @Date 2021/1/27 23:58
* @Description 读取常量
**/
@Component
public class ConstantPropertiesUtils 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;
}
}
上面代码运行出错,上传大小问题:org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
解决办法:
在application进行配置:
spring:
servlet:
multipart:
# 最大上传单个文件大小:默认1M
max-file-size: 1024MB
# 最大置总上传的数据大小: 默认10M
max-request-size: 1024MB
上面介绍了视频上传,接下来看视频的删除:
删除阿里云视频接口
controller:
@ApiOperation(value = "根据视频id删除阿里云视频")
@DeleteMapping("/removeAlyVideo/{id}")
public R removeAlyVideo(@PathVariable("id") String id){
//返回上传视频id
try{
vodService.removeAlyVideo(id);
return R.ok();
}catch (Exception e){
return R.error();
}
}
service:
/**
* 删除视频
* @param id
*/
void removeAlyVideo(String id);
@Override
public void removeAlyVideo(String id) {
//创建删除视频request对象
DeleteVideoRequest request = new DeleteVideoRequest();
DefaultAcsClient client = null;
try {
request.setVideoIds(id);
//初始化对象
client = InitVodObject.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID,ConstantPropertiesUtils.ACCESS_KEY_SECRET);
//调用初始化对象的方法实现删除
client.getAcsResponse(request);
} catch (ClientException e) {
e.printStackTrace();
throw new ELeanExeception(20001,"删除视频失败");
}
}
初始化对象InitVodObject:
package com.atguigu.vod.utils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
/**
* @Author panghl
* @Date 2021/1/28 22:59
**/
public class InitVodObject {
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;
}
}