Przeglądaj źródła

fix aliyunvideo

release_toaliyun_real
xiaohanzi 5 lat temu
rodzic
commit
37491bae34
6 zmienionych plików z 178 dodań i 40 usunięć
  1. +1
    -1
      mallinkVideo/pom.xml
  2. +78
    -4
      mallinkVideo/src/main/java/com/iformall/video/aliyun/AliyunVideoExcutor.java
  3. +1
    -0
      mallinkVideo/src/main/java/com/iformall/video/aliyun/config/AliyunVideoConfig.java
  4. +50
    -0
      mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/server/AliyunVedioServer.java
  5. +46
    -35
      mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/AliyunVedioUpload.java
  6. +2
    -0
      mallinkVideo/src/main/java/com/iformall/video/entity/VideUploadResult.java

+ 1
- 1
mallinkVideo/pom.xml Wyświetl plik

@@ -37,7 +37,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.json</groupId>


+ 78
- 4
mallinkVideo/src/main/java/com/iformall/video/aliyun/AliyunVideoExcutor.java Wyświetl plik

@@ -1,34 +1,108 @@
package com.iformall.video.aliyun;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyun.vod.upload.resp.UploadURLStreamResponse;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.exceptions.ClientException;
import com.iformall.video.VideoExcutor;
import com.iformall.video.aliyun.config.AliyunVideoConfig;
import com.iformall.video.aliyun.sdk.server.AliyunVedioServer;
import com.iformall.video.aliyun.sdk.upload.AliyunVedioUpload;
import com.iformall.video.entity.VideUploadResult;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class AliyunVideoExcutor implements VideoExcutor {

@Autowired
AliyunVideoConfig config;
private String getUrlByVeidoId(String videoId) {
try {
return AliyunVedioServer.getVedioUrl(config, videoId);
} catch (ClientException e) {
log.error("aliyun getUrlByVeidoId error",e);
}
return null;
}
@Override
public VideUploadResult uploadLocalVideo(String title,String localFile) {
//AliyunVedioUploadHelper.
return null;
VideUploadResult result = new VideUploadResult();
try {
UploadVideoResponse response =AliyunVedioUpload.uploadVideo(config.getAccessKeyId(), config.getAccessKeySecret(), title, localFile);
if (response.isSuccess()) {
result.setVideoId(response.getVideoId());
result.setVideoUrl(getUrlByVeidoId(response.getVideoId()));
result.setSuccess(true);
}else {
//如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
result.setSuccess(false);
result.setMsg("ErrorCode["+response.getCode()+"]"+response.getMessage());
}
} catch (Exception e) {
log.error("aliyun uploadLocalVideo error",e);
result.setSuccess(false);
result.setMsg(e.getLocalizedMessage());
}
return result;
}

@Override
public VideUploadResult uploadLocalVideo(String title,File localFile) {
return null;
VideUploadResult result = new VideUploadResult();
UploadStreamResponse response;
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(localFile);
response = AliyunVedioUpload.uploadStream(config.getAccessKeyId(), config.getAccessKeySecret(), title, localFile.getName(), inputStream);
if (response.isSuccess()) {
result.setVideoId(response.getVideoId());
result.setVideoUrl(getUrlByVeidoId(response.getVideoId()));
result.setSuccess(true);
} else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
result.setSuccess(false);
result.setMsg("ErrorCode["+response.getCode()+"]"+response.getMessage());
}
} catch (FileNotFoundException e) {
log.error("aliyun uploadLocalVideo error",e);
result.setSuccess(false);
result.setMsg(e.getLocalizedMessage());
}finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
return result;
}

@Override
public VideUploadResult uploadNetVideo(String title,String url,String ext) {
return null;
VideUploadResult result = new VideUploadResult();
UploadURLStreamResponse response = AliyunVedioUpload.uploadURLStream(config.getAccessKeyId(), config.getAccessKeySecret(), title, url, ext);
if (response.isSuccess()) {
result.setVideoId(response.getVideoId());
result.setVideoUrl(getUrlByVeidoId(response.getVideoId()));
result.setSuccess(true);
} else {
/* 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */
result.setSuccess(false);
result.setMsg("ErrorCode["+response.getCode()+"]"+response.getMessage());
}
return result;
}

}

+ 1
- 0
mallinkVideo/src/main/java/com/iformall/video/aliyun/config/AliyunVideoConfig.java Wyświetl plik

@@ -11,4 +11,5 @@ import lombok.Data;
public class AliyunVideoConfig {
private String accessKeyId;
private String accessKeySecret;
private String regionId;//cn-shanghai 点播服务接入区域
}

+ 50
- 0
mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/server/AliyunVedioServer.java Wyświetl plik

@@ -0,0 +1,50 @@
package com.iformall.video.aliyun.sdk.server;

import java.util.List;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.iformall.video.aliyun.config.AliyunVideoConfig;

import lombok.extern.slf4j.Slf4j;

import com.aliyuncs.exceptions.ClientException;

@Slf4j
public class AliyunVedioServer {

private static DefaultAcsClient initVodClient(AliyunVideoConfig config) throws ClientException {
DefaultProfile profile = DefaultProfile.getProfile(config.getRegionId(), config.getAccessKeyId(), config.getAccessKeySecret());
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
private static DefaultAcsClient initVodClient(AliyunVideoConfig config, String securityToken) throws ClientException {
DefaultProfile profile = DefaultProfile.getProfile(config.getRegionId(), config.getAccessKeyId(), config.getAccessKeySecret(), securityToken);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
//获取视频播放地址
//https://help.aliyun.com/document_detail/61064.html?spm=a2c4g.11186623.6.1043.7c2d3838K6wQPZ
public static String getVedioUrl(AliyunVideoConfig config,String videoId) throws ClientException {
DefaultAcsClient client = initVodClient(config);
GetPlayInfoResponse response = new GetPlayInfoResponse();
try {
GetPlayInfoRequest request = new GetPlayInfoRequest();
request.setVideoId(videoId);
response = client.getAcsResponse(request);
List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
//播放地址
for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
return playInfo.getPlayURL();
}
} catch (Exception e) {
log.error("getVedioUrl error.",e);
}
return null;
}
}

mallinkVideo/src/main/java/com/iformall/video/aliyun/AliyunVedioUploadHelper.java → mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/AliyunVedioUpload.java Wyświetl plik

@@ -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";
/* 文件扩展名 */

+ 2
- 0
mallinkVideo/src/main/java/com/iformall/video/entity/VideUploadResult.java Wyświetl plik

@@ -6,4 +6,6 @@ import lombok.Data;
public class VideUploadResult {
private String videoId;
private String videoUrl;
private boolean success = false;
private String msg;
}

Ładowanie…
Anuluj
Zapisz