Creating a Packaging Task

You can create a video packaging task by creating an MpcClient instance and setting related parameters. The task is used to wrap the compressed video in a packetized format.

Prerequisites

  • You have bought Object Storage Service (OBS) resources and uploaded an input file to an OBS bucket which is in the same region (for example, CN North-Beijing4) as MPC by referring to Uploading Media Files.
  • MPC has been authorized to access OBS resources. For details, see Authorizing Access to Cloud Resources.

Core Code

  1. Create MPC configuration items.
    These configuration items are used for MPC to obtain authorization. Table 1 describes the parameters.
    1
    2
    3
    4
    5
       MpcConfig mpcConfig = new MpcConfig();
           mpcConfig.setEndPoint(endPoint);// Set the endpoint.
           mpcConfig.setProjectId(projectId);// Set the project ID.
           mpcConfig.setSk(sk);// Set the SK.
           mpcConfig.setAk(ak);// Set the AK.
    
    Table 1 MPC parameters

    Parameter

    Type

    Description

    endPoint

    String

    Endpoint. For details, see Obtaining an Endpoint.

    ProjectId

    String

    Project ID. For details, see Obtaining a Project ID and Account Name.

    ak

    String

    Access key ID (AK). For details, see Obtaining the AK/SK Pair.

    sk

    String

    Secret Access Key (SK) used together with the AK. For details, see Obtaining the AK/SK Pair.

  2. Create an MpcClient instance.

    If no proxy server is configured, you can directly create an MpcClient instance.

    1
    2
         // Create an MpcClient instance.
         MpcClient mpcClient = new MpcClient(mpcConfig);
    

    If a proxy server needs to be configured, set proxy parameters and then transfer the proxy as a constructor to the MpcClient instance.

    1
    2
    3
    4
    5
    6
    7
    8
    // Configure the proxy.
    ClientConfig clientConfig = new ClientConfig();
           clientConfig.setProxyHost(proxyHost);// Set the IP address of the proxy server.
           clientConfig.setProxyPort(Integer.parseInt(proxyPort));// Set the port number of the proxy server.
           clientConfig.setProxyUserName(proxyUserName);// Set the username for accessing the proxy server.
           clientConfig.setProxyPassword(proxyPassword);// Set the password for accessing the proxy server.
    // Use the constructor to initialize MpcClient.
     MpcClient mpcClient =new MpcClient(mpcConfig, clientConfig);
    
  3. Create a packaging task.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    CreateRemuxTaskReq req = new CreateRemuxTaskReq();
    
    ObsObjInfo input = new ObsObjInfo();
    input.setBucket("obs-gg");
    input.setLocation("cn-north-7");
    input.setObject("problem/ts_14M_1080p.ts");
    req.setInput(input);
    
    ObsObjInfo output = new ObsObjInfo();
    output.setBucket("obs-gg");
    output.setLocation("cn-north-7");
    output.setObject("output/ts");
    req.setOutput(output);
    
    RemuxOutputParam outputParam = new RemuxOutputParam();
    // Set the output format.
    outputParam.setFormat(RemuxOutputParam.FormatEnum.MP4);
    // Set the segment duration.
    outputParam.setSegmentDuration(7);
    req.setOutputParam(outputParam);
    
    CreateRemuxTaskResponse rsp = mpcClient.creatRemuxTask(req);
    System.out.println("craete: " + gson.toJson(rsp) + "\n");
    

Full Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import com.google.gson.Gson;
import com.huawei.mpc.client.MpcClient;
import com.huawei.mpc.client.MpcConfig;
import com.huawei.mpc.model.CommonCreateTaskRsp;
import com.huawei.mpc.model.ObsObjInfo;
import com.huawei.mpc.model.remux.*;

Gson gson = new Gson();

MpcConfig mpcConfig = new MpcConfig();
mpcConfig.setEndPoint("mpc.cn-north-4.myhuaweicloud.com:443");
mpcConfig.setProjectId("ca225055625d48f6888f60730fb82657");
mpcConfig.setAk("Your project ID");
mpcConfig.setSk("****************************************");

MpcClient mpcClient = new MpcClient(mpcConfig);

CreateRemuxTaskReq req = new CreateRemuxTaskReq();

ObsObjInfo input = new ObsObjInfo();
input.setBucket("obs-gg");
input.setLocation("cn-north-7");
input.setObject("problem/ts_14M_1080p.ts");
req.setInput(input);

ObsObjInfo output = new ObsObjInfo();
output.setBucket("obs-gg");
output.setLocation("cn-north-7");
output.setObject("output/ts");
req.setOutput(output);

RemuxOutputParam outputParam = new RemuxOutputParam();
// Set the output format.
outputParam.setFormat(RemuxOutputParam.FormatEnum.MP4);
// Set the segment duration.
outputParam.setSegmentDuration(7);
req.setOutputParam(outputParam);

CreateRemuxTaskResponse rsp = mpcClient.creatRemuxTask(req);
System.out.println("craete: " + gson.toJson(rsp) + "\n");