edit-icon download-icon

Transcoding

Last Updated: Apr 19, 2018

Procedure

  1. Create AcsClient instance.

    1. client = AcsClient(access_key_id, access_key_secret, mps_region_id);
  2. Create request, and set parameters.

    1. request = SubmitJobsRequest.SubmitJobsRequest()
    2. request.set_accept_format('json')
  3. Transcoding parameters.

    • Input

      1. job_input = {'Location': oss_location,
      2. 'Bucket': oss_bucket,
      3. 'Object': quote(oss_input_object) }
      4. request.set_Input(json.dumps(job_input))
    • Output

      1. output = {'OutputObject': quote(oss_output_object)}
      • Container

        1. output['Container'] = {'Format': 'mp4'}
      • Video

        1. output['Video'] = {'Codec': 'H.264',
        2. 'Bitrate': 1500,
        3. 'Width': 1280,
        4. 'Fps': 25}
      • Audio

        1. output['Audio'] = {'Codec': 'AAC',
        2. 'Bitrate': 128,
        3. 'Channels': 2,
        4. 'Samplerate': 44100}
      • TemplateId

        1. output['TemplateId'] = template_id
    • PipelineId

      1. request.set_PipelineId(pipeline_id)
  4. Initiate API request and display returned value.

    1. response_str = client.do_action_with_exception(request)
    2. response = json.loads(response_str)
    3. print 'RequestId is:', response['RequestId']
    4. if response['JobResultList']['JobResult'][0]['Success']:
    5. print 'JobId is:', response['JobResultList']['JobResult'][0]['Job']['JobId']
    6. else:
    7. print ('SubmitJobs Failed code:',
    8. response['JobResultList']['JobResult'][0]['Code'],
    9. ' message:',
    10. response['JobResultList']['JobResult'][0]['Message'])

Full code

  1. # -*- coding: utf8 -*-
  2. import json
  3. from urllib import quote
  4. from aliyunsdkcore.client import AcsClient
  5. from aliyunsdkmts.request.v20140618 import SubmitJobsRequest
  6. access_key_id = 'xxx'
  7. access_key_secret = 'xxx'
  8. mps_region_id = 'cn-hangzhou'
  9. pipeline_id = 'xxx'
  10. template_id = 'S00000001-200010'
  11. oss_location = 'oss-cn-hangzhou'
  12. oss_bucket = 'xxx'
  13. oss_input_object = 'input.mp4'
  14. oss_output_object = 'output.mp4'
  15. # Create AcsClient instance
  16. client = AcsClient(access_key_id, access_key_secret, mps_region_id);
  17. # Create request,and set parameters
  18. request = SubmitJobsRequest.SubmitJobsRequest()
  19. request.set_accept_format('json')
  20. # Input
  21. job_input = {'Location': oss_location,
  22. 'Bucket': oss_bucket,
  23. 'Object': quote(oss_input_object) }
  24. request.set_Input(json.dumps(job_input))
  25. # Output
  26. output = {'OutputObject': quote(oss_output_object)}
  27. # Ouput->Container
  28. output['Container'] = {'Format': 'mp4'}
  29. # Ouput->Video
  30. output['Video'] = {'Codec': 'H.264',
  31. 'Bitrate': 1500,
  32. 'Width': 1280,
  33. 'Fps': 25}
  34. # Ouput->Audio
  35. output['Audio'] = {'Codec': 'AAC',
  36. 'Bitrate': 128,
  37. 'Channels': 2,
  38. 'Samplerate': 44100}
  39. # Ouput->TemplateId
  40. output['TemplateId'] = template_id
  41. outputs = [output]
  42. request.set_Outputs(json.dumps(outputs))
  43. request.set_OutputBucket(oss_bucket)
  44. request.set_OutputLocation(oss_location)
  45. # PipelineId
  46. request.set_PipelineId(pipeline_id)
  47. # Initiate API request and diaplay returned value
  48. response_str = client.do_action_with_exception(request)
  49. response = json.loads(response_str)
  50. print 'RequestId is:', response['RequestId']
  51. if response['JobResultList']['JobResult'][0]['Success']:
  52. print 'JobId is:', response['JobResultList']['JobResult'][0]['Job']['JobId']
  53. else:
  54. print ('SubmitJobs Failed code:',
  55. response['JobResultList']['JobResult'][0]['Code'],
  56. ' message:',
  57. response['JobResultList']['JobResult'][0]['Message'])
Thank you! We've received your feedback.