OSS 相当于硬盘、便宜容量大,存储不经常使用的文件。
官网介绍: https://help.aliyun.com/document_detail/31817.html?spm=a2c4g.11174359.6.544.51ea1ea9aXOD2K
视频点播相当于存储视频文件的CND,集鉴权、播放、管理于一体的播放系统。
官网介绍: https://help.aliyun.com/document_detail/51236.html?spm=a2c4g.11186623.6.542.1b97152aQrphzw
与阿里云合作,视频媒资量过于巨大,无法通过阿里云平台上传,只能通过寄OSS硬盘拷贝,然后将OSS寄回阿里,编写OSS迁移到视频点播的代码上传即可。
文档信息
读文件: https://help.aliyun.com/video_detail/39691.html?spm=5176.10695662.1996646101.searchclickresult.2f2d36b9m7F1K4
写文件: https://help.aliyun.com/document_detail/64148.html?spm=a2c4g.11186623.6.1066.42976a58M4A9av」
<!-- 阿里云同步开始 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.15.2</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.konka.util</groupId>
<artifactId>aliyun-java-vod-upload</artifactId>
<version>1.4.12</version>
</dependency>
<!-- 阿里云同步结束 -->
<!-- excel处理 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
1 确保OSS和视频点播和ECS(自己的云机器)在一个局域网内、走内网不消耗网络流量。
1 将OSS文件批量查询、并下载到本地ECS服务器
2 将本地下载好的文件上传视频点播(注意配置好转码模板、分类信息等)
@Slf4j
@Service
public class UploadVideoServiceImpl implements UploadVideoService {
public final static List<Suplier> suplierlist = new ArrayList() {{
add(new Suplier("shulang", "供应商1", 8L));
add(new Suplier("jiaoyu", "供应商2", 19L));
add(new Suplier("huanggang", "供应商3", 6020L));
add(new Suplier("yang", "供应商4", 21L));
}};
@Override
public void upload(ABInDTO inDTO) {
Integer start = inDTO.getStart();
Integer end = inDTO.getEnd();
// 1 赛选所有
List<OSSObjectSummary> finds = OSSUploadVideoDemo.select();
log.info("查询总数为:{}", finds.size());
if (start == null) {
start = 0;
}
if (end == null) {
end = finds.size();
}
log.info("开始和结束的数量start:{},end:{}", start, end);
Integer count = 0;
// 2 遍历上传到视频点播
for (int i = 0; i < finds.size(); i++) {
// 过滤掉
if (start <= i && end >= i) {
log.info("开始第:{}个-区间共:{}-{}个,总共:{}", i, start, end, finds.size());
OSSObjectSummary s = finds.get(i);
try {
// 3 上传视频点播
log.info("\t-----------------------start");
log.info("\tgetKey " + s.getKey());
OSSUploadVideoDemo.upload(s.getKey());
log.info("\t上传视频点播完毕getBucketName " + s.getBucketName());
} catch (Exception e) {
log.error("上传视频点播出错了1");
log.info("\tgetKey " + s.getKey());
}
count++;
log.info("\t-----------------------end");
}
}
}
public static void main(String[] args) throws Exception {
OSSUploadVideoDemo.upload(null);
}
}
@Data
public class Suplier {
// OSS文件夹名称***
private String bucketName;
private String nickName; // 备注名称
private Long CateId; // 分类id
public Suplier(String bucketName, String nickName, Long cateId) {
this.bucketName = bucketName;
this.nickName = nickName;
CateId = cateId;
}
}
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.operlog.main.model.po.Suplier;
import com.operlog.main.utils.JavaExcelUtils;
import com.operlog.main.utils.JavaListUtils;
import com.operlog.utils.log.service.impl.UploadVideoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ji
* ji on 2020/4/28.
* 阿里云OSS视频下载
* https://help.aliyun.com/document_detail/84824.html?spm=a2c4g.11186623.6.800.45466328mEu67v
*/
@Slf4j
public class OSSUploadVideoDemo {
//账号AK信息请填写(必选) 子账号
public static final String accessKeyId = "";
//账号AK信息请填写(必选)
public static final String accessKeySecret = "";
// Bucket名称
public static final String bucketName = "kkypf-yixue-media";
// public static final String localUrl = "G:\\office\\test\\"; // 本地
public static final String localUrl = "/alidata/server/test/"; // 线上???
// Endpoint以上海为例,其它Region请按实际情况填写。
// public static final String endpoint = "http://oss-cn-shanghai.aliyuncs.com"; // 外网
public static final String endpoint = "http://oss-cn-shanghai-internal.aliyuncs.com"; // 内网 -UploadVideoDemo- request.setApiRegionId("cn-shanghai"); request.setEcsRegionId("cn-shanghai"); 要开放
// public static final String uploadEndpoint = "http://oss-cn-shanghai-internal.aliyuncs.com"; // 视频点播内网
/**
* 查询路径下所有文件
*/
public static List<OSSObjectSummary> select() {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
final int maxKeys = 1000;
String nextMarker = null;
ObjectListing objectListing;
Long count = 0l;
List<OSSObjectSummary> sumsOut = new ArrayList<>();
do {
objectListing = ossClient.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
sumsOut.addAll(sums);
count += sums.size();
log.info("最大个数\t" + sums.size());
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// 关闭OSSClient。
ossClient.shutdown();
log.info("总数为\t" + count);
return sumsOut;
}
/**
* OSS下载视频
*
* @param objectName 文件的目标名 例如: lang/人教版3年级下册(3起)-第100讲:Unit 2 My family.mp4
*/
public static void upload(String objectName) throws Exception {
log.info("上传开始objectName:{}", objectName);
// 1 OSS下载文件到本地
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String[] split = objectName.split("/");
// 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
String fileNameReal = split[split.length - 1];
String fileName = localUrl + fileNameReal;
log.info("fileName:{}", fileName);
File file = new File(fileName);
try {
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
} catch (Exception e) {
log.error("下载OSS出错了2objectName:{},msg:{}", objectName, e.getMessage());
}
ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, objectName), file);
// 关闭OSSClient。
ossClient.shutdown();
log.info("object.getCacheControl():{}", object.getCacheControl());
log.info("object.getContentDisposition():{}", object.getContentDisposition());
log.info("object.getContentEncoding():{}", object.getContentEncoding());
log.info("object.getContentMD5():{}", object.getContentMD5());
log.info("object.getETag():{}", object.getETag());
log.info("object.getRequestId():{}", object.getRequestId());
log.info("object.getServerSideEncryption():{}", object.getServerSideEncryption());
log.info("object():{}", object.toString());
// 筛选出是哪个供应商
Suplier suplierOut = new Suplier("新建分类zj默认1", "新建分类zj默认1", 1000136023L);
for (Suplier suplier : UploadVideoServiceImpl.suplierlist) {
if (objectName.contains(suplier.getBucketName())) {
suplierOut = suplier;
log.info("objectName:{},属于:{}", objectName, suplier.getNickName());
break;
}
}
// 2 本地上传到视频点播
// UploadVideoResponse uploadVideoResponse = UploadVideoDemo.testUploadVideo(accessKeyId, accessKeySecret, fileNameReal.substring(0, fileNameReal.lastIndexOf(".")), fileName);
UploadVideoResponse uploadVideoResponse = UploadVideoDemo.testUploadVideo(accessKeyId, accessKeySecret, fileNameReal, fileName, suplierOut.getCateId());
log.info("RequestId=" + uploadVideoResponse.getRequestId() + "\n");
log.info("VideoId=" + uploadVideoResponse.getVideoId() + "\n");
log.info("ErrorCode=" + uploadVideoResponse.getCode() + "\n");
log.info("ErrorMessage=" + uploadVideoResponse.getMessage() + "\n");
String[] stringOuts = null;
// 将文件写入表格中 String[] strings = {"1 第三方介质ID", "2 介质名称", "3 介质格式", "4 播放时长 (秒)", "5 分辨率", "6 播放地址", "7 requestId", "8 videoId", "9 code", "10 message", "11 供应商名称", "12 阿里云分类catId"};
if (uploadVideoResponse == null) {
stringOuts = new String[]{"", fileNameReal.substring(0, fileNameReal.lastIndexOf(".")),
fileNameReal.substring(fileNameReal.lastIndexOf("."), fileNameReal.length()), "4",
"5", "6",
"7", "8",
"9", "返回空指针"};
} else {
// 获取视频信息
GetVideoInfoResponse videoInfo = null; //
GetMezzanineInfoResponse mezzanineInfo = null;
String str4 = null;
String str5 = "1280*720"; // 默认
String str6 = "";
try {
videoInfo = getVideoInfo(uploadVideoResponse.getVideoId());
log.info("获取视频信息结束:{},videoInfo:{}", uploadVideoResponse.getVideoId(), videoInfo);
GetVideoInfoResponse.Video video = videoInfo.getVideo();
log.info("获取视频信息结束:{},videoInfo:{},video:{}", uploadVideoResponse.getVideoId(), videoInfo, video);
str4 = video.getDuration() + "";
str6 = video.getCoverURL();
mezzanineInfo = getMezzanineInfo(uploadVideoResponse.getVideoId());
log.info("查询到视频信息videoId:{},mezzanineInfo:{}", uploadVideoResponse.getVideoId(), mezzanineInfo);
GetMezzanineInfoResponse.Mezzanine mezzanine = mezzanineInfo.getMezzanine();
GetMezzanineInfoResponse.Mezzanine.VideoStream videoStream = mezzanine.getVideoStreamList().get(mezzanine.getVideoStreamList().size() - 1);
// str5 = mezzanine.getHeight() + "*" + mezzanine.getWidth();
str5 = videoStream.getHeight() + "*" + videoStream.getWidth();
} catch (Exception e) {
log.error("下载OSS出错了3objectName:{},msg:{}", objectName, e.getMessage());
}
stringOuts = new String[]{uploadVideoResponse.getVideoId(), fileNameReal.substring(0, fileNameReal.lastIndexOf(".")),
fileNameReal.substring(fileNameReal.lastIndexOf(".") + 1, fileNameReal.length()), str4,
str5, str6,
uploadVideoResponse.getRequestId(), uploadVideoResponse.getVideoId(),
uploadVideoResponse.getCode(), uploadVideoResponse.getMessage()};
}
// 3 将信息汇总-写入excel
try {
log.info("写入excel开始objectName:{}", objectName);
List<String[]> list1 = new ArrayList<String[]>();
list1.add(stringOuts);
JavaExcelUtils.addOneLine(list1, JavaExcelUtils.TableName.YIXUE);
log.info("写入excel完毕objectName:{},stringOuts:{}", objectName, stringOuts);
} catch (Exception e) {
log.error("写入excel出错了4objectName:{},list1:{},msg:{}", objectName, stringOuts, e.getMessage());
}
log.info("上传完毕objectName:{}", objectName);
log.info("写入excel完毕stringOuts:{}", stringOuts.toString());
}
/**
* OSS下载视频
*
* @param suplier 供应商
*/
public static void getPalyInfoToExcel(Suplier suplier) throws Exception {
log.info("getPalyInfoToExcel开始suplier:{}", suplier);
Long catId = suplier.getCateId();
// 1 获取分类下面的所有视频
List<GetVideoListResponse.Video> videoListAll = null;
try {
videoListAll = UploadVideoDemo.getVideoListAll(catId);
} catch (Exception e) {
e.printStackTrace();
}
if (JavaListUtils.isEmpty(videoListAll)) {
System.out.println("videoListAll.size():0");
return;
}
System.out.println("videoListAll.size():" + videoListAll.size());
// 2 获取视频详情
List<GetPlayInfoResponse> getPlayInfoResponses = new ArrayList<>();
for (GetVideoListResponse.Video video : videoListAll) {
try {
GetPlayInfoResponse playInfo = OSSUploadVideoDemo.getPlayInfo(video.getVideoId());
getPlayInfoResponses.add(playInfo);
} catch (Exception e) {
e.printStackTrace();
}
}
if (JavaListUtils.isEmpty(getPlayInfoResponses)) {
System.out.println("getPlayInfoResponses.size():0");
return;
}
// 3 遍历写入excel
for (GetPlayInfoResponse getPlayInfoRespons : getPlayInfoResponses) {
String[] stringOuts = null;
// 将文件写入表格中 String[] strings = {"1 第三方介质ID", "2 介质名称", "3 介质格式", "4 播放时长 (秒)", "5 分辨率", "6 播放地址", "7 requestId", "8 videoId", "9 code", "10 message"};
if (getPlayInfoRespons == null) {
stringOuts = new String[]{"1", "2",
"3", "4",
"5", "6",
"7", "8",
"9", "10"};
} else {
String str3 = "m3u8";
String str5 = "1280*720"; // 默认
String str6 = "";
GetPlayInfoResponse.VideoBase videoBase = getPlayInfoRespons.getVideoBase();
List<GetPlayInfoResponse.PlayInfo> playInfoList = getPlayInfoRespons.getPlayInfoList();
if (!JavaListUtils.isEmpty(playInfoList)) {
GetPlayInfoResponse.PlayInfo playInfo = playInfoList.get(0); // 取第一个
str3 = playInfo.getFormat();
str5 = playInfo.getWidth() + "*" + playInfo.getHeight();
str6 = playInfo.getPlayURL();
}
stringOuts = new String[]{videoBase.getVideoId(), videoBase.getTitle(),
str3, videoBase.getDuration(),
str5, str6,
getPlayInfoRespons.getRequestId(), videoBase.getVideoId(),
videoBase.getStatus(), videoBase.getCreationTime(),
suplier.getNickName(), catId + ""};
}
// 3 将信息汇总-写入excel
try {
log.info("写入excel开始objectName:{}", catId);
List<String[]> list1 = new ArrayList<String[]>();
list1.add(stringOuts);
JavaExcelUtils.addOneLine(list1, JavaExcelUtils.TableName.YIXUE);
log.info("写入excel完毕objectName:{},stringOuts:{}", catId, stringOuts);
} catch (Exception e) {
log.error("写入excel出错了4objectName:{},list1:{},msg:{}", catId, stringOuts, e.getMessage());
}
log.info("getPalyInfoToExcel完毕objectName:{}", catId);
log.info("getPalyInfoToExcel写入excel完毕stringOuts:{}", stringOuts.toString());
}
log.info("getPalyInfoToExcel写入excel完毕stringOuts:{}");
}
/**
* 获取源文件信息
*
* @return GetMezzanineInfoResponse 获取源文件信息响应数据
* @throws Exception
*/
public static GetMezzanineInfoResponse getMezzanineInfo(String videoId) throws Exception {
log.info("查询videoId:{}", videoId);
GetMezzanineInfoRequest request = new GetMezzanineInfoRequest();
DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret); // 发送请求客户端
request.setVideoId(videoId);
//源片下载地址过期时间
request.setAuthTimeout(3600L);
return client.getAcsResponse(request);
}
/**
* 初始化 点播视频查询
*
* @param accessKeyId
* @param accessKeySecret
* @return
* @throws ClientException
*/
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;
}
/**
* 获取视频信息
*
* @return GetVideoInfoResponse 获取视频信息响应数据
* @throws Exception
*/
public static GetVideoInfoResponse getVideoInfo(String videoId) throws Exception {
log.info("获取视频信息开始:{}", videoId);
DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret); // 发送请求客户端
GetVideoInfoRequest request = new GetVideoInfoRequest();
request.setVideoId(videoId);
return client.getAcsResponse(request);
}
/*获取播放地址函数*/
/**
* 转码流地址
*
* @param videoId 视频id
* @return
* @throws Exception
*/
public static GetPlayInfoResponse getPlayInfo(String videoId) throws Exception {
DefaultAcsClient client = OSSUploadVideoDemo.initVodClient(OSSUploadVideoDemo.accessKeyId, OSSUploadVideoDemo.accessKeySecret); // 发送请求客户端
GetPlayInfoRequest request = new GetPlayInfoRequest();
request.setVideoId(videoId);
return client.getAcsResponse(request);
}
4 视频点播操作UploadVideoDemo
package com.operlog.main.aliyun;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.vod.upload.impl.UploadAttachedMediaImpl;
import com.aliyun.vod.upload.impl.UploadImageImpl;
import com.aliyun.vod.upload.impl.UploadM3u8FileImpl;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.*;
import com.aliyun.vod.upload.resp.*;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.*;
import com.operlog.main.model.po.Suplier;
import com.operlog.main.utils.JavaListUtils;
import com.operlog.utils.log.service.impl.UploadVideoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* 视频点播工具类
* 以下Java示例代码演示了如何在服务端上传媒资文件至视频点播,媒资类型支持音频、视频和图片。
*
* 一、音视频上传目前支持4种方式上传:
*
* 1.上传本地文件,使用分片上传,并支持断点续传,参见testUploadVideo函数。
* 1.1 当断点续传关闭时,最大支持上传任务执行时间为3000秒,具体可上传文件大小与您的网络带宽及磁盘读写能力有关。
* 1.2 当断点续传开启时,最大支持48.8TB的单个文件,注意,断点续传开启后,上传任务执行过程中,同时会将当前上传位置写入本地磁盘文件,影响您上传文件的速度,请您根据文件大小选择是否开启
*
* 2.上传网络流,可指定文件URL进行上传,支持断点续传,最大支持48.8TB的单个文件。
* 该上传方式需要先将网络文件下载到本地磁盘,再进行上传,所以要保证本地磁盘有充足的空间。参见testUploadURLStream函数。
*
* 3.上传文件流,可指定本地文件进行上传,不支持断点续传,最大支持5GB的单个文件。参见testUploadFileStream函数。
*
* 4.流式上传,可指定输入流进行上传,支持文件流和网络流等,不支持断点续传,最大支持5GB的单个文件。参见testUploadStream函数。
*
*
* 二、图片上传目前支持2种方式上传:
* 1.上传本地文件,不支持断点续传,最大支持5GB的单个文件,参见testUploadImageLocalFile函数
* 2.上传文件流和网络流,InputStream参数必选,不支持断点续传,最大支持5GB的单个文件。参见testUploadImageStream函数。
* 注:图片上传完成后,会返回图片ID和图片地址,也可通过GetImageInfo查询图片信息,参见接口文档 https://help.aliyun.com/document_detail/89742.html
*
*
* 三、m3u8文件上传目前支持2种方式:
* 1.上传本地m3u8音视频文件(包括所有分片文件)到点播,需指定本地m3u8索引文件地址和所有分片地址。
* 2.上传网络m3u8音视频文件(包括所有分片文件)到点播,需指定m3u8索引文件和分片文件的URL地址。
*
* 注:
* 1) 上传网络m3u8音视频文件时需要保证地址可访问,如果有权限限制,请设置带签名信息的地址,且保证足够长的有效期,防止地址无法访问导致上传失败
* 2) m3u8文件上传暂不支持进度回调
*
*
* 四、上传进度回调通知:
* 1.默认上传进度回调函数:视频点播上传SDK内部默认开启上传进度回调函数,输出不同事件通知的日志,您可以设置关闭该上传进度通知及日志输出;
* 2.自定义上传进度回调函数:您可根据自已的业务场景重新定义不同事件处理的方式,只需要修改上传回调示例函数即可。
*
*
* 五、辅助媒资上传目前支持2种方式:
* 1.上传本地文件,不支持断点续传,最大支持5GB的单个文件,参见testUploadAttachedMediaLocalFile函数
* 2.上传文件流和网络流,InputStream参数必选,不支持断点续传,最大支持5GB的单个文件。参见testUploadAttachedMediaStream函数。
*
*
* 六、支持STS方式上传:
* 1.您需要实现VoDRefreshSTSTokenListener接口的onRefreshSTSToken方法,用于生成STS信息,
* 当文件上传时间超过STS过期时间时,SDK内部会定期调用此方法刷新您的STS信息进行后续文件的上传。
*
*
* 七、可指定上传脚本部署的ECS区域(设置Request的EcsRegionId参数,取值参考存储区域标识:https://help.aliyun.com/document_detail/98194.html),
* 如果与点播存储(OSS)区域相同,则自动使用内网上传文件至存储,上传更快且更省公网流量
* 由于点播API只提供外网域名访问,因此部署上传脚本的ECS服务器必须具有访问外网的权限。
*
* 注意:
* 请替换示例中的必选参数,示例中的可选参数如果您不需要设置,请将其删除,以免设置无效参数值与您的预期不符。
*/
@Slf4j
public class UploadVideoDemo {
public static void main(String[] args) {
// 五、查询视频列表
GetVideoListResponse videoList = null;
try {
videoList = getVideoList(1000136018L, 0, 200);
} catch (Exception e) {
e.printStackTrace();
}
List<GetVideoListResponse.Video> videos = videoList.getVideoList();
System.out.println("videos.size():" + videos.size());
// 5.1视频信息
List<GetVideoInfoResponse> getVideoInfoResponses = new ArrayList<>();
List<GetPlayInfoResponse> getPlayInfoResponses = new ArrayList<>();
for (GetVideoListResponse.Video video : videos) {
try {
GetVideoInfoResponse videoInfo = OSSUploadVideoDemo.getVideoInfo(video.getVideoId());
GetPlayInfoResponse playInfo = OSSUploadVideoDemo.getPlayInfo(video.getVideoId());
getVideoInfoResponses.add(videoInfo);
getPlayInfoResponses.add(playInfo);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("getVideoInfoResponses.size():" + getVideoInfoResponses.size());
// 5.2 视频流信息
System.out.println("getPlayInfoResponses.size():" + getPlayInfoResponses.size());
// 5.3 获取分类下所有视频列表
List<GetVideoListResponse.Video> videoListAll = null;
try {
videoListAll = getVideoListAll(19L);
} catch (Exception e) {
e.printStackTrace();
}
if (JavaListUtils.isEmpty(videoListAll)) {
return;
}
System.out.println("videoListAll.size():" + videoListAll.size());
for (Suplier suplier : UploadVideoServiceImpl.suplierlist) {
try {
OSSUploadVideoDemo.getPalyInfoToExcel(suplier);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public final static Integer pageSizeFix = 100;
/**
* 获取全部视频列表
*
* @param catId 分类id
* @return GetVideoListResponse 获取视频列表响应数据
* @throws Exception
*/
public static List<GetVideoListResponse.Video> getVideoListAll(Long catId) throws Exception {
// 1 获取总数
List<GetVideoListResponse.Video> videosOut = new ArrayList<>();
GetVideoListResponse videoListFind = getVideoList(catId, 1, pageSizeFix);
if (videoListFind == null) {
System.out.println("获取的返回结果为空,catId:" + catId);
return videosOut;
}
Integer total = videoListFind.getTotal();
if (total == null || total <= 0) {
System.out.println("获取的总数为空或者小于等于0,total:" + total);
return videosOut;
}
Integer totalPage = total / pageSizeFix + 1;
// 2 分页获取全部
for (Integer i = 0; i < totalPage; i++) {
GetVideoListResponse videoListIn = getVideoList(catId, i + 1, pageSizeFix);
if (videoListIn != null && !JavaListUtils.isEmpty(videoListIn.getVideoList())) {
videosOut.addAll(videoListIn.getVideoList());
}
}
System.out.println("获取的总数为,videosOut:" + videosOut.size());
return videosOut;
}
/**
* 获取分页视频列表 -最多200个
*
* @param catId 分类id
* @param pageNo 页数 1开始
* @param pageSize 每页数量 最大两百
* @return GetVideoListResponse 获取视频列表响应数据
* @throws Exception
*/
public static GetVideoListResponse getVideoList(Long catId, Integer pageNo, Integer pageSize) throws Exception {
DefaultAcsClient client = OSSUploadVideoDemo.initVodClient(OSSUploadVideoDemo.accessKeyId, OSSUploadVideoDemo.accessKeySecret); // 发送请求客户端
GetVideoListRequest request = new GetVideoListRequest();
// 分别取一个月前、当前时间的UTC时间作为筛选视频列表的起止时间
// 视频创建的起始时间,为UTC格式
request.setCateId(catId);
// 视频状态,默认获取所有状态的视频,多个用逗号分隔
// request.setStatus("Uploading,Normal,Transcoding");
request.setPageNo(pageNo);
request.setPageSize(pageSize);
return client.getAcsResponse(request);
}
/**
* 本地文件上传接口
*
* @param accessKeyId
* @param accessKeySecret
* @param title
* @param fileName
* @param cateId 分类id
*/
public static UploadVideoResponse testUploadVideo(String accessKeyId, String accessKeySecret, String title, String fileName, Long cateId) {
log.info("accessKeyId:{},accessKeySecret:{},title:{},fileName:{}", accessKeyId, accessKeySecret, title, fileName);
UploadVideoResponse response = null;
try {
UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
/* 可指定分片上传时每个分片的大小,默认为2M字节 */
request.setPartSize(2 * 1024 * 1024L);
/* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
request.setTaskNum(1);
/* 是否开启断点续传, 默认断点续传功能关闭。当网络不稳定或者程序崩溃时,再次发起相同上传请求,可以继续未完成的上传任务,适用于超时3000秒仍不能上传完成的大文件。
注意: 断点续传开启后,会在上传过程中将上传位置写入本地磁盘文件,影响文件上传速度,请您根据实际情况选择是否开启*/
//request.setEnableCheckpoint(false);
/* OSS慢请求日志打印超时时间,是指每个分片上传时间超过该阈值时会打印debug日志,如果想屏蔽此日志,请调整该阈值。单位: 毫秒,默认为300000毫秒*/
//request.setSlowRequestsThreshold(300000L);
/* 可指定每个分片慢请求时打印日志的时间阈值,默认为300s*/
//request.setSlowRequestsThreshold(300000L);
/* 是否显示水印(可选),指定模板组ID时,根据模板组配置确定是否显示水印*/
//request.setIsShowWaterMark(true);
/* 设置上传完成后的回调URL(可选),建议您通过点播控制台配置事件通知,参见文档 https://help.aliyun.com/document_detail/55627.html */
//request.setCallback("http://callback.sample.com");
/* 自定义消息回调设置(可选),参数说明参考文档 https://help.aliyun.com/document_detail/86952.html#UserData */
// request.setUserData("{\"Extend\":{\"test\":\"www\",\"localId\":\"xxxx\"},\"MessageCallback\":{\"CallbackURL\":\"http://test.test.com\"}}");
/* 视频分类ID(可选) */
request.setCateId(cateId);
/* 视频标签,多个用逗号分隔(可选) */
//request.setTags("标签1,标签2");
/* 视频描述(可选) */
//request.setDescription("视频描述");
/* 封面图片(可选) */
//request.setCoverURL("http://cover.sample.com/sample.jpg");
/* 模板组ID(可选) */
request.setTemplateGroupId("cfafe6e54e9a40");
/* 工作流ID(可选) */
//request.setWorkflowId("9577859b0177b");
/* 存储区域(可选) */
//request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com");
/* 开启默认上传进度回调 */
//request.setPrintProgress(false);
/* 设置自定义上传进度回调 (必须继承 VoDProgressListener) */
//request.setProgressListener(new PutObjectProgressListener());
/* 设置您实现的生成STS信息的接口实现类*/
// request.setVoDRefreshSTSTokenListener(new RefreshSTSTokenImpl());
/* 设置应用ID*/
//request.setAppId("app-1000000");
/* 点播服务接入点 */
request.setApiRegionId("cn-shanghai"); // 内网上传指定传输
// /* ECS部署区域*/
request.setEcsRegionId("cn-shanghai"); // 内网上传指定传输
UploadVideoImpl uploader = new UploadVideoImpl();
response = uploader.uploadVideo(request);
log.info("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
if (response.isSuccess()) {
log.info("VideoId=" + response.getVideoId() + "\n");
} else {
/* 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */
log.info("VideoId=" + response.getVideoId() + "\n");
log.info("ErrorCode=" + response.getCode() + "\n");
log.info("ErrorMessage=" + response.getMessage() + "\n");
}
// 上线注释要去除掉
File file = new File(fileName);
file.delete();
} catch (Exception e) {
log.info("出错了msg6:{}", e.getMessage());
}
return response;
}
}
一起学习,一起进步,有建议请提出。