All Products
Search
Document Center

ApsaraVideo Media Processing:Transcode media files

Last Updated:Nov 15, 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 an example on how to use MPS SDK for Python V2.0 to submit a transcoding job.

Sample code

import os
import sys

from typing import List

from alibabacloud_mts20140618.client import Client as Mts20140618Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_mts20140618 import models as mts_20140618_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client() -> Mts20140618Client:
        """
        Use your AccessKey ID and AccessKey secret to initialize a client.
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. ,
            access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
            # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. ,
            access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
        )
        config.endpoint = f'mts.cn-hangzhou.aliyuncs.com'
        return Mts20140618Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        submit_jobs_request = mts_20140618_models.SubmitJobsRequest(
            # The job input.
            input='{"Bucket":"exampleBucket","Location":"oss-cn-hangzhou","Object":"example.flv","Referer": "The parameter that you set in the Object Storage Service (OSS) console to enable the hotlink protection feature"}',
            # The job output configuration.
            outputs='[{"OutputObject":"exampleOutput.mp4","TemplateId":"6181666213ab41b9bc21da8ff5ff****","WaterMarks":[{"InputFile":{"Bucket":"exampleBucket","Location":"oss-cn-hangzhou","Object":"image_01.png"},"WaterMarkTemplateId":"9b772ce2740d4d55876d8b542d47****"}],"UserData":"testid-001"}]',
            # The OSS bucket that stores the output file.
            output_bucket='exampleBucket',
            # The region in which the OSS bucket resides.
            output_location='oss-cn-hangzhou',
            # The ID of the MPS queue.
            pipeline_id='dd3dae411e704030b921e52698e5****'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # Write your own code to display the response of the API operation if necessary.
            client.submit_jobs_with_options(submit_jobs_request, runtime)
        except Exception as error:
            # Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only. 
            # The error message.
            print(error.message)
            # The URL of the corresponding error diagnostics page.
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        submit_jobs_request = mts_20140618_models.SubmitJobsRequest(
            # The job input. 
            input='{"Bucket":"exampleBucket","Location":"oss-cn-hangzhou","Object":"example.flv","Referer": "The parameter that you set in the OSS console to enable the hotlink protection feature"}',
            # The job output configuration. 
            outputs='[{"OutputObject":"exampleOutput.mp4","TemplateId":"6181666213ab41b9bc21da8ff5ff****","WaterMarks":[{"InputFile":{"Bucket":"exampleBucket","Location":"oss-cn-hangzhou","Object":"image_01.png"},"WaterMarkTemplateId":"9b772ce2740d4d55876d8b542d47****"}],"UserData":"testid-001"}]',
            # The OSS bucket that stores the output file. 
            output_bucket='exampleBucket',
            # The region in which the OSS bucket resides. 
            output_location='oss-cn-hangzhou',
            # The ID of the MPS queue. 
            pipeline_id='dd3dae411e704030b921e52698e5****'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # Write your own code to display the response of the API operation if necessary.
            await client.submit_jobs_with_options_async(submit_jobs_request, runtime)
        except Exception as error:
            # Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only. 
            # The error message.
            print(error.message)
            # The URL of the corresponding error diagnostics page.
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])

References