全部产品
云市场

媒体处理

更新时间:2019-08-29 10:07:25

初始化客户端

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

提交转码作业-普通&阿里云加密

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

此示例代码为使用 普通转码(不加密)、阿里云视频加密 提交转码作业的示例。

  1. """
  2. * 构建覆盖参数,目前只支持图片水印文件地址、文字水印的内容覆盖;如不使用可忽略
  3. * 需要替换的水印信息对应水印ID必须是关联在指定的转码模板ID上(即TranscodeTemplateId)
  4. * 不支持通过本接口去增加一个没有关联上的水印
  5. """
  6. def build_override_params():
  7. # 以替换水印为例
  8. watermarks = []
  9. # 替换图片水印文件
  10. watermark1 = {'WatermarkId': '<watermarkId>', 'FileUrl': 'https://sample.oss-cn-shanghai.aliyuncs.com/watermarks/sample.png'}
  11. watermarks.append(watermark1)
  12. # 替换文字水印
  13. watermark2 = {'WatermarkId': '<watermarkId>', 'Content': 'new Text'}
  14. watermarks.append(watermark2)
  15. return {'Watermarks': watermarks}
  16. """ 发起转码处理 """
  17. from aliyunsdkvod.request.v20170321 import SubmitTranscodeJobsRequest
  18. def normal_submit_transcode_jobs(clt):
  19. request = SubmitTranscodeJobsRequest.SubmitTranscodeJobsRequest()
  20. # 需要转码的视频ID
  21. request.set_VideoId('<videoId>')
  22. # 指定转码模板组
  23. request.set_TemplateGroupId('<templateGroupId>')
  24. """
  25. # 可选设置覆盖参数,如替换水印
  26. overrideParams = build_override_params()
  27. request.set_OverrideParams(json.dumps(overrideParams))
  28. """
  29. request.set_accept_format('JSON')
  30. response = json.loads(clt.do_action_with_exception(request))
  31. return response
  32. try:
  33. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  34. jobs = normal_submit_transcode_jobs(clt)
  35. print(jobs['TranscodeJobs']['TranscodeJob'])
  36. print(json.dumps(jobs, ensure_ascii=False, indent=4))
  37. except Exception as e:
  38. print(e)
  39. print(traceback.format_exc())

提交转码作业-HLS标准加密

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

此示例代码为使用 HLS标准加密 提交转码作业的示例。

  1. """
  2. * 生成HLS标准加密参数的可选配置,用不到的可忽略
  3. * 此时依赖KMS服务,需要安装KMS依赖包 aliyun-python-sdk-kms,并引入相关类
  4. * 生成密钥API参考:https://help.aliyun.com/document_detail/28948.html
  5. """
  6. from aliyunsdkkms.request.v20160120 import GenerateDataKeyRequest
  7. from aliyunsdkcore.http import protocol_type
  8. def build_encrypt_config(clt):
  9. try:
  10. # 随机生成一个加密的秘钥,返回的response包含明文秘钥以及密文秘钥,
  11. # 视频标准加密只需要传递密文秘钥即可
  12. request = GenerateDataKeyRequest.GenerateDataKeyRequest()
  13. request.set_KeyId('<serviceKey>')
  14. request.set_KeySpec('AES_128')
  15. # KMS接口必须使用HTTPS访问
  16. request.set_protocol_type(protocol_type.HTTPS)
  17. request.set_accept_format('JSON')
  18. response = json.loads(clt.do_action_with_exception(request))
  19. # 解密接口地址,该参数需要将每次生成的密文秘钥与接口URL拼接生成,表示每个视频的解密的密文秘钥都不一样;注意您需要自己部署解密服务
  20. # Ciphertext作为解密接口的参数名称,可自定义,此处只作为参考
  21. decryptKeyUri = 'http://decrypt.demo.com/decrypt?' + 'Ciphertext=' + response['CiphertextBlob']
  22. return {'DecryptKeyUri': decryptKeyUri, 'KeyServiceType': 'KMS', 'CipherText': response['CiphertextBlob']}
  23. except Exception as e:
  24. print(e)
  25. #print(traceback.format_exc())
  26. return None
  27. """ 发起转码处理 """
  28. from aliyunsdkvod.request.v20170321 import SubmitTranscodeJobsRequest
  29. def hlsencrypt_submit_transcode_jobs(clt):
  30. request = SubmitTranscodeJobsRequest.SubmitTranscodeJobsRequest()
  31. # 需要转码的视频ID
  32. request.set_VideoId('<videoId>')
  33. # 指定转码模板组
  34. request.set_TemplateGroupId('<templateGroupId>')
  35. # 使用KMS生成随机的加密密钥
  36. encryptConfig = build_encrypt_config(clt)
  37. if encryptConfig is not None:
  38. request.set_EncryptConfig(json.dumps(encryptConfig))
  39. request.set_accept_format('JSON')
  40. response = json.loads(clt.do_action_with_exception(request))
  41. return response
  42. try:
  43. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  44. jobs = hlsencrypt_submit_transcode_jobs(clt)
  45. print(jobs['TranscodeJobs']['TranscodeJob'])
  46. print(json.dumps(jobs, ensure_ascii=False, indent=4))
  47. except Exception as e:
  48. print(e)
  49. print(traceback.format_exc())

提交截图作业

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

创建截图模板详细请参考 截图模板

  1. from aliyunsdkvod.request.v20170321 import SubmitSnapshotJobRequest
  2. def submit_snapshot_job(clt):
  3. request = SubmitSnapshotJobRequest.SubmitSnapshotJobRequest()
  4. # 需要截图的视频ID
  5. request.set_VideoId('<videoId>')
  6. # 截图模板ID
  7. #request.set_SnapshotTemplateId('<snapshotTemplateId>')
  8. # 如果设置了截图模板ID,则会忽略下面参数
  9. request.set_Count(50L)
  10. request.set_SpecifiedOffsetTime(0L)
  11. request.set_Interval(1L)
  12. request.set_Width(200)
  13. request.set_Height(200)
  14. # 雪碧图参数,如不使用可不设置
  15. spriteSnapshotConfig = {'CellWidth': 120, 'CellHeight': 68, 'Columns': 3,
  16. 'Lines': 10, 'Padding': 20, 'Margin': 50}
  17. # 保留雪碧图原始图
  18. spriteSnapshotConfig['KeepCellPic'] = 'keep'
  19. spriteSnapshotConfig['Color'] = 'tomato'
  20. request.set_SpriteSnapshotConfig(json.dumps(spriteSnapshotConfig))
  21. request.set_accept_format('JSON')
  22. response = json.loads(clt.do_action_with_exception(request))
  23. return response
  24. try:
  25. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  26. job = submit_snapshot_job(clt)
  27. print(json.dumps(job, ensure_ascii=False, indent=4))
  28. except Exception as e:
  29. print(e)
  30. print(traceback.format_exc())

查询截图数据

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

  1. from aliyunsdkvod.request.v20170321 import ListSnapshotsRequest
  2. def list_snapshots(clt):
  3. request = ListSnapshotsRequest.ListSnapshotsRequest()
  4. request.set_VideoId('<videoId>')
  5. request.set_SnapshotType('CoverSnapshot')
  6. request.set_PageNo(1)
  7. request.set_PageSize(20)
  8. request.set_accept_format('JSON')
  9. response = json.loads(clt.do_action_with_exception(request))
  10. return response
  11. try:
  12. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  13. snapshots = list_snapshots(clt)
  14. print(snapshots['MediaSnapshot'])
  15. print(json.dumps(snapshots, ensure_ascii=False, indent=4))
  16. except Exception as e:
  17. print(e)
  18. print(traceback.format_exc())

导播台视频预处理

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

  1. from aliyunsdkvod.request.v20170321 import SubmitPreprocessJobsRequest
  2. def submit_preprocess_jobs(clt):
  3. request = SubmitPreprocessJobsRequest.SubmitPreprocessJobsRequest()
  4. request.set_VideoId('<videoId>')
  5. request.set_PreprocessType('PreprocessType')
  6. request.set_accept_format('JSON')
  7. response = json.loads(clt.do_action_with_exception(request))
  8. return response
  9. try:
  10. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  11. jobs = submit_preprocess_jobs(clt)
  12. print(json.dumps(jobs, ensure_ascii=False, indent=4))
  13. except Exception as e:
  14. print(e)
  15. print(traceback.format_exc())