All Products
Search
Document Center

Media processing

Last Updated: Aug 01, 2019

Initialize a client

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

Submit a transcoding job based on normal transcoding or Alibaba Cloud video encryption

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

This sample code applies when you submit a transcoding job based on normal transcoding (without encryption) or Alibaba Cloud video encryption.

  1. """
  2. * Currently, the constructed watermark replacement parameter can be used to replace only the file URL of an image watermark or the content of a text watermark. Ignore this configuration if watermark replacement is not required.
  3. * The watermark ID of the source watermark information (to be replaced) must be associated with the transcoding template ID specified by the TranscodeTemplateId parameter.
  4. * You can call the SubmitTranscodeJobs operation to add only watermarks whose IDs are associated with template IDs.
  5. """
  6. def build_override_params():
  7. # For example, replace a watermark.
  8. watermarks = []
  9. # Replaces the file URL of an image watermark.
  10. watermark1 = {'WatermarkId': '<watermarkId>', 'FileUrl': 'https://sample.oss-cn-shanghai.aliyuncs.com/watermarks/sample.png'}
  11. watermarks.append(watermark1)
  12. # Replaces the content of a text watermark.
  13. watermark2 = {'WatermarkId': '<watermarkId>', 'Content': 'new Text'}
  14. watermarks.append(watermark2)
  15. return {'Watermarks': watermarks}
  16. """ Submits a transcoding job. """
  17. from aliyunsdkvod.request.v20170321 import SubmitTranscodeJobsRequest
  18. def normal_submit_transcode_jobs(clt):
  19. request = SubmitTranscodeJobsRequest.SubmitTranscodeJobsRequest()
  20. # Specifies the ID of the video to be transcoded.
  21. request.set_VideoId('<videoId>')
  22. # Specifies the ID of the transcoding template group.
  23. request.set_TemplateGroupId('<templateGroupId>')
  24. """
  25. # (Optional) Constructs a replacement parameter, for example, a watermark replacement parameter.
  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())

Submit a transcoding job based on standard HLS encryption

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

This sample code applies when you submit a transcoding job based on standard HLS encryption.

  1. """
  2. * The standard HLS encryption configuration is optional. Ignore this configuration if standard HLS encryption is not required.
  3. * This operation is dependent on KMS. You need to install the KMS dependency package aliyun-python-sdk-kms and reference the relevant class.
  4. * For more information about the operation used to generate a key, see 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. # Generates a random encryption key. The response contains a plaintext key and a ciphertext key.
  11. # Specifies only the ciphertext key for standard video encryption.
  12. request = GenerateDataKeyRequest.GenerateDataKeyRequest()
  13. request.set_KeyId('<serviceKey>')
  14. request.set_KeySpec('AES_128')
  15. # Sets the protocol type to HTTPS if you call a KMS operation.
  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. # Specifies the URL of the decryption API. The ciphertext key generated each time and the API URL are concatenated to generate the value of this parameter to provide a unique ciphertext decryption key for each video. Note that you need to deploy a decryption service.
  20. # You can customize the name of the decryption API parameter. Ciphertext is used as an example.
  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. """ Submits a transcoding job. """
  28. from aliyunsdkvod.request.v20170321 import SubmitTranscodeJobsRequest
  29. def hlsencrypt_submit_transcode_jobs(clt):
  30. request = SubmitTranscodeJobsRequest.SubmitTranscodeJobsRequest()
  31. # Specifies the ID of the video to be transcoded.
  32. request.set_VideoId('<videoId>')
  33. # Specifies the ID of the transcoding template group.
  34. request.set_TemplateGroupId('<templateGroupId>')
  35. # Uses KMS to generate a random encryption key.
  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())

Submit a video snapshot job

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

For more information about how to create a snapshot template, see AddVodTemplate.

  1. from aliyunsdkvod.request.v20170321 import SubmitSnapshotJobRequest
  2. def submit_snapshot_job(clt):
  3. request = SubmitSnapshotJobRequest.SubmitSnapshotJobRequest()
  4. # Specifies the ID of the video to be snapshot.
  5. request.set_VideoId('<videoId>')
  6. # Specifies the snapshot template ID.
  7. #request.set_SnapshotTemplateId('<snapshotTemplateId>')
  8. # If you set the SnapshotTemplateId parameter, ignore the following parameters:
  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. # Specifies the sprite snapshot configuration if required.
  15. spriteSnapshotConfig = {'CellWidth': 120, 'CellHeight': 68, 'Columns': 3,
  16. 'Lines': 10, 'Padding': 20, 'Margin': 50}
  17. # Keeps the original sprite snapshots.
  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())

Query snapshot data

For more information about the request and response parameters, see 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())