All Products
Search
Document Center

ApsaraVideo Media Processing:Transcode media files

Last Updated:Mar 14, 2024

Transcoding is to convert an audio or video file into another one or more audio or video files to adapt to different network bandwidths, terminal devices, and user needs. If the transcoding jobs and workflows created in the ApsaraVideo Media Processing (MPS) console cannot meet your business requirements, you can call the SubmitJobs operation to submit transcoding jobs. This topic provides the sample code for using MPS SDK for Python to transcode media files.

Sample code

import os
import json

from urllib.parse import quote
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.SubmitJobsRequest import SubmitJobsRequest

# Obtain an AccessKey ID and an AccessKey secret from environment variables.
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# The region_id parameter specifies the ID of the region in which your MPS service is deployed. For information about supported regions, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/service-regions.
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)

mps_region_id = 'cn-shanghai'
# The ID of the MPS queue.
pipeline_id = '9ba1a2c3dc22c4df9c6****'
# 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-shanghai'
# The name of the OSS bucket.
oss_bucket = '<your bucket name>'
# 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'

request = SubmitJobsRequest()
request.set_accept_format('json')
# The job input. The input object must be URL-encoded.
job_input = {'Location': oss_location,
             'Bucket': oss_bucket,
             'Object': quote(oss_input_object) }
request.set_Input(json.dumps(job_input))
# The job output.
output = {'OutputObject': quote(oss_output_object)}
output['Container'] = {'Format': 'mp4'}
output['Video'] = {'Codec': 'H.264',
                   'Bitrate': 1500,
                   'Width': 1280,
                   'Fps': 25}
output['Audio'] = {'Codec': 'AAC',
                   'Bitrate': 128,
                   'Channels': 2,
                   'Samplerate': 44100}
output['TemplateId'] = template_id
outputs = [output]
request.set_Outputs(json.dumps(outputs))
request.set_OutputBucket(oss_bucket)
request.set_OutputLocation(oss_location)
request.set_PipelineId(pipeline_id)

response = client.do_action_with_exception(request)
# Display the output.
print(str(response, encoding='utf-8'))

References