All Products
Search
Document Center

ApsaraVideo VOD:Snapshot templates

Last Updated:Aug 22, 2023

This topic provides examples on how to use the API operations of the snapshot template module. The API operations are encapsulated in ApsaraVideo VOD SDK for Java. You can call API operations to create, delete, modify, and query snapshot 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.

Create a snapshot template

You can call the AddVodTemplate operation to create a snapshot template.

Click AddVodTemplate to learn more about this API operation.

Sample code:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.AddVodTemplateRequest;
import com.aliyuncs.vod.model.v20170321.AddVodTemplateResponse;

/** 
 * Obtain the AccessKey information.
 */
public static DefaultAcsClient initVodClient() throws ClientException {
    // Specify the region in which ApsaraVideo VOD is activated.
    String regionId = "cn-shanghai";  
    // 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 do not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked. As a result, the security of all resources in your account is compromised. 
    // In this example, the system reads the AccessKey pair from environment variables to implement authentication 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;
    }

/**
 * Construct the snapshot template parameters and modify the corresponding parameter values based on your business requirements.
 * The following sample code describes the complete configuration of image sprites.
 * @return The snapshot template configurations.
 */
public static JSONObject buildSnapshotTemplateConfig() {
    JSONObject templateConfig = new JSONObject();
    JSONObject snapshotConfig = new JSONObject();
    snapshotConfig.put("Count", "50");
    snapshotConfig.put("Interval", "1");
    snapshotConfig.put("SpecifiedOffsetTime", "0");
    snapshotConfig.put("Width", "200");
    snapshotConfig.put("Height", "200");
    snapshotConfig.put("FrameType", "normal");
    // The configurations of normal snapshots. By default, the configurations are also used for image sprites.
    templateConfig.put("SnapshotConfig", snapshotConfig);

    // The configurations of image sprites. The configurations of normal snapshots are included.
    JSONObject spriteSnapshotConfig = new JSONObject();
    spriteSnapshotConfig.put("CellWidth", "120");
    spriteSnapshotConfig.put("CellHeight", "68");
    spriteSnapshotConfig.put("Columns", "3");
    spriteSnapshotConfig.put("Lines", "10");
    spriteSnapshotConfig.put("Padding", "20");
    spriteSnapshotConfig.put("Margin", "50");
    spriteSnapshotConfig.put("KeepCellPic", "keep");
    spriteSnapshotConfig.put("Color", "tomato");
    snapshotConfig.put("SpriteSnapshotConfig", spriteSnapshotConfig);

    // The snapshot type. Set the value to SpriteSnapshot for image sprites and to NormalSnapshot for normal snapshots.
    templateConfig.put("SnapshotType", "SpriteSnapshot");
    return templateConfig;
}

/**
 * Add a snapshot template function.
 */
public static AddVodTemplateResponse addSnapshotVodTemplate(DefaultAcsClient client) throws Exception {
    AddVodTemplateRequest request = new AddVodTemplateRequest();
    // The name of the template.
    request.setName("Test to add a snapshot template");
    // The template type. Set the value to Snapshot.
    request.setTemplateType("Snapshot");
    // The template configurations.
    JSONObject templateConfig = buildSnapshotTemplateConfig();
    request.setTemplateConfig(templateConfig.toJSONString());
    return client.getAcsResponse(request);
}

/**
 * Sample code
 * @param args
 * @throws ClientException
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient();
    AddVodTemplateResponse response = new AddVodTemplateResponse();
    try {
        // Create a snapshot template.
        response = addSnapshotVodTemplate(client);
        // The ID of the snapshot template.
        System.out.println("SnapshotVodTemplateId = " + response.getVodTemplateId());
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

Modify a snapshot template

You can call the UpdateVodTemplate operation to modify a snapshot template.

Click UpdateVodTemplate to learn more about this API operation.

Sample code:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.UpdateVodTemplateRequest;
import com.aliyuncs.vod.model.v20170321.UpdateVodTemplateResponse;

/** 
 * Obtain the AccessKey information.
 */
public static DefaultAcsClient initVodClient() throws ClientException {
    // Specify the region in which ApsaraVideo VOD is activated.
    String regionId = "cn-shanghai";  
    // 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 do not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked. As a result, the security of all resources in your account is compromised. 
    // In this example, the system reads the AccessKey pair from environment variables to implement authentication 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;
    }

/**
 * Construct the snapshot template parameters and modify the corresponding parameter values based on your business requirements.
 * The following sample code describes the complete configuration of normal snapshots.
 * @return The snapshot template configurations.
 */
public static JSONObject buildSnapshotTemplateConfig() {
    JSONObject templateConfig = new JSONObject();
    JSONObject snapshotConfig = new JSONObject();
    snapshotConfig.put("Count", "50");
    snapshotConfig.put("Interval", "1");
    snapshotConfig.put("SpecifiedOffsetTime", "0");
    snapshotConfig.put("Width", "200");
    snapshotConfig.put("Height", "200");
    snapshotConfig.put("FrameType", "normal");
    // The configurations of normal snapshots. By default, the configurations are also used for image sprites.
    templateConfig.put("SnapshotConfig", snapshotConfig);

    // The snapshot type. Set the value to SpriteSnapshot for image sprites and to NormalSnapshot for normal snapshots.
    templateConfig.put("SnapshotType", "NormalSnapshot");
    return templateConfig;
}

/**
 * Modify a snapshot template.
 */
public static UpdateVodTemplateResponse updateSnapshotVodTemplate(DefaultAcsClient client) throws Exception {
    UpdateVodTemplateRequest request = new UpdateVodTemplateRequest();
    // The ID of the template that you want to modify.
    request.setVodTemplateId("53azf9d796fad9d7b862b2e****");
    // The name of the template.
    request.setName("Test to modify a snapshot template");
    // The template configurations.
    JSONObject templateConfig = buildSnapshotTemplateConfig();
    request.setTemplateConfig(templateConfig.toJSONString());
    return client.getAcsResponse(request);
}

/**
 * Sample code
 * @param args
 * @throws ClientException
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient();
    UpdateVodTemplateResponse response = new UpdateVodTemplateResponse();
    try {
        // Modify a snapshot template.
        response = updateSnapshotVodTemplate(client);
        // The ID of the snapshot template.
        System.out.println("SnapshotVodTemplateId = " + response.getVodTemplateId());
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

Query a snapshot template

  • You can call the GetVodTemplate operation to query a snapshot template.

    Click GetVodTemplate to learn more about this API operation.

  • You can call the ListVodTemplate operation to query a list of snapshot templates.

    Click ListVodTemplate to learn more about this API operation.

Delete a snapshot template

You can call the DeleteVodTemplate operation to delete a snapshot template.

Click DeleteVodTemplate to learn more about this API operation.