1.首先复制编写好的工具类(摘取自阿里云官方文档,可以在文芳查看), 然后你需要改写你的ak aksk,其次更改bbucket路径(存储路径),以及点播接入点(如 cn-beijing,cn-shanghai) ,在这之后你需要导入pom依赖,否则下面代码全部报错。(务必静下心看仔细) 阿里云官方文档地址:
https://help.aliyun.com/document_detail/53406.htm?spm=a2c4g.11186623.0.0.235a5d87S8SsE3#multiTask13370
package com.tanhua.commons.utils;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.google.gson.Gson;
public class UploadVideoUtilcopy {
//账号AK信息请填写(必选)
private static final String accessKeyId = "《accessKeyId(点击用户头像获取的ak)》";
//账号AK信息请填写(必选)
private static final String accessKeySecret = "《accessKeySecret(和aksecret)》";
//批量上传逻辑:
//先上传文件,返回videoid,先把videoid存到数据库里,等待成功回调,在回调时数据库查找对应videoid,存放视频地址
//单视频上传逻辑:
//上传文件,等待上传完成,再拿播放地址存到数据库
public static void main(String[] args) {
JSONObject json = UploadVideo("测试end","C:/Users/Administrator/Videos/Captures/1.mp4(本地上传的路径(记得符合规范))");
try {
//注意!如果不sleep的话视频刚上传成功的状态是跟不上运行的
//会报错视频状态不是上传成功状态
//sleep时间按照自己视频大小去改
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url = GetPlayInfo((String)json.get("vid"));
System.out.println(url);
}
/**
* 本地文件上传接口
*
* @param title
* @param fileName
*/
public static JSONObject UploadVideo(String title, String fileName) {
UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
/* 可指定分片上传时每个分片的大小,默认为2M字节 */
request.setPartSize(2 * 1024 * 1024L);
/* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
request.setTaskNum(1);
/* 是否显示水印(可选),指定模板组ID时,根据模板组配置确定是否显示水印*/
request.setIsShowWaterMark(false);
/* 存储区域(可选) */
// 如果需要在阿里云看到固定位置看到视频必须设置桶的路径(bucket)此时是必写
request.setStorageLocation("outin-902ffcafc23211ec9acd00163e10ce6c.oss-cn-beijing.aliyuncs.com");
/* 点播服务接入点 */
// 如果需要在阿里云看到固定位置看到视频必须设置接入点的区域(比如上海 cn-shanghai)此时是必写
request.setApiRegionId("cn-beijing");
/* ECS部署区域*/
// request.setEcsRegionId("cn-shanghai");
UploadVideoImpl uploader = new UploadVideoImpl();
UploadVideoResponse response = uploader.uploadVideo(request);
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
JSONObject json = new JSONObject();
if (response.isSuccess()) {
System.out.print("VideoId=" + response.getVideoId() + "\n");
//这里我用json接收vid以后批量上传会用到
json.put("status",1);
json.put("vid",response.getVideoId());
return json;
} else {
/* 上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */
if(response.getVideoId()==null||response.getVideoId()=="") {
json.put("status",2);
//这里是上传失败的逻辑,可以根据错误代码自己百度
}
System.out.print("ErrorCode=" + response.getCode() + "\n");
System.out.print("ErrorMessage=" + response.getMessage() + "\n");
return json;
}
}
/**
* 获取上传视频地址接口
*
* @param vid
*/
public static String GetPlayInfo(String vid) {
// 创建SubmitMediaInfoJob实例并初始化
DefaultProfile profile = DefaultProfile.getProfile(
"cn-beijing", // // 点播服务所在的地域ID,中国大陆地域请填cn-shanghai
accessKeyId, // 您的AccessKey ID
accessKeySecret ); // 您的AccessKey Secret
IAcsClient client = new DefaultAcsClient(profile);
GetPlayInfoRequest request1 = new GetPlayInfoRequest();
// 视频ID。
request1.setVideoId(vid);
String url = null;
try {
GetPlayInfoResponse response1 = client.getAcsResponse(request1);
System.out.println(new Gson().toJson(response1));
for (GetPlayInfoResponse.PlayInfo playInfo : response1.getPlayInfoList()) {
// 播放地址
System.out.println("PlayInfo.PlayURL = " + playInfo.getPlayURL());
String str = playInfo.getPlayURL();
//这里会返回m3u8和mp4格式,m3u8需要转码,看自己情况
//如果播放地址后缀为mp4返回
if(str != null || str != "") {
if(str.substring(str.length()-3,str.length()).equals("mp4")) {
url = playInfo.getPlayURL();
}
}
}
return url;
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
return null;
}
}
2.导入下面依赖(部分依赖因为无法下载成功会爆红,不必担心,我们手动导入从官方下载的jar包(视频点播jar包已上传,资源免费使用))
<!-- 阿里云上传视频-->
<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>-->
<!-- 阿里云上传视频-->
3.jar包导入方法 1-1 1-2 1-3 1-4 所有jar包全部导入成功 4.如果存在依赖仍然爆红的情况,我们有两种方法重新加载依赖 4.1(点击M执行命令) 1-5 刷新成功! 4.2(点击build执行命令)
|