All Products
Search
Document Center

ApsaraVideo VOD:Video AI

Last Updated:Aug 23, 2023

This topic provides examples on how to use the API operations of the video AI module. The API operations are encapsulated in ApsaraVideo VOD SDK for Java. You can call the API operations to submit AI jobs, set a default AI template, and query the default AI template. You can also create, delete, modify, and query AI templates.

Usage notes

  • In this example, an AccessKey pair is used to initialize a client instance.

  • For more information about the request and response parameters of this operation, visit OpenAPI Explorer. You can click API Documentation in the top navigation bar to view information related to the API operation.

  • This topic provides sample code only for some complex API operations. To obtain sample code for other API operations, perform the following operations: Visit Alibaba Cloud OpenAPI Explorer. In the left-side navigation pane, find the API operation whose sample code you want to obtain and specify the required parameters on the Parameters tab. Then, click Initiate Call. On the SDK Sample Code tab, select the language to view and download the sample code.

Initialize a client

Before you use the SDK, initialize a client. For more information, see Initialization.

Submit an AI job

You can call the SubmitAIJob operation to submit an AI job.

Click SubmitAIJob to learn more about this API operation.

Query AI jobs

You can call the ListAIJob operation to query AI jobs.

Click ListAIJob to learn more about this API operation.

Create an AI template

You can call the AddAITemplate operation to create an AI template.

Click AddAITemplate to learn more about this API operation.

Sample code:

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.AddAITemplateRequest;
import com.aliyuncs.vod.model.v20170321.AddAITemplateResponse;

/**
 * Obtain the AccessKey information.
 */
public static DefaultAcsClient initVodClient() throws ClientException {
    String regionId = "cn-shanghai";  // Specify the region in which ApsaraVideo VOD is activated.
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. 
    // We recommend that you not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
    // In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 
    DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    DefaultAcsClient client = new DefaultAcsClient(profile);
    return client;
}

/**
 * Create an AI template.
 */
public static AddAITemplateResponse addAITemplate(DefaultAcsClient client) throws Exception {
        AddAITemplateRequest request = new AddAITemplateRequest();
        // The template type. An automated review template is used as an example.
        request.setTemplateType("AIMediaAudit");
        // The name of the template.
        request.setTemplateName("myaitemplate");
        // The detailed configurations of the template.
        JSONObject templateConfig = new JSONObject();
        JSONArray auditItem = new JSONArray();
        auditItem.add("terrorism");
        auditItem.add("porn");
        templateConfig.put("AuditItem", auditItem);

        JSONArray auditRange = new JSONArray();
        auditRange.add("video");
        auditRange.add("image-cover");
        auditRange.add("text-title");
        templateConfig.put("AuditRange", auditRange);

        JSONArray auditContent = new JSONArray();
        auditContent.add("screen");
        templateConfig.put("AuditContent", auditContent);

        templateConfig.put("AuditAutoBlock", "no");
        request.setTemplateConfig(templateConfig.toString());

        // The returned results.
        return client.getAcsResponse(request);
}


/**
 * Sample code
 * @param args
 * @throws ClientException
 */
public static void main(String[] args) throws Exception {
        // Initialize a client.
        DefaultAcsClient client = initVodClient();

        try {
            // Obtain the results.
            AddAITemplateResponse response = addAITemplate(client);

            // Display the ID of the request.
            System.out.println("ResquestId:" + response.getRequestId());
            // Display the ID of the template.
            System.out.println("TemplateId:" + response.getTemplateId());
        } catch (Exception e) {
            System.out.println("ErrorMessage:" + e.getLocalizedMessage());
        }
}

Modify an AI template

You can call the UpdateAITemplate operation to modify an AI template.

Click UpdateAITemplate to learn more about this API operation.

Sample code:

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.UpdateAITemplateRequest;
import com.aliyuncs.vod.model.v20170321.UpdateAITemplateResponse;

/**
 * Obtain the AccessKey information.
 */
public static DefaultAcsClient initVodClient() throws ClientException {
    String regionId = "cn-shanghai";  // Specify the region in which ApsaraVideo VOD is activated.
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. 
    // We recommend that you not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
    // In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 
    DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    DefaultAcsClient client = new DefaultAcsClient(profile);
    return client;
}

/**
 * Modify an AI template.
 */
public static UpdateAITemplateResponse updateAITemplate(DefaultAcsClient client) throws Exception {
        UpdateAITemplateRequest request = new UpdateAITemplateRequest();
        // The ID of the template.
        request.setTemplateId("1d763dd8987a122ab8596eb7d257****");
        // The name of the template.
        request.setTemplateName("myaitemplate1");
        // The detailed configurations of the template.
        JSONObject templateConfig = new JSONObject();
        JSONArray auditItem = new JSONArray();
        //auditItem.add("terrorism");
        auditItem.add("porn");
        templateConfig.put("AuditItem", auditItem);

        JSONArray auditRange = new JSONArray();
        auditRange.add("video");
        auditRange.add("image-cover");
        //auditRange.add("text-title");
        templateConfig.put("AuditRange", auditRange);

        JSONArray auditContent = new JSONArray();
        auditContent.add("screen");
        templateConfig.put("AuditContent", auditContent);

        templateConfig.put("AuditAutoBlock", "no");
        request.setTemplateConfig(templateConfig.toString());

        // The returned results.
        return client.getAcsResponse(request);
}

/**
 * Sample code
 * @param args
 * @throws ClientException
 */
public static void main(String[] args) throws Exception {
        // Initialize a client.
        DefaultAcsClient client = initVodClient();

        try {
            // Obtain the results.
            UpdateAITemplateResponse response = updateAITemplate(client);

            // Display the ID of the request.
            System.out.println("ResquestId:" + response.getRequestId());
            // Display the ID of the template.
            System.out.println("TemplateId:" + response.getTemplateId());
        } catch (Exception e) {
            System.out.println("ErrorMessage:" + e.getLocalizedMessage());
        }
}

Query an AI template

  • You can call the GetAITemplate operation to query information about an AI template.

    Click GetAITemplate to learn more about this API operation.

  • You can call the GetAITemplate operation to query AI templates.

    Click ListAITemplate to learn more about this API operation.

Set a default AI template

You can call the SetDefaultAITemplate operation to set a default AI template.

Click SetDefaultAITemplate to learn more about this API operation.

Query the default AI template

You can call the GetDefaultAITemplate operation to query details of the default AI template.

Click GetDefaultAITemplate to learn more about this API operation.

Delete an AI template

You can call the DeleteAITemplate operation to delete an AI template.

Click DeleteAITemplate to learn more about this API operation.