This topic describes how to use ApsaraVideo Media Processing (MPS) SDK for Python to transcode media files. This topic also provides the complete sample code.

For more information about the API operation for transcoding and parameters of the operation, see SubmitJobs.

  1. Create an AcsClient instance.
    client = AcsClient(access_key_id, access_key_secret, mps_region_id);
  2. Create a request and set the required parameters.
    request = SubmitJobsRequest.SubmitJobsRequest()
    request.set_accept_format('json')
  3. Set the transcoding parameters.
    • Input
      job_input = {'Location': oss_location,
                    'Bucket': oss_bucket,
                    'Object': quote(oss_input_object) }
      request.set_Input(json.dumps(job_input))
    • Output
      output = {'OutputObject': quote(oss_output_object)}
      • Container
        output['Container'] = {'Format': 'mp4'}
      • Video
        output['Video'] = {'Codec': 'H.264',
                              'Bitrate': 1500,
                              'Width': 1280,
                              'Fps': 25}
      • Audio
        output['Audio'] = {'Codec': 'AAC',
                              'Bitrate': 128,
                              'Channels': 2,
                              'Samplerate': 44100}
      • TemplateId
        output['TemplateId'] = template_id
    • PipelineId
      request.set_PipelineId(pipeline_id)
  4. Send the request and handle the response or exception.
    response_str = client.do_action_with_exception(request)
    response = json.loads(response_str)
    print 'RequestId is:', response['RequestId']
    if response['JobResultList']['JobResult'][0]['Success']:
    print 'JobId is:', response['JobResultList']['JobResult'][0]['Job']['JobId']
    else:
    print ('SubmitJobs Failed code:',
          response['JobResultList']['JobResult'][0]['Code'],
          ' message:',
          response['JobResultList']['JobResult'][0]['Message'])
Sample code
# -*- coding: utf8 -*-
import json
from urllib import quote
from aliyunsdkcore.client import AcsClient
from aliyunsdkmts.request.v20140618 import SubmitJobsRequest
access_key_id = 'xxx'
access_key_secret = 'xxx'

# The ID of the region in which you use the MPS service.
mps_region_id = 'cn-hangzhou'
# The ID of the MPS queue.
pipeline_id = 'xxx'
# The ID of the transcoding template.
template_id = 'S00000001-200010'
# The location information about the region in which the Object Storage Service (OSS) bucket resides.
oss_location = 'oss-cn-hangzhou'
# The name of the OSS bucket.
oss_bucket = 'xxx'
# The path of the input file, which is an OSS object.
oss_input_object = 'input.mp4'
# The path of the output file, which is an OSS object.
oss_output_object = 'output.mp4'
# Create an AcsClient object.
client = AcsClient(access_key_id, access_key_secret, mps_region_id);
# Create a request and set the required parameters.
request = SubmitJobsRequest.SubmitJobsRequest()
request.set_accept_format('json')
# Input
job_input = {'Location': oss_location,
             'Bucket': oss_bucket,
             'Object': quote(oss_input_object) }
request.set_Input(json.dumps(job_input))
# Output
output = {'OutputObject': quote(oss_output_object)}
# Container
output['Container'] = {'Format': 'mp4'}
# Video
output['Video'] = {'Codec': 'H.264',
                   'Bitrate': 1500,
                   'Width': 1280,
                   'Fps': 25}
# Audio
output['Audio'] = {'Codec': 'AAC',
                   'Bitrate': 128,
                   'Channels': 2,
                   'Samplerate': 44100}
# TemplateId
output['TemplateId'] = template_id
outputs = [output]
request.set_Outputs(json.dumps(outputs))
request.set_OutputBucket(oss_bucket)
request.set_OutputLocation(oss_location)
# PipelineId
request.set_PipelineId(pipeline_id)
# Send the request and handle the response or exception.
response_str = client.do_action_with_exception(request)
response = json.loads(response_str)
print 'RequestId is:', response['RequestId']
if response['JobResultList']['JobResult'][0]['Success']:
    print 'JobId is:', response['JobResultList']['JobResult'][0]['Job']['JobId']
else:
    print ('SubmitJobs Failed code:',
           response['JobResultList']['JobResult'][0]['Code'],
           ' message:',
           response['JobResultList']['JobResult'][0]['Message'])