赞
踩
视频点播(ApsaraVideo for VoD)是集音视频采集、编辑、上传、自动化转码处理、媒体资源管理、分发加速于一体的一站式音视频点播解决方案。
1、应用场景
音视频网站:无论是初创视频服务企业,还是已拥有海量视频资源,可定制化的点播服务帮助客户快速搭建拥有极致观看体验、安全可靠的视频点播应用。
短视频:集音视频拍摄、特效编辑、本地转码、高速上传、自动化云端转码、媒体资源管理、分发加速、播放于一体的完整短视频解决方案。目前已帮助1000+APP快速实现手机短视频功能。
直播转点播:将直播流同步录制为点播视频,用于回看。并支持媒资管理、媒体处理(转码及内容审核/智能首图等AI处理)、内容制作(云剪辑)、CDN分发加速等一系列操作。
在线教育:为在线教育客户提供简单易用、安全可靠的视频点播服务。可通过控制台/API等多种方式上传教学视频,强大的转码能力保证视频可以快速发布,覆盖全网的加速节点保证学生观看的流畅度。防盗链、视频加密等版权保护方案保护教学内容不被窃取。
视频生产制作:提供在线可视化剪辑平台及丰富的OpenAPI,帮助客户高效处理、制作视频内容。除基础的剪切拼接、混音、遮标、特效、合成等一系列功能外,依托云剪辑及点播一体化服务还可实现标准化、智能化剪辑生产,大大降低视频制作的槛,缩短制作时间,提升内容生产效率。
内容审核:应用于短视频平台、传媒行业审核等场景,帮助客户从从语音、文字、视觉等多维度精准识别视频、封面、标题或评论的违禁内容进行AI智能审核与人工审核。
2、功能介绍
3、整体流程
使用视频点播实现音视频上传、存储、处理和播放的整体流程如下:
参考地址:https://help.aliyun.com/document_detail/53406.html
1.配置pom.xml文件,引入依赖
<!--阿里云视频点播上传--> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.30</version> </dependency> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.13.2</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-vod</artifactId> <version>2.15.12</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.78</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.6</version> </dependency>
2、安装非开源jar包 aliyun-java-vod-upload-1.4.14.jar
aliyun-java-vod-upload-1.4.14.jar
在本地Maven仓库中安装jar包:
下载视频上传SDK,解压,命令行进入lib目录,执行以下代码
mvn install:install-file -DgroupId=com.aliyun
-DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.14 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.14.jar
3. 编写VideoController.java
package com.xiaogui.video.controller; import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse; import com.xiaogui.security.common.results.ResultEnum; import com.xiaogui.security.common.results.ResultResponse; import com.xiaogui.video.service.VideoService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.List; /** * @author PC */ @RestController @RequestMapping("video") @CrossOrigin @Api(value = "阿里云视频点播", tags = "阿里云视频点播") @Slf4j public class VideoController { @Autowired private VideoService videoService; @PostMapping("uploadAlyiVideo") @ApiOperation(value = "上传阿里云视频", notes = "上传阿里云视频") public ResultResponse uploadAlyiVideo(@RequestPart("file") MultipartFile file) { //返回上传视频id String videoId = videoService.uploadVideoAly(file); return ResultResponse.of(videoId, ResultEnum.SUCCESS,true); } /** * 根据视频id删除阿里云视频 */ @DeleteMapping("removeAlyVideo/{id}") @ApiOperation(value = "根据视频id删除阿里云视频", notes = "根据视频id删除阿里云视频") public ResultResponse removeAlyVideo(@PathVariable String id) { return videoService.removeAlyVideo(id); } /** * 删除多个阿里云视频的方法 * 参数多个视频id List videoIdList */ @DeleteMapping("deleteBatch") @ApiOperation(value = "删除多个阿里云视频", notes = "删除多个阿里云视频") public ResultResponse deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) { videoService.removeMoreAlyVideo(videoIdList); return ResultResponse.of(null,ResultEnum.SUCCESS,true); } /** * 视频id获取视频播放凭证 */ @GetMapping("getVideoPlayAuth/{id}") @ApiOperation(value = "获取视频播放凭证", notes = "获取视频播放凭证") public ResultResponse getVideoPlayAuth(@PathVariable String id) { String playAuth = videoService.getVideoPlayAuth(id); return ResultResponse.of(playAuth,ResultEnum.SUCCESS,true); } /** * 视频id获取视频播放地址 */ @GetMapping("getPlayInfo/{id}") @ApiOperation(value = "视频id获取视频播放地址", notes = "视频id获取视频播放地址") public ResultResponse getPlayInfo(@PathVariable String id) { List<GetPlayInfoResponse.PlayInfo> playUrl = videoService.getPlayInfo(id); return ResultResponse.of(playUrl,ResultEnum.SUCCESS,true); } }
编写 VideoService.java
package com.xiaogui.video.service; 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.vod.model.v20170321.*; import com.xiaogui.security.common.results.ResultEnum; import com.xiaogui.security.common.results.ResultResponse; import com.xiaogui.video.utils.ConstantVideoUtils; import com.xiaogui.video.utils.InitVideoClient; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * @author PC */ @Service @Slf4j public class VideoService { /** *上传阿里云视频 */ public String uploadVideoAly(MultipartFile file) { String videoId = null; try { //accessKeyId, accessKeySecret //fileName:上传文件原始名称 获取视频文件原始名称 String fileName = file.getOriginalFilename(); //title:上传之后显示名称 去除视频后缀 assert fileName != null; String title = fileName.substring(0, fileName.lastIndexOf(".")); //inputStream:上传文件输入流 InputStream inputStream = file.getInputStream(); UploadStreamRequest request = new UploadStreamRequest(ConstantVideoUtils.ACCESS_KEY_ID, ConstantVideoUtils.ACCESS_KEY_SECRET,title,fileName,inputStream); UploadVideoImpl uploader = new UploadVideoImpl(); UploadStreamResponse response = uploader.uploadStream(request); if (response.isSuccess()) { videoId = response.getVideoId(); } else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。 // 其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 String errorMessage = "阿里云上传错误:" + "code:" + response.getCode() + ", message:" + response.getMessage(); log.warn(errorMessage); } }catch(Exception e) { e.printStackTrace(); } return videoId; } /** *根据视频id删除阿里云视频 */ public ResultResponse removeAlyVideo(String id) { try { //初始化对象 DefaultAcsClient client = InitVideoClient.initVideoClient( ConstantVideoUtils.ACCESS_KEY_ID, ConstantVideoUtils.ACCESS_KEY_SECRET); //创建删除视频request对象 DeleteVideoRequest request = new DeleteVideoRequest(); //向request设置视频id request.setVideoIds(id); //调用初始化对象的方法实现删除 DeleteVideoResponse acsResponse = client.getAcsResponse(request); return ResultResponse.of(acsResponse.getNonExistVideoIds(), ResultEnum.SUCCESS,true); }catch(Exception e) { e.printStackTrace(); log.error("删除视频失败异常"); return ResultResponse.failed(ResultEnum.FAILURE,true); } } /** *删除多个阿里云视频的 */ public void removeMoreAlyVideo(List<String> videoIdList) { try { //初始化对象 DefaultAcsClient client = InitVideoClient.initVideoClient( ConstantVideoUtils.ACCESS_KEY_ID, ConstantVideoUtils.ACCESS_KEY_SECRET); //创建删除视频request对象 DeleteVideoRequest request = new DeleteVideoRequest(); //videoIdList值转换成 1,2,3格式 String videoIds = StringUtils.join(videoIdList.toArray(), ","); //向request设置视频id request.setVideoIds(videoIds); //调用初始化对象的方法实现删除 client.getAcsResponse(request); }catch(Exception e) { e.printStackTrace(); log.error("删除视频失败异常"); } } /** * 视频id获取视频播放凭证 */ public String getVideoPlayAuth(String id) { try { //初始化客户端、请求对象和相应对象 DefaultAcsClient client = InitVideoClient.initVideoClient( ConstantVideoUtils.ACCESS_KEY_ID, ConstantVideoUtils.ACCESS_KEY_SECRET); GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest(); GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse(); //设置请求参数 request.setVideoId(id); //获取请求响应 response = client.getAcsResponse(request); //输出请求结果 //播放凭证 String playAuth = response.getPlayAuth(); return playAuth; }catch (Exception e){ log.error("ErrorMessage = " + e.getLocalizedMessage()); return null; } } /** * 视频id获取视频播放地址 */ public List<GetPlayInfoResponse.PlayInfo> getPlayInfo(String id) { try { //初始化客户端、请求对象和相应对象 DefaultAcsClient client = InitVideoClient.initVideoClient( ConstantVideoUtils.ACCESS_KEY_ID, ConstantVideoUtils.ACCESS_KEY_SECRET); GetPlayInfoRequest request = new GetPlayInfoRequest(); GetPlayInfoResponse response = new GetPlayInfoResponse(); //设置请求参数 request.setVideoId(id); //获取请求响应 response = client.getAcsResponse(request); //输出请求结果 //输出请求结果播放地址 return response.getPlayInfoList(); }catch (Exception e){ log.error("ErrorMessage = " + e.getLocalizedMessage()); return null; } } }
编写application.properties或application.yml文件
#阿里云 视频点播
#不同的服务器,地址不同
aliyun.video.file.keyid=your accessKeyId
aliyun.video.file.keysecret=your accessKeySecret
# 最大上传单个文件大小:默认1M 设置为1G
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB
编写视频点播常量工具类 ConstantVideoUtils.java
和初始化视频客户端工具类InitVideoClient.java
package com.xiaogui.video.utils; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * 视频点播常量类 * @author PC */ @Component public class ConstantVideoUtils implements InitializingBean { @Value("${aliyun.video.file.keyid}") private String keyId; @Value("${aliyun.video.file.keysecret}") private String keySecret; /** * 定义公开静态常量 */ public static String ACCESS_KEY_SECRET; public static String ACCESS_KEY_ID; @Override public void afterPropertiesSet() throws Exception { ACCESS_KEY_ID = keyId; ACCESS_KEY_SECRET = keySecret; } }
package com.xiaogui.video.utils; import com.aliyun.oss.ClientException; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.profile.DefaultProfile; /** * @author PC * 初始化AccessKey */ public class InitVideoClient { public static DefaultAcsClient initVideoClient(String accessKeyId, String accessKeySecret) throws ClientException { // 点播服务接入区域 String regionId = "cn-shanghai"; DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); return new DefaultAcsClient(profile); } }
阿里云视频播放器见下章
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。