Transcoding templates

Updated at:

Video transcoding uses transcoding templates. If the built-in transcoding template groups in ApsaraVideo VOD do not meet your needs, you can create a new transcoding template group and customize parameters such as the encoding format, frame rate, and resolution. This topic shows you how to use the ApsaraVideo VOD software development kit (SDK) for Java to call API operations to create and manage transcoding template groups.

Usage notes

  • All examples in this topic use an AccessKey pair to initialize a client instance.

  • For request and response parameter details, visit Alibaba Cloud OpenAPI Explorer and open the API Documentations tab.

  • For SDK examples of other API operations, visit Alibaba Cloud OpenAPI Explorer. On the Parameters tab, specify the parameters and initiate the call. Then select the SDK version and language on the SDK Sample Code tab to view and download the code.

  • The examples in this topic use SDK V1.0. For V2.0 examples, select the correct SDK version in OpenAPI Explorer.

Initialize a client

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

Create a transcoding template group

You can call the AddTranscodeTemplateGroup operation to create a transcoding template group.

Alibaba Cloud OpenAPI Explorer: AddTranscodeTemplateGroup .

Sample code:

import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.AddTranscodeTemplateGroupRequest;
import com.aliyuncs.vod.model.v20170321.AddTranscodeTemplateGroupResponse;

/** 
 * 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;
    }

/**
 * Configure a transcoding template group.
 */
public static AddTranscodeTemplateGroupResponse addTranscodeTemplateGroup(DefaultAcsClient client) throws Exception {
    AddTranscodeTemplateGroupRequest request = new AddTranscodeTemplateGroupRequest();
    // The ID of the transcoding template.
    request.setName("grouptest");
    request.setTranscodeTemplateList(buildTranscodeTemplateList().toJSONString());
    return client.getAcsResponse(request);
}

/**
 * Set parameters required for adding a transcoding template.
 *
 * @return
 */
public static JSONArray buildTranscodeTemplateList() {
    JSONObject transcodeTemplate = new JSONObject();
    // The name of the template.
    transcodeTemplate.put("TemplateName", "testtemplate");

    // The resolution.
    transcodeTemplate.put("Definition", "LD");

    // The transcoding settings for video streams.
    JSONObject video = new JSONObject();
    video.put("Width", 640);
    video.put("Bitrate", 400);
    video.put("Fps", 25);
    video.put("Remove", false);
    video.put("Codec", "H.264");
    video.put("Gop", "250");
    transcodeTemplate.put("Video", video);

    // The transcoding settings for audio streams.
    JSONObject audio = new JSONObject();
    audio.put("Codec", "AAC");
    audio.put("Bitrate", "64");
    audio.put("Channels", "2");
    audio.put("Samplerate", "32000");
    transcodeTemplate.put("Audio", audio);

    // The container.
    JSONObject container = new JSONObject();
    container.put("Format", "mp4");
    transcodeTemplate.put("Container", container);

    // The conditional transcoding configurations.
    JSONObject transconfig = new JSONObject();
    transconfig.put("IsCheckReso", false);
    transconfig.put("IsCheckResoFail", false);
    transconfig.put("IsCheckVideoBitrate", false);
    transconfig.put("IsCheckVideoBitrateFail", false);
    transconfig.put("IsCheckAudioBitrate", false);
    transconfig.put("IsCheckAudioBitrateFail", false);
    transcodeTemplate.put("TransConfig", transconfig);

    // The encryption configurations. Only videos in the M3U8 format can be encrypted.
    //JSONObject encryptSetting = new JSONObject();
    //encryptSetting.put("EncryptType", "Private");
    //transcodeTemplate.put("EncryptSetting", encryptSetting);

    // The IDs of associated watermarks.
    JSONArray watermarkIdList = new JSONArray();
    watermarkIdList.add("263261bdc1ff65782f8995c6dd22****");
    // USER_DEFAULT_WATERMARK indicates the ID of the default watermark.
    watermarkIdList.add("USER_DEFAULT_WATERMARK");
    transcodeTemplate.put("WatermarkIds", watermarkIdList);

    JSONArray transcodeTemplateList = new JSONArray();
    transcodeTemplateList.add(transcodeTemplate);
    return transcodeTemplateList;
}

/**
 * Sample code
 */
public static void main(String[] args) throws ClientException {
    DefaultAcsClient client = initVodClient();
    AddTranscodeTemplateGroupResponse response = new AddTranscodeTemplateGroupResponse();
    try {
        response = addTranscodeTemplateGroup(client);
        System.out.println("TranscodeTemplateGroupId = " + response.getTranscodeTemplateGroupId());
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

Modify a transcoding template group

You can call the UpdateTranscodeTemplateGroup operation to modify a transcoding template group.

Alibaba Cloud OpenAPI Portal: UpdateTranscodeTemplateGroup / .

Sample code:

import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.UpdateTranscodeTemplateGroupRequest;
import com.aliyuncs.vod.model.v20170321.UpdateTranscodeTemplateGroupResponse;

/** 
 * 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;
    }

/**
 * Modify a transcoding template group.
 */
public static UpdateTranscodeTemplateGroupResponse updateTranscodeTemplateGroup(DefaultAcsClient client) throws Exception {
    UpdateTranscodeTemplateGroupRequest request = new UpdateTranscodeTemplateGroupRequest();
    request.setName("grouptest1");
    // The ID of the transcoding template group.
    request.setTranscodeTemplateGroupId("4c71a339fecec0152b4fa6f452****");
    request.setTranscodeTemplateList(buildTranscodeTemplateList().toJSONString());
    return client.getAcsResponse(request);
}

/**
 * Construct the parameters of the transcoding template that you want to modify.
 *
 * @return
 */
public static JSONArray buildTranscodeTemplateList() {
    JSONObject transcodeTemplate = new JSONObject();
    // The ID of the transcoding template.
    transcodeTemplate.put("TranscodeTemplateId", "85c2b18ac08fda33e8f6d9c56****");

     // The name of the template.
    transcodeTemplate.put("TemplateName", "testtemplate");

    // The transcoding settings for video streams.
    JSONObject video = new JSONObject();
    video.put("Width", 960);
    video.put("Bitrate", 900);
    video.put("Fps", 25);
    video.put("Remove", false);
    video.put("Codec", "H.264");
    video.put("Gop", "250");
    transcodeTemplate.put("Video", video);

    // The transcoding settings for audio streams.
    JSONObject audio = new JSONObject();
    audio.put("Codec", "AAC");
    audio.put("Bitrate", "96");
    audio.put("Channels", "2");
    audio.put("Samplerate", "32000");
    transcodeTemplate.put("Audio", audio);

    // The container.
    JSONObject container = new JSONObject();
    container.put("Format", "mp4");
    transcodeTemplate.put("Container", container);

    // The conditional transcoding configurations.
    JSONObject transconfig = new JSONObject();
    transconfig.put("IsCheckReso", false);
    transconfig.put("IsCheckResoFail", false);
    transconfig.put("IsCheckVideoBitrate", false);
    transconfig.put("IsCheckVideoBitrateFail", false);
    transconfig.put("IsCheckAudioBitrate", false);
    transconfig.put("IsCheckAudioBitrateFail", false);
    transcodeTemplate.put("TransConfig", transconfig);

    // The encryption configurations. Only videos in the M3U8 format can be encrypted.
    JSONObject encryptSetting = new JSONObject();
    encryptSetting.put("EncryptType", "Private");

    // The IDs of associated watermarks.
    JSONArray watermarkIdList = new JSONArray();
    watermarkIdList.add("263261bdc1ff65782f95c6dd22****");
    // USER_DEFAULT_WATERMARK indicates the ID of the default watermark.
    watermarkIdList.add("USER_DEFAULT_WATERMARK");
    transcodeTemplate.put("WatermarkIds", watermarkIdList);

    JSONArray transcodeTemplateList = new JSONArray();
    transcodeTemplateList.add(transcodeTemplate);
    return transcodeTemplateList;
}

/**
 * Sample code
 */
public static void main(String[] args) throws ClientException {
    DefaultAcsClient client = initVodClient();
    UpdateTranscodeTemplateGroupResponse response = new UpdateTranscodeTemplateGroupResponse();
    try {
        response = updateTranscodeTemplateGroup(client);
        System.out.println("TranscodeTemplateGroupId = " + response.getTranscodeTemplateGroupId());
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

Query a transcoding template group

  • You can call the GetTranscodeTemplateGroup operation to query a transcoding template group.

    Alibaba Cloud OpenAPI Portal: GetTranscodeTemplateGroup or .

  • You can call the ListTranscodeTemplateGroup operation to query a list of transcoding template groups.

    Alibaba Cloud OpenAPI Portal: ListTranscodeTemplateGroup.

Specify a transcoding template group as the default one

You can call the SetDefaultTranscodeTemplateGroup operation to specify a transcoding template group as the default one.

You can call the SetDefaultTranscodeTemplateGroup operation in the Alibaba Cloud OpenAPI Portal.

Delete a transcoding template group

You can call the DeleteTranscodeTemplateGroup operation to delete a transcoding template group.

Alibaba Cloud OpenAPI Portal: DeleteTranscodeTemplateGroup .

References