All Products
Search
Document Center

ApsaraVideo VOD:Transcoding templates

Last Updated:Aug 22, 2023

This topic provides examples on how to use the API operations of the transcoding template module. The API operations are encapsulated in ApsaraVideo VOD SDK for Python. You can call the API operations to create, modify, delete, and query transcoding template groups. You can also specify a transcoding template group as the default one.

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 transcoding template group

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

Click AddTranscodeTemplateGroup to learn more about this API operation.

Sample code:

from aliyunsdkvod.request.v20170321 import AddTranscodeTemplateGroupRequest

""" Construct the parameters of the transcoding template that you want to create. """
def build_transcode_template_list():
    transcodeTemplateList = []
    transcodeTemplate = {}

    # The name of the template.
    transcodeTemplate["TemplateName"] = "MP4-for-Low-Definition"

    # The definition.
    transcodeTemplate["Definition"] = "LD"

    # The transcoding settings for video streams.
    videoConfig = {"Width": 640, "Bitrate": 400, "Fps": 25, "Remove": False, "Codec": "H.264", "Gop": "250"}
    transcodeTemplate["Video"] = videoConfig

    # The transcoding settings for audio streams.
    audioConfig = {"Codec": "AAC", "Bitrate": "64", "Channels": "2", "Samplerate": "32000"}
    transcodeTemplate["Audio"] = audioConfig

    # The container.
    container = {"Format": "mp4"}
    transcodeTemplate["Container"] = container

    # The conditional transcoding configurations.
    condition = {"IsCheckReso": False, "IsCheckResoFail": False, "IsCheckVideoBitrate": False,
                 "IsCheckVideoBitrateFail": False, "IsCheckAudioBitrate": False, "IsCheckAudioBitrateFail": False}
    transcodeTemplate["TransConfig"] = condition

    """
    # The encryption configurations. Only HLS encryption is supported.
    encryptSetting = {"EncryptType": "Private"}
    transcodeTemplate["EncryptSetting"] = encryptSetting
    """

    # The IDs of associated watermarks.
    watermarkIdList = []
    #watermarkIdList.append("<WatermarkId>")
    watermarkIdList.append("USER_DEFAULT_WATERMARK")
    transcodeTemplate["WatermarkIds"] = watermarkIdList

    transcodeTemplateList.append(transcodeTemplate)

    return transcodeTemplateList


"""  Add transcoding template configurations.   """
def add_transcode_template_group(clt):
    request = AddTranscodeTemplateGroupRequest.AddTranscodeTemplateGroupRequest()

    request.set_Name("SampleTranscodeTemplateGroup")

    transcodeTemplateList = build_transcode_template_list()
    request.set_TranscodeTemplateList(json.dumps(transcodeTemplateList))

    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    res = add_transcode_template_group(clt)
    print(json.dumps(res, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

Modify a transcoding template group

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

Click UpdateTranscodeTemplateGroup to learn more about this API operation.

Sample code:

from aliyunsdkvod.request.v20170321 import UpdateTranscodeTemplateGroupRequest

""" Construct the parameters of the transcoding template that you want to modify. """
def build_transcode_template_list():
    transcodeTemplateList = []
    transcodeTemplate = {}

    # Required. The ID of the transcoding template.
    transcodeTemplate["TranscodeTemplateId"] = "<TranscodeTemplateId>"

    # The following parameters are optional:

    # The new template name. Specify this parameter based on your business requirements.
    transcodeTemplate["TemplateName"] = "new-MP4-for-Low-Definition"

    # The transcoding settings for video streams.
    videoConfig = {"Width": 640, "Bitrate": 500, "Fps": 60, "Remove": False, "Codec": "H.264", "Gop": "250"}
    transcodeTemplate["Video"] = videoConfig

    # The transcoding settings for audio streams.
    audioConfig = {"Codec": "AAC", "Bitrate": "128", "Channels": "2", "Samplerate": "32000"}
    transcodeTemplate["Audio"] = audioConfig

    # The container.
    container = {"Format": "mp4"}
    transcodeTemplate["Container"] = container

    # The conditional transcoding configurations.
    condition = {"IsCheckReso": False, "IsCheckResoFail": False, "IsCheckVideoBitrate": False,
                 "IsCheckVideoBitrateFail": False, "IsCheckAudioBitrate": False, "IsCheckAudioBitrateFail": False}
    transcodeTemplate["TransConfig"] = condition

    """
    # The encryption configurations. Only HLS encryption is supported.
    encryptSetting = {"EncryptType": "Private"}
    transcodeTemplate["EncryptSetting"] = encryptSetting
    """

    # The IDs of associated watermarks.
    watermarkIdList = []
    #watermarkIdList.append("<WatermarkId>")
    watermarkIdList.append("USER_DEFAULT_WATERMARK")
    transcodeTemplate["WatermarkIds"] = watermarkIdList

    transcodeTemplateList.append(transcodeTemplate)

    return transcodeTemplateList


"""  Modify a transcoding template.   """
def update_transcode_template_group(clt):
    request = UpdateTranscodeTemplateGroupRequest.UpdateTranscodeTemplateGroupRequest()

    # Specify the transcoding template group ID.
    request.set_TranscodeTemplateGroupId("<TranscodeTemplateGroupId>")

    # Specify the transcoding template configurations.
    transcodeTemplateList = build_transcode_template_list()
    request.set_TranscodeTemplateList(json.dumps(transcodeTemplateList))

    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    res = update_transcode_template_group(clt)
    print(json.dumps(res, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

Query a transcoding template group

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

    Click GetTranscodeTemplateGroup to learn more about this API operation.

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

    Click ListTranscodeTemplateGroup to learn more about this API operation.

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.

Click SetDefaultTranscodeTemplateGroup to learn more about this API operation.

Delete a transcoding template group

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

Click DeleteTranscodeTemplateGroup to learn more about this API operation.