snapshot capturing task
Guided reading
Python
snapshot capturing task
Author
c***r
Released
Nov 14, 2023 03:25:58

1. Functions

You can integrate Media Processing Center (MPC) server SDKs provided by Huawei Cloud to call MPC APIs, making it easier for you to use the service. This sample shows how to use the Python SDK to create, cancel, and query a snapshot capturing task.

What You Will Learn

Using the Python SDK to create, cancel, and query a snapshot capturing task on MPC

2. Development Sequence Diagram

Creating a Snapshot Capturing Task

image

Deleting a Snapshot Capturing Task

image

Querying Snapshot Capturing Tasks

image

3. Prerequisites

    1. You haveregisteredwith Huawei Cloud and passedreal-name authentication.
    1. The development environment (Python 3 or later) is available.
    1. You have obtained the access key (AK) and secret access key (SK) of the Huawei Cloud account. To create and view an AK/SK, go to the My Credentials > Access Keys page of the Huawei Cloud console. For details, see Access Keys.
    1. You have obtained the project ID of the corresponding region of MPC. You can view the project ID on the My Credentials > API Credentials page of the Huawei Cloud console. For details, seeAPI Credentials
    1. You have uploaded the media files to an OBS bucket in the regionof MPC, and authorized MPC to access the OBS bucket. For details, see Uploading Media Files and Authorizing Access to Cloud Resources.

4. Obtaining and Installing the SDK

The MPC SDK supports Python 3 or later. Run python --version to check the Python version.

Before using the server SDK, you need to install huaweicloudsdkcore and huaweicloudsdkmpc. For details about the SDK version, seeSDK Development Center

Using pip

Run the following commands to install the Python SDK core library and related service libraries:

# Install the core library. pip install huaweicloudsdkcore # Install the MPC service library. pip install huaweicloudsdkmpc

Using Source Code

Run the following commands to install the Python SDK core library and related service libraries:

# Install the core library. cd huaweicloudsdkcore-${version} python setup.py install # Install the MPC service library. cd huaweicloudsdkmpc-${version} python setup.py install

6. Key Code Snippets

6.1. Creating a Snapshot Capturing Task

@staticmethod def create_thumbnails_task(): """ Create a snapshot capturing task. @return taskId: ID of the submitted snapshot capturing task. """ # Set the input file path. _input = ObsObjInfo( bucket="<example-bucket>", location="<Region ID>", object="<example-path/input.mp4>" ) output = ObsObjInfo( bucket="<example-bucket>", location="<Region ID>", object="<example-path/output>" ) # Create a snapshot capturing request. request = CreateThumbnailsTaskRequest() body = CreateThumbReq() list_thumbnail_para_dots = [] list_thumbnail_para_dots.append(50000) # Set the snapshot capturing type and capture a snapshot at a specified time point. thumbnail_parabody = ThumbnailPara() # Set the sampling type and capture a snapshot at a specified time point. thumbnail_parabody.type = "DOTS" thumbnail_parabody.output_filename = "photo" thumbnail_parabody.time = 10 thumbnail_parabody.start_time = 100 thumbnail_parabody.duration = 1 thumbnail_parabody.dots = list_thumbnail_para_dots thumbnail_parabody.format = 1 thumbnail_parabody.width = 96 thumbnail_parabody.height = 96 body.thumbnail_para = thumbnail_parabody body.output = output body.input = _input request.body = body # Send a snapshot capturing request. try: response = ThumbnailsTaskSolution.init_mpc_client().create_thumbnails_task(request) print(response) return response.task_id except ConnectionException as e: logger.error("The authentication is rejected. Check whether the AK/SK is correct: ", e) except RequestTimeoutException as e: logger.error("The server processing times out and does not return a response: ", e) except ServiceResponseException as e: logger.error("HttpStatusCode:{}, RequestId:{}, ErrorCode:{}, ErrorMsg:{}", e.status_code, e.request_id, e.error_code, e.error_msg) return ""

6.2. Canceling a Snapshot Capturing Task

@staticmethod def delete_thumbnails_task(task_id): """ Delete a snapshot capturing task. @param taskId: Task ID """ request = DeleteThumbnailsTaskRequest() request.task_id = task_id try: response = ThumbnailsTaskSolution.init_mpc_client().delete_thumbnails_task(request) print(response) except ConnectionException as e: logger.error("The authentication is rejected. Check whether the AK/SK is correct: ", e) except RequestTimeoutException as e: logger.error("The server processing times out and does not return a response: ", e) except ServiceResponseException as e: logger.error("HttpStatusCode:{}, RequestId:{}, ErrorCode:{}, ErrorMsg:{}", e.status_code, e.request_id, e.error_code, e.error_msg)

6.3. Querying Snapshot Capturing Tasks

@staticmethod def list_thumbnails_task(task_id): """ Query snapshot capturing tasks. @param taskId: Task ID """ request = ListThumbnailsTaskRequest() list_request_task_id = [] list_request_task_id.append(task_id) request.task_id = list_request_task_id try: response = ThumbnailsTaskSolution.init_mpc_client().list_thumbnails_task(request) print(response) except ConnectionException as e: logger.error("The authentication is rejected. Check whether the AK/SK is correct: ", e) except RequestTimeoutException as e: logger.error("The server processing times out and does not return a response: ", e) except ServiceResponseException as e: logger.error("HttpStatusCode:{}, RequestId:{}, ErrorCode:{}, ErrorMsg:{}", e.status_code, e.request_id, e.error_code, e.error_msg)

7. Execution Results

The ID of a created snapshot capturing task is returned.

{"task_id": "1024"}

Query the status and result of a snapshot capturing task.

{ "task_array" : [ { "task_id" : 2528, "status" : "SUCCEEDED", "create_time" : 20201118121333, "end_time" : 20201118121336, "input" : { "bucket" : "example-bucket", "location" : "region01", "object" : "example-input.ts" }, "output" : { "bucket" : "example-bucket", "location" : "region01", "object" : "example-output/example-path" }, "thumbnail_info" : [ { "pic_name" : "9.jpg" }, { "pic_name" : "5.jpg" } ] } ], "is_truncated" : 0, "total" : 1 }

Cancel a delivered snapshot capturing task.

When a snapshot capturing task is canceled, the status code is 204 and the response body is empty.

When a snapshot capturing task fails to be canceled, the status code is 400 and the response body is as follows:

{ "error_code" : "MPC.10202", "error_msg" : "Invalid request parameter" }

8. Reference

The code project in this sample is used only for demonstration. During actual development, strictly follow the development guide. Learn more details from Development Guide.