فهرست منبع

增加mps服务

xuzuoyun 5 سال پیش
والد
کامیت
55d0855219
1فایلهای تغییر یافته به همراه252 افزوده شده و 0 حذف شده
  1. 252 0
      module-common/src/main/java/cn/com/ctop/common/module/utils/MpsUtils.java

+ 252 - 0
module-common/src/main/java/cn/com/ctop/common/module/utils/MpsUtils.java

@@ -0,0 +1,252 @@
+package cn.com.ctop.common.module.utils;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.aliyuncs.DefaultAcsClient;
+import com.aliyuncs.IAcsClient;
+import com.aliyuncs.exceptions.ClientException;
+import com.aliyuncs.exceptions.ServerException;
+import com.aliyuncs.mts.model.v20140618.*;
+import com.aliyuncs.profile.DefaultProfile;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.text.SimpleDateFormat;
+import java.util.*;
+
+@Configuration
+@PropertySource("classpath:config.properties")
+public class MpsUtils {
+    public static final String ossLocation = "oss-cn-beijing";
+    public static final String ossBucket = "ctop-part";
+    public static void main(String[] args){
+        String[] a = {"1","2","3","4","5"};
+        combinationSelect(a,3);
+        arrangementSelect(a,3);
+    }
+    /**
+     * 排列计算公式A<sup>m</sup><sub>n</sub> = n!/(n - m)!
+     * @param m
+     * @param n
+     * @return
+     */
+    public static long arrangement(int m, int n) {
+        return m <= n ? factorial(n) / factorial(n - m) : 0;
+    }
+
+    /**
+     * 排列选择(从列表中选择n个排列)
+     * @param dataList 待选列表
+     * @param n 选择个数
+     */
+    public static void arrangementSelect(String[] dataList, int n) {
+        System.out.println(String.format("A(%d, %d) = %d", dataList.length, n,
+                arrangement(n, dataList.length)));
+        arrangementSelect(dataList, new String[n], 0);
+    }
+
+    /**
+     * 排列选择
+     * @param dataList 待选列表
+     * @param resultList 前面(resultIndex-1)个的排列结果
+     * @param resultIndex 选择索引,从0开始
+     */
+    private static void arrangementSelect(String[] dataList, String[] resultList, int resultIndex) {
+        int resultLen = resultList.length;
+        if (resultIndex >= resultLen) { // 全部选择完时,输出排列结果
+            System.out.println(Arrays.asList(resultList));
+            return;
+        }
+
+        // 递归选择下一个
+        for (int i = 0; i < dataList.length; i++) {
+            // 判断待选项是否存在于排列结果中
+            boolean exists = false;
+            for (int j = 0; j < resultIndex; j++) {
+                if (dataList[i].equals(resultList[j])) {
+                    exists = true;
+                    break;
+                }
+            }
+            if (!exists) { // 排列结果不存在该项,才可选择
+                resultList[resultIndex] = dataList[i];
+                arrangementSelect(dataList, resultList, resultIndex + 1);
+            }
+        }
+    }
+
+    /**
+     * 计算阶乘数,即n! = n * (n-1) * ... * 2 * 1
+     * @param n
+     * @return
+     */
+    private static long factorial(int n) {
+        long sum = 1;
+        while( n > 0 ) {
+            sum = sum * n--;
+        }
+        return sum;
+    }
+
+    /**
+     * 组合计算公式C<sup>m</sup><sub>n</sub> = n! / (m! * (n - m)!)
+     * @param m
+     * @param n
+     * @return
+     */
+    public static long combination(int m, int n) {
+        return m <= n ? factorial(n) / (factorial(m) * factorial((n - m))) : 0;
+    }
+
+    /**
+     * 组合选择(从列表中选择n个组合)
+     * @param dataList 待选列表
+     * @param n 选择个数
+     */
+    public static void combinationSelect(String[] dataList, int n) {
+        System.out.println(String.format("C(%d, %d) = %d",
+                dataList.length, n, combination(dataList.length, n)));
+        combinationSelect(dataList, 0, new String[n], 0);
+    }
+
+    /**
+     * 组合选择
+     * @param dataList 待选列表
+     * @param dataIndex 待选开始索引
+     * @param resultList 前面(resultIndex-1)个的组合结果
+     * @param resultIndex 选择索引,从0开始
+     */
+    private static void combinationSelect(String[] dataList, int dataIndex, String[] resultList, int resultIndex) {
+        int resultLen = resultList.length;
+        int resultCount = resultIndex + 1;
+        if (resultCount > resultLen) { // 全部选择完时,输出组合结果
+            System.out.println(Arrays.asList(resultList));
+            return;
+        }
+
+        // 递归选择下一个
+        for (int i = dataIndex; i < dataList.length + resultCount - resultLen; i++) {
+            resultList[resultIndex] = dataList[i];
+            combinationSelect(dataList, i + 1, resultList, resultIndex + 1);
+        }
+    }
+
+    public IAcsClient getClient(){
+        DefaultProfile profile = DefaultProfile.getProfile(
+                "cn-beijing",      // 地域ID
+                "LTAIbNbqWzSOklQV",      // RAM账号的AccessKey ID
+                "1rkPz7JNoXk8sJevPaeYHWqfkQXBGh"); // RAM账号Access Key Secret
+        IAcsClient client = new DefaultAcsClient(profile);
+        return client;
+    }
+
+    public String mergeOneVideo(String startPart,List<String> videoPart,String endPart){
+        IAcsClient client = getClient();
+        String pipelineId = getPipelineId(client);
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        String outputFilename = "output/"+sdf.format(new Date())+"/"+ UUID.randomUUID()+".mp4";
+        List<String> videos = new ArrayList<>();
+        videos.add(startPart);
+        videos.addAll(videoPart);
+        videos.add(endPart);
+        mergeVideo(client,pipelineId,videos,outputFilename);
+        return outputFilename;
+    }
+
+    public String mergeVideo(IAcsClient client, String pipelineId, List<String> videos, String outputName){
+        String jobId = null;
+        SubmitJobsRequest request = new SubmitJobsRequest();
+        JSONArray mergeList = new JSONArray();
+        if (videos != null && videos.size() > 0){
+            for (int i = 0; i < videos.size();i++){
+                String url = videos.get(i);
+                if (i == 0){
+                    JSONObject input = new JSONObject();
+                    input.put("Location", ossLocation);
+                    input.put("Bucket", ossBucket);
+                    try {
+                        input.put("Object", URLEncoder.encode(url, "utf-8"));
+                    } catch (UnsupportedEncodingException e) {
+                        throw new RuntimeException("input URL encode failed");
+                    }
+                    request.setInput(input.toJSONString());
+                }else {
+                    JSONObject mergeVideo = new JSONObject();
+                    String mergeVideoURL;
+                    try {
+                        mergeVideoURL = String.format(
+                                "http://%s.%s.aliyuncs.com/%s",
+                                ossBucket,
+                                ossLocation,
+                                URLEncoder.encode(url, "utf-8"));
+                    } catch (UnsupportedEncodingException e) {
+                        throw new RuntimeException("mergeVideoURL encode failed");
+                    }
+                    mergeVideo.put("MergeURL", mergeVideoURL);
+                    mergeList.add(mergeVideo);
+                }
+            }
+        }
+        String outputOSSObject;
+        try {
+            outputOSSObject = URLEncoder.encode(outputName, "utf-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException("output URL encode failed");
+        }
+        JSONObject output = new JSONObject();
+        output.put("OutputObject", outputOSSObject);
+        JSONObject video = new JSONObject();
+        video.put("Width", "1280");
+        video.put("Height", "720");
+        output.put("Video", video.toJSONString());
+        output.put("MergeList", mergeList.toJSONString());
+        // Outputs
+        JSONArray outputs = new JSONArray();
+        outputs.add(output);
+        request.setOutputs(outputs.toJSONString());
+        request.setOutputBucket(ossBucket);
+        request.setOutputLocation(ossLocation);
+        // PipelineId
+        request.setPipelineId(pipelineId);
+        // call api
+        SubmitJobsResponse response;
+        try {
+            response = client.getAcsResponse(request);
+            System.out.println("RequestId is:"+response.getRequestId());
+            if (response.getJobResultList().get(0).getSuccess()) {
+                jobId = response.getJobResultList().get(0).getJob().getJobId();
+                System.out.println("JobId is:" + response.getJobResultList().get(0).getJob().getJobId());
+            } else {
+                System.out.println("SubmitJobs Failed code:" + response.getJobResultList().get(0).getCode() +
+                        " message:" + response.getJobResultList().get(0).getMessage());
+            }
+        } catch (ServerException e) {
+            e.printStackTrace();
+        } catch (ClientException e) {
+            e.printStackTrace();
+        }
+        return jobId;
+    }
+
+    public String getPipelineId(IAcsClient client){
+        String pipelineId = null;
+        // 创建API请求并设置参数
+        SearchPipelineRequest request = new SearchPipelineRequest();
+        // 发起请求并处理应答或异常
+        SearchPipelineResponse response;
+        try {
+            response = client.getAcsResponse(request);
+            pipelineId = response.getPipelineList().get(0).getId();
+            System.out.println("PipelineName is:" + response.getPipelineList().get(0).getName());
+            System.out.println("PipelineId is:" + response.getPipelineList().get(0).getId());
+        } catch (ServerException e) {
+            e.printStackTrace();
+        } catch (ClientException e) {
+            e.printStackTrace();
+        }
+        return pipelineId;
+    }
+}