全部产品
云市场

转码模板

更新时间:2019-02-14 16:41:51

初始化客户端

使用前请先初始化客户端,参考 init_vod_client 接口。

添加转码模板组

接口参数和返回字段请参考 AddTranscodeTemplateGroup

  1. from aliyunsdkvod.request.v20170321 import AddTranscodeTemplateGroupRequest
  2. """ 构建需要添加的转码模板配置数据 """
  3. def build_transcode_template_list():
  4. transcodeTemplateList = []
  5. transcodeTemplate = {}
  6. # 设置模板名称
  7. transcodeTemplate["TemplateName"] = "MP4-for-Low-Definition"
  8. # 清晰度
  9. transcodeTemplate["Definition"] = "LD"
  10. # 视频流转码配置
  11. videoConfig = {"Width": 640, "Bitrate": 400, "Fps": 25, "Remove": False, "Codec": "H.264", "Gop": "250"}
  12. transcodeTemplate["Video"] = videoConfig
  13. # 音频流转码配置
  14. audioConfig = {"Codec": "AAC", "Bitrate": "64", "Channels": "2", "Samplerate": "32000"}
  15. transcodeTemplate["Audio"] = audioConfig
  16. # 封装容器
  17. container = {"Format": "mp4"}
  18. transcodeTemplate["Container"] = container
  19. # 条件转码配置
  20. condition = {"IsCheckReso": False, "IsCheckResoFail": False, "IsCheckVideoBitrate": False,
  21. "IsCheckVideoBitrateFail": False, "IsCheckAudioBitrate": False, "IsCheckAudioBitrateFail": False}
  22. transcodeTemplate["TransConfig"] = condition
  23. """
  24. # 加密配置(只支持HLS)
  25. encryptSetting = {"EncryptType": "Private"}
  26. transcodeTemplate["EncryptSetting"] = encryptSetting
  27. """
  28. # 水印ID(多水印关联)
  29. watermarkIdList = []
  30. #watermarkIdList.append("<WatermarkId>")
  31. watermarkIdList.append("USER_DEFAULT_WATERMARK")
  32. transcodeTemplate["WatermarkIds"] = watermarkIdList
  33. transcodeTemplateList.append(transcodeTemplate)
  34. return transcodeTemplateList
  35. """ 添加转码模板配置 """
  36. def add_transcode_template_group(clt):
  37. request = AddTranscodeTemplateGroupRequest.AddTranscodeTemplateGroupRequest()
  38. request.set_Name("SampleTranscodeTemplateGroup")
  39. transcodeTemplateList = build_transcode_template_list()
  40. request.set_TranscodeTemplateList(json.dumps(transcodeTemplateList))
  41. request.set_accept_format('JSON')
  42. response = json.loads(clt.do_action_with_exception(request))
  43. return response
  44. try:
  45. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  46. res = add_transcode_template_group(clt)
  47. print(json.dumps(res, ensure_ascii=False, indent=4))
  48. except Exception as e:
  49. print(e)
  50. print(traceback.format_exc())

修改转码模板组

接口参数和返回字段请参考 UpdateTranscodeTemplateGroup

  1. from aliyunsdkvod.request.v20170321 import UpdateTranscodeTemplateGroupRequest
  2. """ 构建需要修改的转码模板配置数据 """
  3. def build_transcode_template_list():
  4. transcodeTemplateList = []
  5. transcodeTemplate = {}
  6. # 设置转码模板ID,必填
  7. transcodeTemplate["TranscodeTemplateId"] = "<TranscodeTemplateId>"
  8. # 以下都是可选项
  9. # 设置新的模板名称(如果需要更改)
  10. transcodeTemplate["TemplateName"] = "new-MP4-for-Low-Definition"
  11. # 视频流转码配置
  12. videoConfig = {"Width": 640, "Bitrate": 500, "Fps": 60, "Remove": False, "Codec": "H.264", "Gop": "250"}
  13. transcodeTemplate["Video"] = videoConfig
  14. # 音频流转码配置
  15. audioConfig = {"Codec": "AAC", "Bitrate": "128", "Channels": "2", "Samplerate": "32000"}
  16. transcodeTemplate["Audio"] = audioConfig
  17. # 封装容器
  18. container = {"Format": "mp4"}
  19. transcodeTemplate["Container"] = container
  20. # 条件转码配置
  21. condition = {"IsCheckReso": False, "IsCheckResoFail": False, "IsCheckVideoBitrate": False,
  22. "IsCheckVideoBitrateFail": False, "IsCheckAudioBitrate": False, "IsCheckAudioBitrateFail": False}
  23. transcodeTemplate["TransConfig"] = condition
  24. """
  25. # 加密配置(只支持HLS)
  26. encryptSetting = {"EncryptType": "Private"}
  27. transcodeTemplate["EncryptSetting"] = encryptSetting
  28. """
  29. # 水印ID(多水印关联)
  30. watermarkIdList = []
  31. #watermarkIdList.append("<WatermarkId>")
  32. watermarkIdList.append("USER_DEFAULT_WATERMARK")
  33. transcodeTemplate["WatermarkIds"] = watermarkIdList
  34. transcodeTemplateList.append(transcodeTemplate)
  35. return transcodeTemplateList
  36. """ 修改转码模板配置 """
  37. def update_transcode_template_group(clt):
  38. request = UpdateTranscodeTemplateGroupRequest.UpdateTranscodeTemplateGroupRequest()
  39. # 设置转码模板组ID
  40. request.set_TranscodeTemplateGroupId("<TranscodeTemplateGroupId>")
  41. # 设置转码模板配置
  42. transcodeTemplateList = build_transcode_template_list()
  43. request.set_TranscodeTemplateList(json.dumps(transcodeTemplateList))
  44. request.set_accept_format('JSON')
  45. response = json.loads(clt.do_action_with_exception(request))
  46. return response
  47. try:
  48. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  49. res = update_transcode_template_group(clt)
  50. print(json.dumps(res, ensure_ascii=False, indent=4))
  51. except Exception as e:
  52. print(e)
  53. print(traceback.format_exc())

查询转码模板组列表

接口参数和返回字段请参考 ListTranscodeTemplateGroup

  1. from aliyunsdkvod.request.v20170321 import ListTranscodeTemplateGroupRequest
  2. def list_transcode_template_group(clt):
  3. request = ListTranscodeTemplateGroupRequest.ListTranscodeTemplateGroupRequest()
  4. request.set_accept_format('JSON')
  5. response = json.loads(clt.do_action_with_exception(request))
  6. return response
  7. try:
  8. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  9. data = list_transcode_template_group(clt)
  10. for item in data['TranscodeTemplateGroupList']:
  11. print("TranscodeTemplateGroupId: %s, Name: %s" % (item['TranscodeTemplateGroupId'], item['Name']))
  12. print(json.dumps(data, ensure_ascii=False, indent=4))
  13. except Exception as e:
  14. print(e)
  15. print(traceback.format_exc())

查询单个转码模板组

接口参数和返回字段请参考 GetTranscodeTemplateGroup

  1. from aliyunsdkvod.request.v20170321 import GetTranscodeTemplateGroupRequest
  2. def get_transcode_template_group(clt):
  3. request = GetTranscodeTemplateGroupRequest.GetTranscodeTemplateGroupRequest()
  4. request.set_TranscodeTemplateGroupId("<TranscodeTemplateGroupId>")
  5. request.set_accept_format('JSON')
  6. response = json.loads(clt.do_action_with_exception(request))
  7. return response
  8. try:
  9. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  10. templateGroup = get_transcode_template_group(clt)
  11. for item in templateGroup['TranscodeTemplateGroup']['TranscodeTemplateList']:
  12. print("TranscodeTemplateId: %s, TemplateName: %s, Definition: %s" %
  13. (item['TranscodeTemplateId'], item['TemplateName'], item['Definition']))
  14. print(json.dumps(templateGroup, ensure_ascii=False, indent=4))
  15. except Exception as e:
  16. print(e)
  17. print(traceback.format_exc())

设置默认转码模板组

接口参数和返回字段请参考 SetDefaultTranscodeTemplateGroup

  1. from aliyunsdkvod.request.v20170321 import SetDefaultTranscodeTemplateGroupRequest
  2. def set_default_transcode_template_group(clt):
  3. request = SetDefaultTranscodeTemplateGroupRequest.SetDefaultTranscodeTemplateGroupRequest()
  4. request.set_TranscodeTemplateGroupId("<TranscodeTemplateGroupId>")
  5. request.set_accept_format('JSON')
  6. response = json.loads(clt.do_action_with_exception(request))
  7. return response
  8. try:
  9. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  10. res = set_default_transcode_template_group(clt)
  11. print(json.dumps(res, ensure_ascii=False, indent=4))
  12. except Exception as e:
  13. print(e)
  14. print(traceback.format_exc())

删除转码模板组

接口参数和返回字段请参考 DeleteTranscodeTemplateGroup

  1. from aliyunsdkvod.request.v20170321 import DeleteTranscodeTemplateGroupRequest
  2. def delete_transcode_template_group(clt):
  3. request = DeleteTranscodeTemplateGroupRequest.DeleteTranscodeTemplateGroupRequest()
  4. request.set_TranscodeTemplateGroupId("<TranscodeTemplateGroupId>")
  5. request.set_accept_format('JSON')
  6. response = json.loads(clt.do_action_with_exception(request))
  7. return response
  8. try:
  9. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  10. res = delete_transcode_template_group(clt)
  11. print(json.dumps(res, ensure_ascii=False, indent=4))
  12. except Exception as e:
  13. print(e)
  14. print(traceback.format_exc())