Help Center/ Media Processing Center/ API Reference/ Application Examples/ Example 2: Submit a Video Transcoding Task
Updated on 2024-03-14 GMT+08:00

Example 2: Submit a Video Transcoding Task

Scenario

If you need to transcode a video file stored in an OBS bucket and add an image watermark, you can call an MPC API to transcode the video file.

This section describes how to submit a video transcoding task by invoking an API.

Constraints

Cross-region transcoding is not supported. For example, MPC in the CN North-Beijing1 region cannot transcode video files stored in an OBS bucket in the CN North-Beijing4 region.

Prerequisites

  • You have obtained the project ID of the region where the transcoding task is to be created. For details, see Obtaining a Project ID.
  • You have determined the region where the transcoding task to be created is located and obtained the endpoint of MPC for this region from the Regions and Endpoints.
  • You have stored the video file to be transcoded in an OBS bucket. If you have not, call the API for uploading an object to upload the file.
  • You have created an OBS bucket where transcoding outputs will be stored. If you have not, call the API for creating a bucket.

Procedure

  1. Obtain a user token and use it to authenticate the calling of MPC APIs.

    Obtain the username, password, domainname, and projectname from the My Credentials page. Call the following API to obtain the token. For details, see Making an API Request.
    POST https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens
    Content-Type: application/json
    {
      "auth": {
        "identity": {
          "methods": [
            "password"
          ],
          "password": {
            "user": {
              "name": "********", //Username
              "password": "********", //Login password
              "domain": {
                "name": "domainname" //Name of the account to which the user belongs
              }
            }
          }
        },
        "scope": {
          "project": {
             "name": "*******"  //Project name
          }
        }
      }
    }

    As shown in Figure 1, information in the red box indicates the user token.

    Figure 1 Obtaining a user token

  2. Create a video transcoding template and obtain the transcoding template ID. For details about how to call the API for creating a transcoding template, see Example 1: Create a Transcoding Template.
  3. Create a watermark template.

    1. Select the POST request method and enter the request URI.
      https://{endpoint}/v1/{project_id}/template/watermark
    2. In the request header, add X-Auth-Token and set its value to be the token obtained in 1.
    3. Specify the following parameters in the request body:
      {
        "template_name": "watermark_1920*1080_10*10_ BottomLeft",
        "type": "Image",
        "image_process": "Grayed",
        "width": "1920",
        "height": "1080",
        "dx": "10",
        "dy": "10",
        "referpos": "BottomLeft",
        "timeline_start": "6",
        "timeline_duration": "8"
      }
    4. If the request succeeds, the watermark template ID is returned.
      {
        "template_id":242
      }

  4. Submit the transcoding task.

    1. Select the POST request method and enter the request URI.
       https://{endpoint}/v1/{project_id}/transcodings
    2. In the request header, add X-Auth-Token and set its value to be the token obtained in 1.
    3. Specify the following parameters in the request body:
      {
      "input": {
                 "bucket": "example-bucket",
                 "location": "region01",
                 "object": "/mpc/input/sample.MP4"
      },
      "output": {
                 "bucket": "example-bucket",
                 "location": "region01",
                 "object": "/mpc/output/"
      },
      "trans_template_id":[1002],
      "watermarks": [{
      	"input": {
      		"bucket": "example-bucket",
      		"location": "region01",
      		"object": "meipai.jpg"
      	},
      	"template_id":242
      }],
      "priority":"9",
      "audit":{
      	"position":2,
               "index":2
      }
      } 
    4. If the request succeeds, the transcoding task ID is returned.
      {
        "task_id":100211
      }

  5. Query the task status.

    1. Select the GET request method and enter the request URI.
      https://{endpoint}/v1/{project_id}/transcodings?task_id=100211
    2. In the request header, add X-Auth-Token and set its value to be the token obtained in 1.
    3. If the request succeeds, details about the transcoding task are returned.