【Spring Boot】整合阿里云视频点播
作者:互联网
阿里云视频点播
准备工作
在进行整合前,先来看看视频点播要用的的几个基本功能,这里先用一个基本的maven项目来演示一下
1、创建maven项目,引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>aliyun-vod</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>sonatype-nexus-staging</id>
<name>Sonatype Nexus Staging</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<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</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
<version>1.4.13</version>
</dependency>
</dependencies>
</project>
这里需要注意一下aliyun-sdk-vod-upload,这个依赖可能下载失败,我们需要手动进行安装
去阿里云视频点播的文档中找就可以了
这里放出链接 --> SDK下载
将压缩包解压,进入解压后的lib目录
在此处打开cmd,执行下面的命令
mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.13 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.13.jar
安装成功后,重新加载maven依赖即可
2、初始化
package com.example;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
/**
* @author martin
* @date 2021/2/5
**/
public class InitObject {
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
String regionId = "cn-shanghai";
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
return new DefaultAcsClient(profile);
}
}
3、获取视频的播放地址
package com.example;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import java.util.List;
/**
* @author martin
* @date 2021/2/5
**/
// 根据视频id获取视频的播放地址
public class GetVideoSrc {
public static void main(String[] args) throws Exception{
// 创建初始化对象
DefaultAcsClient client = InitObject.initVodClient("", "");
// 创建获取视频地址的request和response
GetPlayInfoRequest request = new GetPlayInfoRequest();
GetPlayInfoResponse response;
// 向request对象里面设置视频id
request.setVideoId("");
// 调用初始化对象里面的方法,传递request,获取数据
response = client.getAcsResponse(request);
List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
// 播放地址
for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
System.out.println("PlayInfo.PlayURL = " + playInfo.getPlayURL());
}
// 视频标题
System.out.println("Title = " + response.getVideoBase().getTitle());
}
}
4、获取视频的播放凭证
package com.example;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
/**
* @author martin
* @date 2021/2/5
**/
// 根据视频id获取视频的播放凭证
public class GetVideoAuth {
public static void main(String[] args) throws Exception{
// 创建初始化对象
DefaultAcsClient client = InitObject.initVodClient("", "");
// 创建获取视频凭证的request和response
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse response;
// 设置视频id
request.setVideoId("");
// 调用client中的方法,得到视频凭证
response = client.getAcsResponse(request);
System.out.println(response.getPlayAuth());
}
}
5、上传视频
package com.example;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
/**
* @author martin
* @date 2021/2/5
**/
public class UploadVideoTest {
public static void main(String[] args) {
String accessKeyId = "";
String accessKeySecret = "";
String title = "What If I Want to Move Faster";
String fileName = "D:\\Project\\guli\\testData\\What If I Want to Move Faster.mp4";
UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
request.setPartSize(2 * 1024 * 1024L);
request.setTaskNum(1);
request.setEnableCheckpoint(false);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadVideoResponse response = uploader.uploadVideo(request);
if (response.isSuccess()) {
System.out.println(response.getVideoId());
} else {
System.out.println(response.getVideoId());
System.out.println(response.getCode());
System.out.println(response.getMessage());
}
}
}
以上是一些基本的功能,更多的功能可以查看官方文档
!!!
注意:依赖中的各个版本必须严格按照官方文档中的写,不然会出现上传的视频一直转圈的现象
具体实现
1、创建Spring Boot工程,并引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lyh</groupId>
<artifactId>vod</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>vod</name>
<description>Spring Boot 整合阿里云视频点播</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
<version>1.4.13</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>sonatype-nexus-staging</id>
<name>Sonatype Nexus Staging</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、配置文件
3、Service
package com.lyh.service.impl;
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.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.lyh.service.VodService;
import com.lyh.utils.ConstantVodUtils;
import com.lyh.utils.InitClient;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.UUID;
/**
* @author martin
* @date 2021/2/6
**/
@Service
public class VodServiceImpl implements VodService {
@Override
public String uploadVideo(MultipartFile file) {
try {
String fileName = file.getOriginalFilename();
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
fileName = uuid + fileName;
String title = fileName.substring(0, fileName.lastIndexOf("."));
InputStream in = file.getInputStream();
UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, in);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
return response.getVideoId();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public String getVideoAuth(String videoId) {
try {
DefaultAcsClient client = InitClient.init(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
request.setVideoId(videoId);
GetVideoPlayAuthResponse response = client.getAcsResponse(request);
return response.getPlayAuth();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
4、controller
package com.lyh.controller;
import com.lyh.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
/**
* @author martin
* @date 2021/2/6
**/
@Controller
public class VodController {
@Autowired
private VodService vodService;
@GetMapping("/")
public String indexHandler() {
return "index";
}
@PostMapping("/upload")
public String upload(@RequestParam(value = "file") MultipartFile file, Model model) {
String videoId = vodService.uploadVideo(file);
String videoAuth = vodService.getVideoAuth(videoId);
model.addAttribute("videoId", videoId);
model.addAttribute("videoAuth", videoAuth);
return "play";
}
}
效果展示
完整代码
完整代码已经上传至Github
标签:vod,String,Spring,org,Boot,aliyun,import,视频点播,com 来源: https://blog.csdn.net/m0_54468192/article/details/113703730