All Products
Search
Document Center

ApsaraVideo Media Processing:Transcode media files

Last Updated:Mar 07, 2024

Transcoding is to convert an audio or video file into one or more audio or video files to adapt to different network bandwidths, terminal devices, and user needs. If the transcoding jobs and workflows created in the ApsaraVideo Media Processing (MPS) console cannot meet your business requirements, you can call the SubmitJobs operation to submit transcoding jobs. This topic provides examples on how to use MPS SDK for Java to submit a transcoding job, cancel a transcoding job, query transcoding jobs, and traverse all transcoding jobs by calling API operations.

Prerequisites

The SDK client is created. For more information, see Initialize a client.

Submit a transcoding job

You can call the SubmitJobs to submit a transcoding job.

Note
  • When you submit a transcoding job by using the SDK, you must perform URL encoding on the objects. Otherwise, the transcoding job fails. For more information, see URL encoding.

  • You must specify an object name that conforms to the naming conventions. Otherwise, the object cannot be found and the transcoding job fails. For more information, see Parameter details.

  • We recommend that you record the ID of the transcoding job that you submit. This facilitates subsequent query and traverse operations.

/**
 * Submit a transcoding job
 * @param client
 * @return
 * @throws Exception
 */
public static SubmitJobsResponse submitJobs(DefaultAcsClient client) throws Exception {

    SubmitJobsRequest request = new SubmitJobsRequest();
    // Construct output parameters.
    JSONArray outputs = new JSONArray();

    // Construct an input object. Make sure that the value of the Location parameter is the region in which the client is deployed.
    JSONObject input = new JSONObject();
    input.put("Location", "oss-cn-beijing");
    input.put("Bucket", "<your bucket name>");

    // Construct an output object.
    JSONObject output = new JSONObject();
    try {
        input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
        String outPutObject = URLEncoder.encode("mps-test/demo/test-out.mp4", "utf-8");
        output.put("OutputObject", outPutObject);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("input URL encode failed");
    }
    output.put("TemplateId", templateId);
    outputs.add(output);

    request.setInput(input.toJSONString());
    request.setOutputs(outputs.toJSONString());
    // Specify an output bucket.
    request.setOutputBucket("<your bucket name>");
    // Specify the region in which the output bucket resides. The region must be in the oss-cn-**** format. Example: oss-cn-beijing.
    request.setOutputLocation("oss-cn-beijing");
    // PipelineId
    request.setPipelineId(pipelineId);

    return client.getAcsResponse(request);
}

Query transcoding jobs

You can call the QueryJobList operation to query the information about transcoding jobs.

/**
 * Query transcoding jobs
 * @param client
 * @return
 * @throws Exception
 */
public static QueryJobListResponse queryJobList(DefaultAcsClient client) throws Exception {

    QueryJobListRequest request = new QueryJobListRequest();
    // You can query up to 10 transcoding jobs at a time. Separate multiple job IDs with commas (,).
    request.setJobIds("606179a72c64e572e****");

    return client.getAcsResponse(request);
}

Cancel a transcoding job

You can call the CancelJob operation to cancel a transcoding job.

/**
 * Cancel a transcoding job
 * @param client
 * @return
 * @throws Exception
 */
public static CancelJobResponse cancelJob(DefaultAcsClient client) throws Exception {

    CancelJobRequest request = new CancelJobRequest();
    request.setJobId("606179a72c64ee2b6****");

    return client.getAcsResponse(request);
}

Traverse all transcoding jobs

You can call the ListJob operation to traverse all transcoding jobs.

/**
 * Traverse all transcoding jobs
 * @param client
 * @return
 * @throws Exception
 */
public static ListJobResponse listJob(DefaultAcsClient client) throws Exception {

    ListJobRequest request = new ListJobRequest();
    request.setState("All");

    return client.getAcsResponse(request);
}

Sample code

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.*;
import com.aliyuncs.profile.DefaultProfile;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * ***** Usage notes ******
 * The demo code shows only basic transcoding operations. Operations such as watermarking and merging are not involved.
 *
 * ***** References ******
 * For more information about the SubmitJobs operation, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-submitjobs.
 * For more information about the QueryJobList operation, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-queryjoblist.
 * For more information about the CancelJob operation, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-canceljob.
 * For more information about the ListJob operation, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-doc-mts-2014-06-18-api-doc-listjob.
 * For more information about the parameters, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a.
 *
 * ***** main method ******
 * In this example, only the SubmitJobs operation is called. If you want to call another operation, replace the response.
 */
public class TransCodeJobs {

    // The ID of the MPS queue to which the job is submitted. To view the ID, log on to the MPS console and choose Global Settings > MPS Queue and Callback in the left-side navigation pane.
    private static String pipelineId = "bee7a540a0cbf4****";
    // The ID of the template. For more information about preset templates, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/yuzhimobanxiangqing.
    private static String templateId = "S00000001-200010";

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

        // Initialize a client.
        DefaultAcsClient client = InitClient.initMpsClient();

        SubmitJobsResponse response;
        try {
            response = submitJobs(client);
            System.out.println("RequestId is:"+response.getRequestId());
            // If multiple output objects exist, traverse the objects by calling the getJobResultList() method to obtain the result.
            if (response.getJobResultList().get(0).getSuccess()) {
                System.out.println("JobId is:" + response.getJobResultList().get(0).getJob().getJobId());
                System.out.println("Response is:" + JSON.toJSONString(response));
            } else {
                System.out.println("SubmitJobs Failed code:" + response.getJobResultList().get(0).getCode() +
                        " message:" + response.getJobResultList().get(0).getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Submit a transcoding job
     * @param client
     * @return
     * @throws Exception
     */
    public static SubmitJobsResponse submitJobs(DefaultAcsClient client) throws Exception {

        SubmitJobsRequest request = new SubmitJobsRequest();
        // Construct output parameters.
        JSONArray outputs = new JSONArray();

        // Construct an input object. Make sure that the value of the Location parameter is the region in which the client is deployed.
        JSONObject input = new JSONObject();
        input.put("Location", "oss-cn-beijing");
        input.put("Bucket", "<your bucket name>");

        // Construct an output object.
        JSONObject output = new JSONObject();
        try {
            input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
            String outPutObject = URLEncoder.encode("mps-test/demo/test-out.mp4", "utf-8");
            output.put("OutputObject", outPutObject);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("input URL encode failed");
        }
        output.put("TemplateId", templateId);
        outputs.add(output);

        request.setInput(input.toJSONString());
        request.setOutputs(outputs.toJSONString());
        // Specify an output bucket.
        request.setOutputBucket("<your bucket name>");
        // Specify the region in which the output bucket resides. The region must be in the oss-cn-**** format. Example: oss-cn-beijing.
        request.setOutputLocation("oss-cn-beijing");
        // PipelineId
        request.setPipelineId(pipelineId);

        return client.getAcsResponse(request);
    }

    /**
     * Query transcoding jobs
     * @param client
     * @return
     * @throws Exception
     */
    public static QueryJobListResponse queryJobList(DefaultAcsClient client) throws Exception {

        QueryJobListRequest request = new QueryJobListRequest();
        // You can query up to 10 transcoding jobs at a time. Separate multiple job IDs with commas (,).
        request.setJobIds("6061772c64e3c2****");

        return client.getAcsResponse(request);
    }

    /**
     * Cancel a transcoding job
     * @param client
     * @return
     * @throws Exception
     */
    public static CancelJobResponse cancelJob(DefaultAcsClient client) throws Exception {

        CancelJobRequest request = new CancelJobRequest();
        request.setJobId("60617872c64e53f9d796f****");

        return client.getAcsResponse(request);
    }

    /**
     * Traverse all transcoding jobs
     * @param client
     * @return
     * @throws Exception
     */
    public static ListJobResponse listJob(DefaultAcsClient client) throws Exception {

        ListJobRequest request = new ListJobRequest();
        request.setState("All");

        return client.getAcsResponse(request);
    }
}
            

References