新增媒体工作流

更新时间: 2023-07-31 09:34:09

您可以对MPS服务提供的活动(例如:转码、截图等活动)进行组装,拓扑结构如下。

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.mps.sdk.utils.InitClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.mts.model.v20140618.AddMediaWorkflowRequest;
import com.aliyuncs.mts.model.v20140618.AddMediaWorkflowResponse;

/**
 * *****   使用须知     ******
 * 本demo为创建工作流代码示例
 * 工作流数据结构较为复杂, 修改时请注意参数的准确性, 工作流数据结构参考 https://help.aliyun.com/document_detail/602860.html
 */
public class AddMediaWorkflow {

    /**管道ID, 可以在控制台 【全局设置】->【管道】查看*/
    final static String PIPELINE_ID = "b40a0cbf******";
    /**模板ID, 预置模板参考 https://help.aliyun.com/document_detail/29256.html*/
    final static String TEMPLATE_ID = "S00000001-100020";
    final static String OSS_LOCATION = "oss-cn-beijing";
    /**控制台绑定的的输入bucket名称*/
    final static String INPUT_BUCKET = "<your bucket name>";
    /**触发路径  以 / 结尾*/
    final static String INPUT_PATH = "mps-test/demo/";
    /**控制台绑定的的输出bucket名称*/
    final static String OUTPUT_BUCKET = "<your bucket name>";
    final static String ACT_START = "Act-Start";
    final static String ACT_REPORT = "Act-Report";

    public static void main(String[] args) throws Exception {

        //初始化调用 client
        DefaultAcsClient client = InitClient.initMpsClient();
        AddMediaWorkflowResponse response;
        try {
            response = addMediaWorkflow(client);
            System.out.println("RequestId is:" + response.getRequestId());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static AddMediaWorkflowResponse addMediaWorkflow(IAcsClient client) throws Exception {
        AddMediaWorkflowRequest request = new AddMediaWorkflowRequest();
        request.setTopology(createWorkflow().toJSONString());
        request.setName("转码工作流");
        return client.getAcsResponse(request);
    }

    /**
     * 创建工作流数据结构
     * @return
     */
    private static JSONObject createWorkflow() {
        JSONObject workflow = new JSONObject();
        JSONObject activities = new JSONObject();
        activities.put(ACT_START, createStartActivity());
        activities.put("TranscodNode", createTrancodeActivity());
        activities.put(ACT_REPORT, createReportActivity());
        workflow.put("Activities", activities);
        workflow.put("Dependencies", createDependencies());
        return workflow;
    }

    /**
     * 添加工作流开始节点
     * @return
     */
    private static JSONObject createStartActivity() {
        JSONObject startActivity = new JSONObject();
        startActivity.put("Name", ACT_START);
        startActivity.put("Type", "Start");
        startActivity.put("Parameters", buildStartParameters());
        return startActivity;
    }

    /**
     * 添加工作流输入配置
     * @return
     */
    private static JSONObject buildStartParameters() {
        JSONObject parameters = new JSONObject();
        parameters.put("PipelineId", PIPELINE_ID);
        //输入buket结构
        JSONObject inputFile = new JSONObject();
        inputFile.put("Bucket", INPUT_BUCKET);
        inputFile.put("Location", OSS_LOCATION);
        inputFile.put("ObjectPrefix", INPUT_PATH);

        parameters.put("InputFile", inputFile);
        return parameters;
    }

    /**
     * 添加工作流转码节点
     * @return
     */
    private static JSONObject createTrancodeActivity() {
        JSONObject transcodeActivity = new JSONObject();
        transcodeActivity.put("Name", "Transcode-test");
        transcodeActivity.put("Type", "Transcode");

        JSONObject transcodeParamters = new JSONObject();
        transcodeParamters.put("OutputBucket", OUTPUT_BUCKET);
        transcodeParamters.put("OutputLocation", OSS_LOCATION);
        transcodeParamters.put("Outputs", buildOutputsConfig());

        transcodeActivity.put("Parameters", transcodeParamters);
        return transcodeActivity;
    }

    /**
     * 添加工作流输出配置
     * @return
     */
    private  static JSONArray buildOutputsConfig() {
        JSONArray outputs = new JSONArray();
        JSONObject output = new JSONObject();
        output.put("ObjectRegex", "Transcode/{RunId}/{FileName}");
        output.put("TemplateId", TEMPLATE_ID);
        outputs.add(output);
        return outputs;
    }

    /**
     * 添加Report节点配置
     * @return
     */
    private static JSONObject createReportActivity() {
        JSONObject reportActivity = new JSONObject();
        reportActivity.put("Name", ACT_REPORT);
        JSONObject parameters = new JSONObject();
        parameters.put("PublishType","Auto");
        reportActivity.put("Parameters", parameters);
        reportActivity.put("Type", "Report");
        return  reportActivity;
    }

    /**
     * 添加工作流依赖关系
     * 依赖关系是拓扑结构中的边,指明活动之间的依赖。
     * @return
     */
    private static JSONObject createDependencies() {
        JSONObject dependencies = new JSONObject();
        JSONArray subActivityOfStart = new JSONArray();
        subActivityOfStart.add("TranscodNode");
        dependencies.put(ACT_START, subActivityOfStart);
        JSONArray subActivityOfTranscode = new JSONArray();
        subActivityOfTranscode.add(ACT_REPORT);
        dependencies.put("TranscodNode", subActivityOfTranscode);
        dependencies.put(ACT_REPORT, new JSONArray());
        return dependencies;
    }
}
阿里云首页 媒体处理 相关技术圈