All Products
Search
Document Center

ApsaraVideo Media Processing:Add a media file

Last Updated:Aug 28, 2023

This topic provides the sample code for using ApsaraVideo Media Processing (MPS) SDK for Java to add a video file to the media library. You can specify the ID of a workflow to process the video file.

Note

If the directory of the video 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 and choose Workflow > Workflow Orchestration in the left-side navigation pane.
        request.setMediaWorkflowId("2f8c24be9afc5585e****");
        System.out.println(JSON.toJSON(request));
        return client.getAcsResponse(request);
    }
}