All Products
Search
Document Center

ApsaraVideo Media Processing:Add a media file

Last Updated:Apr 07, 2024

After you add a media file to the media library of ApsaraVideo Media Processing (MPS), you can specify the ID of a workflow to process the media file. This topic provides the sample code for using MPS SDK for Java to add a media file to the media library and trigger a specific workflow to process the media file.

Note

If the directory of the media file that you want to add meets the triggering rules, the workflow is triggered. Otherwise, the workflow is not triggered. For more information, see Workflow triggering rules for files.

import com.alibaba.bltest.Config;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.AddMediaRequest;
import com.aliyuncs.mts.model.v20140618.AddMediaResponse;

/**
 * *****   Usage notes     ******
 * This sample code triggers a workflow to process an existing media file.
 */
public class AddMedia {

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

        DefaultAcsClient client = Config.initMpsClient();
        // Initiate an API request and handle the response or exception.
        AddMediaResponse response;
        try {
            response = addMedia(client);
            System.out.println("RequestId is:" + response.getRequestId());
            System.out.println("Media Info is:" + JSON.toJSON(response.getMedia()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static AddMediaResponse addMedia(DefaultAcsClient client) throws Exception{
        // Create an API request and specify the request parameters.
        AddMediaRequest request = new AddMediaRequest();
        // The URL of the media file for which you want to trigger a workflow.
        request.setFileURL("http://<your bucket name>.oss-cn-shanghai.aliyuncs.com/mps-test/video.mp4");
        request.setTitle("case title");
        // The ID of the workflow that you want to trigger. To obtain the ID of a workflow, log on to the MPS console. In the left-side navigation pane, choose Workflow > Workflow Orchestration.
        request.setMediaWorkflowId("2f8c24be9afc5585e****");
        System.out.println(JSON.toJSON(request));
        return client.getAcsResponse(request);
    }
}