All Products
Search
Document Center

ApsaraVideo Media Processing:Create a media workflow

Last Updated:Apr 07, 2024

If you want to produce videos that involve multiple bitrates, audio tracks, subtitles, and formats, or want to run multiple jobs based on the specified sequence or conditions, you can create a workflow. In the workflow, you can configure nodes for running jobs such as transcoding, analysis, snapshot, packaging or encapsulation, review, media fingerprint extraction, and intelligent thumbnail configuration. This topic provides the sample code for using ApsaraVideo Media Processing (MPS) SDK for Java to create a media workflow.

Sample code

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;

/**
 * *****   Usage notes     ******
 * In this demo, a media workflow is created.
 * The topology of the workflow is complex. You must pay attention to the accuracy of parameter settings when you modify the topology. For more information, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-mts-2014-06-18-addmediaworkflow.
 */
public class AddMediaWorkflow {

    /**The ID of the MPS queue. To view the ID, you can log on to the MPS console and choose Global Settings > MPS Queue and Callback in the left-side navigation pane.*/
    final static String PIPELINE_ID = "b40a0cbf******";
    /**The ID of the template. For more information about preset templates, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/yuzhimobanxiangqing.*/
    final static String TEMPLATE_ID = "S00000001-100020";
    final static String OSS_LOCATION = "oss-cn-beijing";
    /**The name of the input media bucket that is specified in the MPS console.*/
    final static String INPUT_BUCKET = "<your bucket name>";
    /**The trigger path. The path name must end with a forward slash (/).*/
    final static String INPUT_PATH = "mps-test/demo/";
    /**The name of the output media bucket that is specified in the MPS console.*/
    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 {

        // Initialize the SDK 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("Transcoding workflow");
        return client.getAcsResponse(request);
    }

    /**
     * Create the topology of the media workflow.
     * @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;
    }

    /**
     * Add a start node to the media 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;
    }

    /**
     * Add the input configurations.
     * @return
     */
    private static JSONObject buildStartParameters() {
        JSONObject parameters = new JSONObject();
        parameters.put("PipelineId", PIPELINE_ID);
        // The structure of the input media bucket.
        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;
    }

    /**
     * Add a transcoding node to the media workflow.
     * @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;
    }

    /**
     * Add the output configurations.
     * @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;
    }

    /**
     * Add the configurations of the Report node.
     * @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;
    }

    /**
     * Add dependencies.
     * The edges in the topology indicate the dependencies among activities. 
     * @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;
    }
}

References