package com.peanut.common.utils; import com.alibaba.fastjson.JSONObject; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import com.aliyun.vod20170321.Client; import com.aliyun.vod20170321.models.*; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest; import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse; //import org.bytedeco.javacv.FFmpegFrameGrabber; //import org.bytedeco.javacv.FrameGrabber; import java.util.HashMap; import java.util.Map; public class SpdbUtil { private static Client client; static { com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 .setAccessKeyId("LTAI5tMKmWhPfnPsz2J3bfxL") // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 .setAccessKeySecret("doFUplbiIxL6PgJME3eSaW8G6HauuC"); // Endpoint 请参考 https://api.aliyun.com/product/vod config.endpoint = "vod.cn-shanghai.aliyuncs.com"; try { client = new Client(config); } catch (Exception e) { throw new RuntimeException(e); } } // private static String accessKeyId = "LTAI5tMKmWhPfnPsz2J3bfxL"; // private static String accessKeySecret = "doFUplbiIxL6PgJME3eSaW8G6HauuC"; // public static Integer getMp4Duration(String url){ // double duration = 0; // try (FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(url)) { // frameGrabber.start(); // duration = frameGrabber.getLengthInTime()/1000000; // frameGrabber.stop(); // } catch (FrameGrabber.Exception e) { // e.printStackTrace(); // } // return (int)duration; // }; public static GetVideoPlayAuthResponse getPlayAuth(String vid) throws Exception { // com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() // // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 // .setAccessKeyId("LTAI5tMKmWhPfnPsz2J3bfxL") // // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 // .setAccessKeySecret("doFUplbiIxL6PgJME3eSaW8G6HauuC"); // // Endpoint 请参考 https://api.aliyun.com/product/vod // config.endpoint = "vod.cn-shanghai.aliyuncs.com"; // Client client = new Client(config); GetVideoPlayAuthRequest getVideoPlayAuthRequest = new GetVideoPlayAuthRequest().setVideoId(vid).setAuthInfoTimeout(1000L);; RuntimeOptions runtimeOptions = new RuntimeOptions(); try { // 复制代码运行请自行打印 API 的返回值 GetVideoPlayAuthResponse videoPlayAuthWithOptions = client.getVideoPlayAuthWithOptions(getVideoPlayAuthRequest, runtimeOptions); return videoPlayAuthWithOptions; // return videoPlayAuthWithOptions.getBody().getPlayAuth(); } catch (TeaException error) { // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message System.out.println(error.getMessage()); // 诊断地址 System.out.println(error.getData().get("Recommend")); return null; } catch (Exception _error) { TeaException error = new TeaException(_error.getMessage(), _error); // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message System.out.println(error.getMessage()); // 诊断地址 System.out.println(error.getData().get("Recommend")); return null; } } public static GenerateKMSDataKeyResponseBody KMS() throws Exception { // com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() // // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 // .setAccessKeyId("LTAI5tMKmWhPfnPsz2J3bfxL") // // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 // .setAccessKeySecret("doFUplbiIxL6PgJME3eSaW8G6HauuC"); // config.endpoint = "vod.cn-shanghai.aliyuncs.com"; // Client client = new Client(config); com.aliyun.vod20170321.models.GenerateKMSDataKeyRequest generateKMSDataKeyRequest = new com.aliyun.vod20170321.models.GenerateKMSDataKeyRequest(); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); try { // 复制代码运行请自行打印 API 的返回值 GenerateKMSDataKeyResponse generateKMSDataKeyResponse = client.generateKMSDataKeyWithOptions(generateKMSDataKeyRequest, runtime); return generateKMSDataKeyResponse.getBody(); } catch (TeaException error) { // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message System.out.println(error.getMessage()); // 诊断地址 System.out.println(error.getData().get("Recommend")); com.aliyun.teautil.Common.assertAsString(error.message); return null; } catch (Exception _error) { TeaException error = new TeaException(_error.getMessage(), _error); // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message System.out.println(error.getMessage()); // 诊断地址 System.out.println(error.getData().get("Recommend")); com.aliyun.teautil.Common.assertAsString(error.message); return null; } } public static DecryptKMSDataKeyResponseBody enKMS(String kms){ com.aliyun.vod20170321.models.DecryptKMSDataKeyRequest decryptKMSDataKeyRequest = new com.aliyun.vod20170321.models.DecryptKMSDataKeyRequest(); decryptKMSDataKeyRequest.setCipherText(kms); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); try { // 复制代码运行请自行打印 API 的返回值 DecryptKMSDataKeyResponse decryptKMSDataKeyResponse = client.decryptKMSDataKeyWithOptions(decryptKMSDataKeyRequest, runtime); return decryptKMSDataKeyResponse.getBody(); } catch (TeaException error) { // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message System.out.println(error.getMessage()); // 诊断地址 System.out.println(error.getData().get("Recommend")); com.aliyun.teautil.Common.assertAsString(error.message); return null; } catch (Exception _error) { TeaException error = new TeaException(_error.getMessage(), _error); // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message System.out.println(error.getMessage()); // 诊断地址 System.out.println(error.getData().get("Recommend")); com.aliyun.teautil.Common.assertAsString(error.message); return null; } } public static void startM3u8(String vid,String edk){ com.aliyun.vod20170321.models.SubmitTranscodeJobsRequest submitTranscodeJobsRequest = new com.aliyun.vod20170321.models.SubmitTranscodeJobsRequest(); submitTranscodeJobsRequest.setVideoId(vid); submitTranscodeJobsRequest.setTemplateGroupId("d346d2609a058b9dfd9b8bb392175721"); JSONObject jsonObject = new JSONObject(); jsonObject.put("CipherText",edk); jsonObject.put("DecryptKeyUri","http://api.nuttyreading.com/sys/vodAli/vodAliVideoRe?CipherText="+edk); jsonObject.put("KeyServiceType","KMS"); submitTranscodeJobsRequest.setEncryptConfig(jsonObject.toJSONString()); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); try { // 复制代码运行请自行打印 API 的返回值 client.submitTranscodeJobsWithOptions(submitTranscodeJobsRequest, runtime); } catch (TeaException error) { // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message System.out.println(error.getMessage()); // 诊断地址 System.out.println(error.getData().get("Recommend")); com.aliyun.teautil.Common.assertAsString(error.message); } catch (Exception _error) { TeaException error = new TeaException(_error.getMessage(), _error); // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message System.out.println(error.getMessage()); // 诊断地址 System.out.println(error.getData().get("Recommend")); com.aliyun.teautil.Common.assertAsString(error.message); } } public static GetPlayInfoResponseBody getUrl(String vid){ com.aliyun.vod20170321.models.GetPlayInfoRequest getPlayInfoRequest = new com.aliyun.vod20170321.models.GetPlayInfoRequest(); getPlayInfoRequest.setVideoId(vid); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); try { // 复制代码运行请自行打印 API 的返回值 GetPlayInfoResponse playInfoWithOptions = client.getPlayInfoWithOptions(getPlayInfoRequest, runtime); GetPlayInfoResponseBody body = playInfoWithOptions.getBody(); return body; } catch (TeaException error) { // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message System.out.println(error.getMessage()); // 诊断地址 System.out.println(error.getData().get("Recommend")); com.aliyun.teautil.Common.assertAsString(error.message); return null; } catch (Exception _error) { TeaException error = new TeaException(_error.getMessage(), _error); // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 // 错误 message System.out.println(error.getMessage()); // 诊断地址 System.out.println(error.getData().get("Recommend")); com.aliyun.teautil.Common.assertAsString(error.message); return null; } } public static AssumeRoleResponse assumeRole() throws ClientException { try { String accessKeyId = "LTAI5tC9d38msYxw6RSEwJJR"; String accessKeySecret = "njJxkvrwBmbMfGyhAFeyjthodnwt58"; String roleArn = "acs:ram::1604740137891907:role/vodrole"; String roleSessionName = "testsession"; String policy = "{\n" + " \"Version\": \"1\",\n" + " \"Statement\": [\n" + " {\n" + " \"Action\": \"vod:*\",\n" + " \"Resource\": \"*\",\n" + " \"Effect\": \"Allow\"\n" + " }\n" + " ]\n" + "}"; //构造default profile(参数留空,无需添加Region ID) /* 说明:当设置SysEndpoint为sts.aliyuncs.com时,regionId可填可不填;反之,regionId必填,根据使用的服务区域填写,例如:cn-shanghai 详情参考STS各地域的Endpoint。 */ IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret); //用profile构造client DefaultAcsClient client = new DefaultAcsClient(profile); // 创建一个 AssumeRoleRequest 并设置请求参数 final AssumeRoleRequest request = new AssumeRoleRequest(); // request.setSysEndpoint("sts.aliyuncs.com"); // request.setSysMethod(MethodType.POST); request.setRoleArn(roleArn); request.setRoleSessionName(roleSessionName); request.setPolicy(policy); request.setRegionId("cn-shanghai"); // 发起请求,并得到response final AssumeRoleResponse response = client.getAcsResponse(request); return response; } catch (ClientException e) { throw e; } } }