|
|
|
@@ -1,4 +1,4 @@ |
|
|
|
package com.iformall.video.aliyun; |
|
|
|
package com.iformall.video.aliyun.sdk.upload; |
|
|
|
|
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
@@ -26,6 +26,9 @@ import com.aliyun.vod.upload.resp.UploadStreamResponse; |
|
|
|
import com.aliyun.vod.upload.resp.UploadURLStreamResponse; |
|
|
|
import com.aliyun.vod.upload.resp.UploadVideoResponse; |
|
|
|
import com.aliyun.vod.upload.resp.UploadWebM3u8Response; |
|
|
|
import com.aliyuncs.DefaultAcsClient; |
|
|
|
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest; |
|
|
|
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse; |
|
|
|
|
|
|
|
/** |
|
|
|
* 以下Java示例代码演示了如何在服务端上传媒资文件至视频点播,媒资类型支持音频、视频和图片。 |
|
|
|
@@ -81,7 +84,7 @@ import com.aliyun.vod.upload.resp.UploadWebM3u8Response; |
|
|
|
* 注意: |
|
|
|
* 请替换示例中的必选参数,示例中的可选参数如果您不需要设置,请将其删除,以免设置无效参数值与您的预期不符。 |
|
|
|
*/ |
|
|
|
public class AliyunVedioUploadHelper { |
|
|
|
public class AliyunVedioUpload { |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
@@ -150,6 +153,37 @@ public class AliyunVedioUploadHelper { |
|
|
|
// testUploadAttachedMediaStream(accessKeyId, accessKeySecret); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 通过视频ID获取音视频播放地址 |
|
|
|
* @param client |
|
|
|
* @return |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client) throws Exception { |
|
|
|
CreateUploadVideoRequest request = new CreateUploadVideoRequest(); |
|
|
|
request.setTitle("this is a sample"); |
|
|
|
request.setFileName("filename.mp4"); |
|
|
|
|
|
|
|
//UserData,用户自定义设置参数,用户需要单独回调URL及数据透传时设置(非必须) |
|
|
|
//JSONObject userData = new JSONObject(); |
|
|
|
|
|
|
|
//UserData回调部分设置 |
|
|
|
//JSONObject messageCallback = new JSONObject(); |
|
|
|
//messageCallback.put("CallbackURL", "http://192.168.0.0/16"); |
|
|
|
//messageCallback.put("CallbackType", "http"); |
|
|
|
//userData.put("MessageCallback", messageCallback.toJSONString()); |
|
|
|
|
|
|
|
//UserData透传数据部分设置 |
|
|
|
//JSONObject extend = new JSONObject(); |
|
|
|
//extend.put("MyId", "user-defined-id"); |
|
|
|
//userData.put("Extend", extend.toJSONString()); |
|
|
|
|
|
|
|
//request.setUserData(userData.toJSONString()); |
|
|
|
|
|
|
|
return client.getAcsResponse(request); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 本地文件上传接口 |
|
|
|
* |
|
|
|
@@ -158,7 +192,7 @@ public class AliyunVedioUploadHelper { |
|
|
|
* @param title |
|
|
|
* @param fileName |
|
|
|
*/ |
|
|
|
public static void uploadVideo(String accessKeyId, String accessKeySecret, String title, String fileName) { |
|
|
|
public static UploadVideoResponse uploadVideo(String accessKeyId, String accessKeySecret, String title, String fileName) { |
|
|
|
UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName); |
|
|
|
/* 可指定分片上传时每个分片的大小,默认为2M字节 */ |
|
|
|
request.setPartSize(2 * 1024 * 1024L); |
|
|
|
@@ -203,15 +237,7 @@ public class AliyunVedioUploadHelper { |
|
|
|
// request.setEcsRegionId("cn-shanghai"); |
|
|
|
UploadVideoImpl uploader = new UploadVideoImpl(); |
|
|
|
UploadVideoResponse response = uploader.uploadVideo(request); |
|
|
|
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID |
|
|
|
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"); |
|
|
|
} |
|
|
|
return response; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -224,7 +250,7 @@ public class AliyunVedioUploadHelper { |
|
|
|
* @param fileName |
|
|
|
* @param url |
|
|
|
*/ |
|
|
|
public static void uploadURLStream(String accessKeyId, String accessKeySecret, String title, String url, String fileExtension) { |
|
|
|
public static UploadURLStreamResponse uploadURLStream(String accessKeyId, String accessKeySecret, String title, String url, String fileExtension) { |
|
|
|
UploadURLStreamRequest request = new UploadURLStreamRequest(accessKeyId, accessKeySecret, title, url); |
|
|
|
|
|
|
|
/* 文件扩展名*/ |
|
|
|
@@ -265,15 +291,7 @@ public class AliyunVedioUploadHelper { |
|
|
|
// request.setEcsRegionId("cn-shanghai"); |
|
|
|
UploadVideoImpl uploader = new UploadVideoImpl(); |
|
|
|
UploadURLStreamResponse response = uploader.uploadURLStream(request); |
|
|
|
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID |
|
|
|
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"); |
|
|
|
} |
|
|
|
return response; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -284,7 +302,7 @@ public class AliyunVedioUploadHelper { |
|
|
|
* @param title |
|
|
|
* @param fileName |
|
|
|
*/ |
|
|
|
public static void uploadFileStream(String accessKeyId, String accessKeySecret, String title, String fileName) { |
|
|
|
private static void uploadFileStream(String accessKeyId, String accessKeySecret, String title, String fileName) { |
|
|
|
UploadFileStreamRequest request = new UploadFileStreamRequest(accessKeyId, accessKeySecret, title, fileName); |
|
|
|
/* 是否使用默认水印(可选),指定模板组ID时,根据模板组配置确定是否使用默认水印*/ |
|
|
|
//request.setShowWaterMark(true); |
|
|
|
@@ -336,7 +354,7 @@ public class AliyunVedioUploadHelper { |
|
|
|
* @param fileName |
|
|
|
* @param inputStream |
|
|
|
*/ |
|
|
|
public static void uploadStream(String accessKeyId, String accessKeySecret, String title, String fileName, InputStream inputStream) { |
|
|
|
public static UploadStreamResponse uploadStream(String accessKeyId, String accessKeySecret, String title, String fileName, InputStream inputStream) { |
|
|
|
UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream); |
|
|
|
/* 是否使用默认水印(可选),指定模板组ID时,根据模板组配置确定是否使用默认水印*/ |
|
|
|
//request.setShowWaterMark(true); |
|
|
|
@@ -368,14 +386,7 @@ public class AliyunVedioUploadHelper { |
|
|
|
// request.setEcsRegionId("cn-shanghai"); |
|
|
|
UploadVideoImpl uploader = new UploadVideoImpl(); |
|
|
|
UploadStreamResponse response = uploader.uploadStream(request); |
|
|
|
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID |
|
|
|
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"); |
|
|
|
} |
|
|
|
return response; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -538,7 +549,7 @@ public class AliyunVedioUploadHelper { |
|
|
|
* m3u8FileURL: 网络m3u8索引文件的URL地址,且m3u8文件的分片信息必须是相对地址,不能含有URL或本地绝对路径 |
|
|
|
* sliceFileURLs: ts分片文件的URL地址列表;需自行拼接ts分片的URL地址列表 |
|
|
|
*/ |
|
|
|
private static void uploadWebM3u8(String accessKeyId, String accessKeySecret) { |
|
|
|
public static void uploadWebM3u8(String accessKeyId, String accessKeySecret) { |
|
|
|
String title = "test_upload_web_m3u8"; |
|
|
|
String m3u8FileURL = "http://test.aliyun.com/f0d644abc547129e957b386f772c72/a0e1e2817ab9425aa558fe67a90e717f-538087dcf2c201c31ce4324bf76af691-ld.m3u8"; |
|
|
|
UploadWebM3u8Request request = new UploadWebM3u8Request(accessKeyId, accessKeySecret, title, m3u8FileURL); |
|
|
|
@@ -596,7 +607,7 @@ public class AliyunVedioUploadHelper { |
|
|
|
* @param accessKeyId |
|
|
|
* @param accessKeySecret |
|
|
|
*/ |
|
|
|
private static void uploadAttachedMediaLocalFile(String accessKeyId, String accessKeySecret) { |
|
|
|
public static void uploadAttachedMediaLocalFile(String accessKeyId, String accessKeySecret) { |
|
|
|
/* 业务类型 */ |
|
|
|
String businessType = "watermark"; |
|
|
|
/* 文件扩展名 */ |
|
|
|
@@ -639,7 +650,7 @@ public class AliyunVedioUploadHelper { |
|
|
|
* @param accessKeyId |
|
|
|
* @param accessKeySecret |
|
|
|
*/ |
|
|
|
private static void uploadAttachedMediaStream(String accessKeyId, String accessKeySecret) { |
|
|
|
public static void uploadAttachedMediaStream(String accessKeyId, String accessKeySecret) { |
|
|
|
/* 业务类型 */ |
|
|
|
String businessType = "watermark"; |
|
|
|
/* 文件扩展名 */ |