All Products
Search
Document Center

ApsaraVideo Media Processing:Add a video file

Last Updated:Nov 15, 2024

After you add a media file to the media library of ApsaraVideo Media Processing (MPS), you can specify the ID of a workflow to process the media file. This topic provides an example on how to use MPS SDK for Python V2.0 to add a media file to the media library and trigger a specific workflow to process the media file.

Note

If the directory of the media file that you want to add meets the triggering rules of a workflow, the workflow is triggered. Otherwise, the workflow is not triggered. For more information, see Workflow triggering rules for files.

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()
        add_media_request = mts_20140618_models.AddMediaRequest(
            # The path of the input file.
            file_url='http://bucket.oss-cn-hangzhou.aliyuncs.com/A/B/C/test.mp4',
            # The media title.
            title='mytest',
            # The description.
            description='A test video',
            # The URL of the thumbnail.
            cover_url='http://bucket.oss-cn-hangzhou.aliyuncs.com/example/1.png',
            # The tags.
            tags='tag1,tag2',
            # The ID of the media workflow.
            media_workflow_id='07da6c65da7f458997336e0de192****',
            # The custom data of the media workflow.
            media_workflow_user_data='test',
            // Specify whether to check if the media workflow supports the specified input path.
            input_unbind=False,
            # The ID of the category to which the media file belongs.
            cate_id=123,
            # Set the override_params parameter.
            override_params='{“subtitleTransNodeName”:{“InputConfig”:{“Format”:”stl”,”InputFile”:{“URL”:”http://exampleBucket.oss-cn-hangzhou.aliyuncs.com/package/example/CENG.stl"}}}}'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # Write your own code to display the response of the API operation if necessary.
            client.add_media_with_options(add_media_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()
        add_media_request = mts_20140618_models.AddMediaRequest(
            # The path of the input file.
            file_url='http://bucket.oss-cn-hangzhou.aliyuncs.com/A/B/C/test.mp4',
            # The media title.
            title='mytest',
            # The description.
            description='A test video',
            # The URL of the thumbnail.
            cover_url='http://bucket.oss-cn-hangzhou.aliyuncs.com/example/1.png',
            # The tags.
            tags='tag1,tag2',
            # The ID of the media workflow.
            media_workflow_id='07da6c65da7f458997336e0de192****',
            # The custom data of the media workflow.
            media_workflow_user_data='test',
            // Specify whether to check if the media workflow supports the specified input path.
            input_unbind=False,
            # The ID of the category to which the media file belongs.
            cate_id=123,
            # Set the override_params parameter.
            override_params='{“subtitleTransNodeName”:{“InputConfig”:{“Format”:”stl”,”InputFile”:{“URL”:”http://exampleBucket.oss-cn-hangzhou.aliyuncs.com/package/example/CENG.stl"}}}}'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # Write your own code to display the response of the API operation if necessary.
            await client.add_media_with_options_async(add_media_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:])