转码模板

本篇文档提供了Python SDK转码模板模块相关功能的API调用示例。包含添加转码模板组、修改转码模板组、删除转码模板组、查询转码模板组、设置默认转码模板组等。

初始化客户端

使用前请先初始化客户端,请参见初始化

添加转码模板组

调用AddTranscodeTemplateGroup接口,完成添加转码模板组功能。

接口参数和返回字段请参见AddTranscodeTemplateGroup。调用示例如下:

from aliyunsdkvod.request.v20170321 import AddTranscodeTemplateGroupRequest

""" 构建需要添加的转码模板配置数据 """
def build_transcode_template_list():
    transcodeTemplateList = []
    transcodeTemplate = {}

    # 设置模板名称
    transcodeTemplate["TemplateName"] = "MP4-for-Low-Definition"

    # 清晰度
    transcodeTemplate["Definition"] = "LD"

    # 视频流转码配置
    videoConfig = {"Width": 640, "Bitrate": 400, "Fps": 25, "Remove": False, "Codec": "H.264", "Gop": "250"}
    transcodeTemplate["Video"] = videoConfig

    # 音频流转码配置
    audioConfig = {"Codec": "AAC", "Bitrate": "64", "Channels": "2", "Samplerate": "32000"}
    transcodeTemplate["Audio"] = audioConfig

    # 封装容器
    container = {"Format": "mp4"}
    transcodeTemplate["Container"] = container

    # 条件转码配置
    condition = {"IsCheckReso": False, "IsCheckResoFail": False, "IsCheckVideoBitrate": False,
                 "IsCheckVideoBitrateFail": False, "IsCheckAudioBitrate": False, "IsCheckAudioBitrateFail": False}
    transcodeTemplate["TransConfig"] = condition

    """
    # 加密配置(只支持HLS)
    encryptSetting = {"EncryptType": "Private"}
    transcodeTemplate["EncryptSetting"] = encryptSetting
    """

    # 水印ID(多水印关联)
    watermarkIdList = []
    #watermarkIdList.append("<WatermarkId>")
    watermarkIdList.append("USER_DEFAULT_WATERMARK")
    transcodeTemplate["WatermarkIds"] = watermarkIdList

    transcodeTemplateList.append(transcodeTemplate)

    return transcodeTemplateList


"""  添加转码模板配置   """
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())

修改转码模板组

调用UpdateTranscodeTemplateGroup接口,完成修改转码模板组功能。

接口参数和返回字段请参见UpdateTranscodeTemplateGroup。调用示例如下:

from aliyunsdkvod.request.v20170321 import UpdateTranscodeTemplateGroupRequest

""" 构建需要修改的转码模板配置数据 """
def build_transcode_template_list():
    transcodeTemplateList = []
    transcodeTemplate = {}

    # 设置转码模板ID,必填
    transcodeTemplate["TranscodeTemplateId"] = "<TranscodeTemplateId>"

    # 以下都是可选项

    # 设置新的模板名称(如果需要更改)
    transcodeTemplate["TemplateName"] = "new-MP4-for-Low-Definition"

    # 视频流转码配置
    videoConfig = {"Width": 640, "Bitrate": 500, "Fps": 60, "Remove": False, "Codec": "H.264", "Gop": "250"}
    transcodeTemplate["Video"] = videoConfig

    # 音频流转码配置
    audioConfig = {"Codec": "AAC", "Bitrate": "128", "Channels": "2", "Samplerate": "32000"}
    transcodeTemplate["Audio"] = audioConfig

    # 封装容器
    container = {"Format": "mp4"}
    transcodeTemplate["Container"] = container

    # 条件转码配置
    condition = {"IsCheckReso": False, "IsCheckResoFail": False, "IsCheckVideoBitrate": False,
                 "IsCheckVideoBitrateFail": False, "IsCheckAudioBitrate": False, "IsCheckAudioBitrateFail": False}
    transcodeTemplate["TransConfig"] = condition

    """
    # 加密配置(只支持HLS)
    encryptSetting = {"EncryptType": "Private"}
    transcodeTemplate["EncryptSetting"] = encryptSetting
    """

    # 水印ID(多水印关联)
    watermarkIdList = []
    #watermarkIdList.append("<WatermarkId>")
    watermarkIdList.append("USER_DEFAULT_WATERMARK")
    transcodeTemplate["WatermarkIds"] = watermarkIdList

    transcodeTemplateList.append(transcodeTemplate)

    return transcodeTemplateList


"""  修改转码模板配置   """
def update_transcode_template_group(clt):
    request = UpdateTranscodeTemplateGroupRequest.UpdateTranscodeTemplateGroupRequest()

    # 设置转码模板组ID
    request.set_TranscodeTemplateGroupId("<TranscodeTemplateGroupId>")

    # 设置转码模板配置
    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())

查询转码模板组列表

调用ListTranscodeTemplateGroup接口,完成查询转码模板组列表功能。

接口参数和返回字段请参见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())

查询单个转码模板组

调用GetTranscodeTemplateGroup接口,完成查询单个转码模板组功能。

接口参数和返回字段请参见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())

设置默认转码模板组

调用SetDefaultTranscodeTemplateGroup接口,完成设置默认转码模板组功能。

接口参数和返回字段请参见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())

删除转码模板组

调用DeleteTranscodeTemplateGroup接口,完成删除转码模板组功能。

接口参数和返回字段请参见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())