All Products
Search
Document Center

Transcoding template

Last Updated: Jul 11, 2019

Initialize a client

Initialize a client before using the SDK. For more information, see init_vod_client.

Add a transcoding template group

For more information about the request and response parameters, see AddTranscodeTemplateGroup.

from aliyunsdkvod.request.v20170321 import AddTranscodeTemplateGroupRequest

""" Constructs the transcoding template configuration. """
def build_transcode_template_list():
    transcodeTemplateList = []
    transcodeTemplate = {}

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

    # Sets the definition.
    transcodeTemplate["Definition"] = "LD"

    # Specifies the video stream transcoding configuration.
    videoConfig = {"Width": 640, "Bitrate": 400, "Fps": 25, "Remove": False, "Codec": "H. 264", "Gop": "250"}
    transcodeTemplate["Video"] = videoConfig

    # Specifies the audio stream transcoding configuration.
    audioConfig = {"Codec": "AAC", "Bitrate": "64", "Channels": "2", "Samplerate": "32000"}
    transcodeTemplate["Audio"] = audioConfig

    # Specifies the encapsulation container.
    container = {"Format": "mp4"}
    transcodeTemplate["Container"] = container

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

    """
    # Specifies the encryption configuration, which supports only HTTP Live Streaming (HLS).
    encryptSetting = {"EncryptType": "Private"}
    transcodeTemplate["EncryptSetting"] = encryptSetting
    """

    # Associates watermark IDs with transcoding template IDs.
    watermarkIdList = []
    #watermarkIdList.append("<WatermarkId>") 
    watermarkIdList.append("USER_DEFAULT_WATERMARK")
    transcodeTemplate["WatermarkIds"] = watermarkIdList

    transcodeTemplateList.append(transcodeTemplate)

    return transcodeTemplateList


"""  Adds a transcoding template group.   """
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

For more information about the request and response parameters, see UpdateTranscodeTemplateGroup.

from aliyunsdkvod.request.v20170321 import UpdateTranscodeTemplateGroupRequest

""" Constructs the transcoding template configuration. """
def build_transcode_template_list():
    transcodeTemplateList = []
    transcodeTemplate = {}

    # (Required) Sets the transcoding template ID.
    transcodeTemplate["TranscodeTemplateId"] = "<TranscodeTemplateId>" 

    # The following settings are optional.

    # Sets a new transcoding template name as required.
    transcodeTemplate["TemplateName"] = "new-MP4-for-Low-Definition"

    # Specifies the video stream transcoding configuration.
    videoConfig = {"Width": 640, "Bitrate": 500, "Fps": 60, "Remove": False, "Codec": "H. 264", "Gop": "250"}
    transcodeTemplate["Video"] = videoConfig

    # Specifies the audio stream transcoding configuration.
    audioConfig = {"Codec": "AAC", "Bitrate": "128", "Channels": "2", "Samplerate": "32000"}
    transcodeTemplate["Audio"] = audioConfig

    # Specifies the encapsulation container.
    container = {"Format": "mp4"}
    transcodeTemplate["Container"] = container

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

    """
    # Specifies the encryption configuration, which supports only HLS.
    encryptSetting = {"EncryptType": "Private"}
    transcodeTemplate["EncryptSetting"] = encryptSetting
    """

    # Associates watermark IDs with transcoding template IDs.
    watermarkIdList = []
    #watermarkIdList.append("<WatermarkId>")
    watermarkIdList.append("USER_DEFAULT_WATERMARK")
    transcodeTemplate["WatermarkIds"] = watermarkIdList

    transcodeTemplateList.append(transcodeTemplate)

    return transcodeTemplateList


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

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

    # Specifies the transcoding template configuration.
    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 list of transcoding template groups

For more information about the request and response parameters, see ListTranscodeTemplateGroup.

from aliyunsdkvod.request.v20170321 import ListTranscodeTemplateGroupRequest
def list_transcode_template_group(clt):
    request = ListTranscodeTemplateGroupRequest.ListTranscodeTemplateGroupRequest()

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    data = list_transcode_template_group(clt)
    for item in data['TranscodeTemplateGroupList']:
        print("TranscodeTemplateGroupId: %s, Name: %s" % (item['TranscodeTemplateGroupId'], item['Name']))

    print(json.dumps(data, ensure_ascii=False, indent=4))

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

Query a transcoding template group

For more information about the request and response parameters, see GetTranscodeTemplateGroup.

from aliyunsdkvod.request.v20170321 import GetTranscodeTemplateGroupRequest
def get_transcode_template_group(clt):
    request = GetTranscodeTemplateGroupRequest.GetTranscodeTemplateGroupRequest()
    request.set_TranscodeTemplateGroupId("<TranscodeTemplateGroupId>") 

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>') 
    templateGroup = get_transcode_template_group(clt)
    for item in templateGroup['TranscodeTemplateGroup']['TranscodeTemplateList']:
        print("TranscodeTemplateId: %s, TemplateName: %s, Definition: %s" %
              (item['TranscodeTemplateId'], item['TemplateName'], item['Definition']))

    print(json.dumps(templateGroup, ensure_ascii=False, indent=4))

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

Set the default transcoding template group

For more information about the request and response parameters, see SetDefaultTranscodeTemplateGroup.

from aliyunsdkvod.request.v20170321 import SetDefaultTranscodeTemplateGroupRequest
def set_default_transcode_template_group(clt):
    request = SetDefaultTranscodeTemplateGroupRequest.SetDefaultTranscodeTemplateGroupRequest()
    request.set_TranscodeTemplateGroupId("<TranscodeTemplateGroupId>")

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

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

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

Delete a transcoding template group

For more information about the request and response parameters, see DeleteTranscodeTemplateGroup.

from aliyunsdkvod.request.v20170321 import DeleteTranscodeTemplateGroupRequest
def delete_transcode_template_group(clt):
    request = DeleteTranscodeTemplateGroupRequest.DeleteTranscodeTemplateGroupRequest()
    request.set_TranscodeTemplateGroupId("<TranscodeTemplateGroupId>") 

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

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

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