|
@@ -0,0 +1,224 @@
|
|
|
+package cn.com.ctop.common.module.utils;
|
|
|
+
|
|
|
+import cn.com.ctop.common.module.constant.CosConstant;
|
|
|
+import cn.hutool.core.map.MapUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.qcloud.cos.auth.BasicCOSCredentials;
|
|
|
+import com.qcloud.cos.auth.COSCredentials;
|
|
|
+import com.qcloud.cos.auth.COSSigner;
|
|
|
+import com.qcloud.cos.http.HttpMethodName;
|
|
|
+import com.tencentcloudapi.common.Credential;
|
|
|
+import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
|
|
+import com.tencentcloudapi.common.profile.ClientProfile;
|
|
|
+import com.tencentcloudapi.common.profile.HttpProfile;
|
|
|
+import com.tencentcloudapi.mps.v20190612.MpsClient;
|
|
|
+import com.tencentcloudapi.mps.v20190612.models.*;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.PropertySource;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.web.client.RestClientException;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by JQ.bi on 2020.6.28
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@PropertySource("classpath:config.properties")
|
|
|
+public class CloudVideoProcessUtil {
|
|
|
+
|
|
|
+ private static RestTemplate restTemplate=new RestTemplate();
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(CloudVideoProcessUtil.class);
|
|
|
+
|
|
|
+ private static final String SECRET_ID="AKIDE6IpMi8fJQRCg1iuWzFajjRs43kbbets";
|
|
|
+ private static final String SECRET_KEY="tXzuwMfplTTK3c9GFUyETilasvQfePu9";
|
|
|
+ private static final String ENDPOINT = "mps.tencentcloudapi.com";
|
|
|
+ private static final String COS_LOCATION = "ap-chongqing";
|
|
|
+ private static final String COS_BUCKET_MEDIA = "media-1301855440";
|
|
|
+ private static final String COS_BUCKET_PART = "part-1301855440";
|
|
|
+
|
|
|
+
|
|
|
+ public static DescribeTaskDetailResponse getTaskDetailResponse(String taskId) {
|
|
|
+ MpsClient client = getClient();
|
|
|
+ DescribeTaskDetailRequest describeTaskDetailRequest = new DescribeTaskDetailRequest();
|
|
|
+ describeTaskDetailRequest.setTaskId(taskId);
|
|
|
+ DescribeTaskDetailResponse resp = null;
|
|
|
+ try {
|
|
|
+ resp = client.DescribeTaskDetail(describeTaskDetailRequest);
|
|
|
+ } catch (TencentCloudSDKException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发起视频水印处理请求
|
|
|
+ */
|
|
|
+ public static String videoWatermarkHandle(String videoPath, Long watermarkTemplateId) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ MpsClient client = getClient();
|
|
|
+ //发起视频处理请求
|
|
|
+ ProcessMediaRequest request = new ProcessMediaRequest();
|
|
|
+ TranscodeTaskInput transcodeTaskInput = new TranscodeTaskInput();
|
|
|
+ transcodeTaskInput.setOutputObjectPath("/watermark/" + sdf.format(new Date()) + "/" + UUID.randomUUID() + ".mp4");
|
|
|
+ //水印模板信息
|
|
|
+ WatermarkInput watermarkInput = new WatermarkInput();
|
|
|
+ watermarkInput.setDefinition(watermarkTemplateId);
|
|
|
+ //指定视频转码模板
|
|
|
+ transcodeTaskInput.setDefinition(30L);
|
|
|
+ transcodeTaskInput.setWatermarkSet(new WatermarkInput[]{watermarkInput});
|
|
|
+ MediaProcessTaskInput mediaProcessTaskInput = new MediaProcessTaskInput();
|
|
|
+ mediaProcessTaskInput.setTranscodeTaskSet(new TranscodeTaskInput[]{transcodeTaskInput});
|
|
|
+ request.setMediaProcessTask(mediaProcessTaskInput);
|
|
|
+ //视频输入
|
|
|
+ CosInputInfo cosInputInfo = new CosInputInfo();
|
|
|
+ cosInputInfo.setRegion(COS_LOCATION);
|
|
|
+ cosInputInfo.setBucket(COS_BUCKET_PART);
|
|
|
+ cosInputInfo.setObject(videoPath);
|
|
|
+ MediaInputInfo mediaInputInfo = new MediaInputInfo();
|
|
|
+ mediaInputInfo.setType("COS");
|
|
|
+ mediaInputInfo.setCosInputInfo(cosInputInfo);
|
|
|
+ request.setInputInfo(mediaInputInfo);
|
|
|
+ //加水印视频输出
|
|
|
+ CosOutputStorage cosOutputStorage = new CosOutputStorage();
|
|
|
+ cosOutputStorage.setBucket(COS_BUCKET_MEDIA);
|
|
|
+ cosOutputStorage.setRegion(COS_LOCATION);
|
|
|
+ TaskOutputStorage taskOutputStorage = new TaskOutputStorage();
|
|
|
+ taskOutputStorage.setType("COS");
|
|
|
+ taskOutputStorage.setCosOutputStorage(cosOutputStorage);
|
|
|
+ request.setOutputStorage(taskOutputStorage);
|
|
|
+ //指定输出路径
|
|
|
+ ProcessMediaResponse response = null;
|
|
|
+ String taskId = "";
|
|
|
+ try {
|
|
|
+ response = client.ProcessMedia(request);
|
|
|
+ } catch (TencentCloudSDKException e) {
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ }
|
|
|
+ if (null != response) {
|
|
|
+ taskId = response.getTaskId();
|
|
|
+ }
|
|
|
+ return taskId;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询数据万象任务状态
|
|
|
+ */
|
|
|
+ public static Map<String,Object> getMediaJobsResponse(String jobId){
|
|
|
+ String auth =generateAuth(SECRET_ID,SECRET_KEY,"/jobs/"+jobId,HttpMethodName.GET);
|
|
|
+ if(auth.isEmpty()){
|
|
|
+ logger.error("腾讯云Authorization获取失败");
|
|
|
+ }
|
|
|
+ //请求media-1301855440.ci.ap-chongqing.myqcloud.com/jobs/jobId 接口
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.set("Authorization", auth);
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<>(headers);
|
|
|
+ String response = null;
|
|
|
+ try {
|
|
|
+ response = restTemplate.exchange(CosConstant.URL_JOBS+"/"+jobId,HttpMethod.GET,requestEntity,String.class).getBody();
|
|
|
+ } catch (RestClientException e) {
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ }
|
|
|
+ Map<String,Object> mapResp= XmlToMapUtil.multilayerXmlToMap(response);
|
|
|
+ Map JobsDetail= (Map)((Map)mapResp.get("Response")).get("JobsDetail");
|
|
|
+ if(JobsDetail.get("Code").equals("Success")){
|
|
|
+ return JobsDetail;
|
|
|
+ }else {
|
|
|
+ return MapUtil.newHashMap();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 数据万象发起视频抽帧处理请求
|
|
|
+ */
|
|
|
+ public static Map<String,Object> videoCutFrameHandle(String videoPath, String outputPath) {
|
|
|
+
|
|
|
+ String auth =generateAuth(SECRET_ID,SECRET_KEY,"/jobs",HttpMethodName.POST);
|
|
|
+ if(auth.isEmpty()){
|
|
|
+ logger.error("腾讯云Authorization获取失败");
|
|
|
+ }
|
|
|
+ //拼接请求数据万象jobs接口的参数
|
|
|
+ JSONObject input = new JSONObject();
|
|
|
+ input.put("Object", videoPath);
|
|
|
+ JSONObject output = new JSONObject();
|
|
|
+ output.put("Region", COS_LOCATION);
|
|
|
+ output.put("Bucket", COS_BUCKET_MEDIA);
|
|
|
+ output.put("Object", outputPath);
|
|
|
+ JSONObject operation = new JSONObject();
|
|
|
+ operation.put("TemplateId", CosConstant.TEMPLATE_ID);
|
|
|
+ operation.put("Output", output);
|
|
|
+ JSONObject request = new JSONObject();
|
|
|
+ request.put("Tag", "Snapshot");
|
|
|
+ request.put("Input", input);
|
|
|
+ request.put("Operation", operation);
|
|
|
+ request.put("QueueId", CosConstant.QUEUE_ID);
|
|
|
+
|
|
|
+ //请求media-1301855440.ci.ap-chongqing.myqcloud.com/jobs 接口
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.set("Authorization", auth);
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<>(request.toJSONString(), headers);
|
|
|
+ String response = null;
|
|
|
+ try {
|
|
|
+ response = restTemplate.exchange(CosConstant.URL_JOBS, HttpMethod.POST, requestEntity, String.class).getBody();
|
|
|
+ } catch (RestClientException e) {
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ }
|
|
|
+ Map<String,Object> mapResp= XmlToMapUtil.multilayerXmlToMap(response);
|
|
|
+ Map JobsDetail= (Map)((Map)mapResp.get("Response")).get("JobsDetail");
|
|
|
+ if(JobsDetail.get("Code").equals("Success")){
|
|
|
+ return JobsDetail;
|
|
|
+ }else {
|
|
|
+ return MapUtil.newHashMap();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取视频处理服务 MpsClient
|
|
|
+ */
|
|
|
+ private static MpsClient getClient() {
|
|
|
+ MpsClient mpsClient = null;
|
|
|
+ try {
|
|
|
+ Credential cred = new Credential(SECRET_ID, SECRET_KEY);
|
|
|
+ HttpProfile httpProfile = new HttpProfile();
|
|
|
+ httpProfile.setEndpoint(ENDPOINT);
|
|
|
+ ClientProfile clientProfile = new ClientProfile();
|
|
|
+ clientProfile.setHttpProfile(httpProfile);
|
|
|
+ mpsClient = new MpsClient(cred, COS_LOCATION, clientProfile);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ logger.error(ex.getMessage());
|
|
|
+ }
|
|
|
+ return mpsClient;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取数据万象认证Auth
|
|
|
+ * @param secretId
|
|
|
+ * @param secretKey
|
|
|
+ * @param path
|
|
|
+ * @param httpMethodName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String generateAuth(String secretId, String secretKey, String path, HttpMethodName httpMethodName) {
|
|
|
+ COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
|
|
+ COSSigner signer = new COSSigner();
|
|
|
+ //设置过期时间为1个小时
|
|
|
+ Date expiredTime = new Date(System.currentTimeMillis() + 3600L * 1000L);
|
|
|
+ //授权的签名
|
|
|
+ return signer.buildAuthorizationStr(
|
|
|
+ httpMethodName,
|
|
|
+ path,
|
|
|
+ cred,
|
|
|
+ expiredTime
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|