全部产品
媒体转码

转码

更新时间:2017-07-28 11:37:00   分享:   

对存储于OSS上的视频文件,经过转码获得适合在PC、TV以及移动终端上播放的文件。提供一系列转码接口 ,比如SubmitJobs,CancelJob、QueryJobList、ListJob。

提交转码作业

批量提交转码作业接口,一个转码输出会生成一个转码作业,接口返回转码作业列表。

  1. python
  2. import json
  3. from aliyunsdkmts.request.v20140618 import SubmitJobsRequest
  4. from aliyunsdkcore import client
  5. from urllib import quote
  6. region = '<region>'
  7. access_key_id = '<access_key_id>'
  8. access_key_secret = '<access_key_secret>'
  9. pipeline_id = ‘<pipeline_id>’
  10. oss_location = ‘<oss_location>’
  11. input_object = ‘<input_object>’
  12. input_bucket = ‘<input_bucket>’
  13. output_object = ‘<output_objcet>’
  14. output_bucket = ‘<output_bucket>’
  15. transcode_template_id = ‘<transcode_template_id>’
  16. clt = client.AcsClient(access_key_id, access_key_secret, region)
  17. input_file = {
  18. 'Location':oss_location,
  19. 'Bucket':input_bucket,
  20. 'Object':quote(input_object)
  21. }
  22. outputs = [{
  23. 'OutputObject': quote(output_object),
  24. 'TemplateId': transcode_template_id
  25. }]
  26. request = SubmitJobsRequest.SubmitJobsRequest()
  27. request.set_accept_format('json')
  28. request.set_Input(json.dumps(input_file))
  29. request.set_OutputBucket(output_bucket)
  30. request.set_OutputLocation(oss_location)
  31. request.set_Outputs(json.dumps(outputs))
  32. request.set_PipelineId(pipeline_id)
  33. response = json.loads(clt.do_action_with_exception(request))
  34. print(response['JobResultList']['JobResult'][0]['Job']['JobId'])

取消转码作业

对于状态处于”已提交”的作业,可以调用CancelJob接口取消作业。

  1. python
  2. import json
  3. from aliyunsdkmts.request.v20140618 import CancelJobRequest
  4. from aliyunsdkcore import client
  5. from aliyunsdkcore.acs_exception.exceptions import ServerException
  6. #cn-hangzhou,cn-shagnhai, etc.
  7. region = '<region>'
  8. access_key_id = '<access_key_id>'
  9. access_key_secret = '<access_key_secret>'
  10. #job_id to be cancelled
  11. job_id=‘<job_id>’
  12. clt = client.AcsClient(access_key_id, access_key_secret, region)
  13. request = CancelJobRequest.CancelJobRequest()
  14. request.set_accept_format('json')
  15. request.set_JobId(job_id)
  16. try:
  17. clt.do_action_with_exception(request)
  18. except ServerException, e:
  19. print("RequestId:" + e.get_request_id() + ",\tCode:" + e.get_error_code() + ",\tMessage:" + e.get_error_msg())
  20. else:
  21. print(job_id + " is been cancelled")

查询转码作业

通过转码作业ID列表,调用QueryJobList接口批量查询作业信息。

  1. python
  2. import json
  3. from aliyunsdkmts.request.v20140618 import QueryJobListRequest
  4. from aliyunsdkcore import client
  5. from urllib import quote
  6. from urllib import unquote
  7. import urllib2
  8. #cn-hangzhou,cn-shagnhai, etc.
  9. region = '<region>'
  10. access_key_id = '<access_key_id>'
  11. access_key_secret = '<access_key_secret>'
  12. #job_id split by ‘,’, at most ten items
  13. job_id_list = ‘<job_id1,job_id2…>’
  14. clt = client.AcsClient(access_key_id, access_key_secret, region)
  15. request = QueryJobListRequest.QueryJobListRequest()
  16. request.set_accept_format('json')
  17. request.set_JobIds(job_id_list)
  18. response = json.loads(clt.do_action_with_exception(request))
  19. jobs = response['JobList']['Job']
  20. for transcode_job in jobs:
  21. print("JobId: " + transcode_job['JobId'])
  22. print ("JobStatus: " + transcode_job['State'])
  23. if transcode_job['State'] == "TranscodeSuccess":
  24. output_file = transcode_job['Output']['OutputFile']
  25. print ("OutputFile OSS url is http://%s.%s.aliyuncs.com/%s" % (output_file['Bucket'], output_file['Location'] , unquote(output_file['Object'])))
  26. elif transcode_job['State'] == "TranscodeFail":
  27. print("Transcode failed, Code is %s, Message is %s" % (transcode_job['Code'], transcode_job['Message']))
  28. print

列出转码作业

通过作业状态、创建时间区间、转码管道,调用ListJob接口列出转码作业(按CreationTime降序排列)。

  1. python
  2. import json
  3. from aliyunsdkmts.request.v20140618 import ListJobRequest
  4. from aliyunsdkcore import client
  5. from urllib import quote
  6. from urllib import unquote
  7. import urllib2
  8. region = '<region>'
  9. access_key_id = '<access_key_id>'
  10. access_key_secret = '<access_key_secret>'
  11. #UTC date format: YYYY-MM-DDThh:mm:ssZ, eg:2017-02-10T12:00:00Z, optional
  12. start_of_job_created_time_range = ‘<start_of_job_created_time_range>’
  13. #UTC date format: YYYY-MM-DDThh:mm:ssZ, eg:2017-03-10T12:00:00Z, optional
  14. end_of_job_created_time_range = ‘<end_of_job_created_time_range>’
  15. #optional, default 10, range:[1,100]
  16. maximum_page_size = 5L
  17. clt = client.AcsClient(access_key_id, access_key_secret, region)
  18. request = ListJobRequest.ListJobRequest()
  19. request.set_accept_format('json')
  20. request.set_StartOfJobCreatedTimeRange(start_of_job_created_time_range)
  21. request.set_EndOfJobCreatedTimeRange(end_of_job_created_time_range)
  22. response = json.loads(clt.do_action_with_exception(request))
  23. jobs = response['JobList']['Job']
  24. for transcode_job in jobs:
  25. print("JobId: " + transcode_job['JobId'])
  26. print ("JobStatus: " + transcode_job['State'])
  27. if transcode_job['State'] == "TranscodeSuccess":
  28. output_file = transcode_job['Output']['OutputFile']
  29. print ("OutputFile OSS url is http://%s.%s.aliyuncs.com/%s" % (output_file['Bucket'], output_file['Location'] , unquote(output_file['Object'])))
  30. elif transcode_job['State'] == "TranscodeFail":
  31. print("Transcode failed, Code is %s, Message is %s" % (transcode_job['Code'], transcode_job['Message']))
  32. print
  33. print ("NextPageToken is %s" % (response['NextPageToken']));
本文导读目录
本文导读目录
以上内容是否对您有帮助?