Creating a Snapshot Task

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 request for creating a snapshot task.
    The request contains the input file and output file paths. For details about the parameters, see Creating a Snapshot Task.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    // Set request parameters.
    CreateThumbnailRequest createThumbnailRequest = new CreateThumbnailRequest();
    
    // Set the storage location of an input file.
    ObsObjInfo input  = new ObsObjInfo();
    // OBS bucket name
    input.setBucket("bucketName");
    // OBS path
    input.setObject("objectKey");
    // Region where an OBS bucket is deployed
    input.setLocation("cn-north-4");
    createThumbnailRequest.setInput(input);
    
    // Set the storage location of an output file.
    ObsObjInfo output  = new ObsObjInfo();
    output.setBucket("bucketName");
    output.setObject("path");
    output.setLocation("cn-north-4");
    createThumbnailRequest.setOutput(output);
    
  4. Set snapshot parameters.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    // Set snapshot parameters.
    CreateThumbnailRequest.ThumbnailPara thumbnailPara = new CreateThumbnailRequest.ThumbnailPara();
    
    // Sampling type. Three options are available: PERCENT, TIME, and DOTS. PERCENT indicates sampling based on a percentage of the video duration. Time indicates sampling by interval. DOTS indicating sampling based on a datetime array. Now only TIME and DOTS are supported.
    thumbnailPara.setType(CreateThumbnailRequest.ThumbnailPara.TypeEnum.TIME);
    // Interval for sampling
    thumbnailPara.setTime(12);
    // Start time if the sampling type is TIME, which is used together with time. The unit is second. Its value is a number. The default value is 0.
    thumbnailPara.setStartTime(0);
    // Duration if the sampling type is TIME, which is used together with time and start_time. The unit is second. Its value is a digit greater than or equal to 0. The default value is ToEND. ToEND indicates sampling lasts until the end of the video.
    thumbnailPara.setDuration(60);	
    // Datetime array for capturing snapshots. This parameter is used only when the sampling type is DOTS.
    List<Integer> dots = new ArrayList<>();
    dots.add(10);
    dots.add(20);
    thumbnailPara.setDots(dots);
    // Set the snapshot width. Its value ranges from 380 to 3,840. The snapshot height is scaled based on the ratio between the size and the input video pixel.
    thumbnailPara.setMaxLength(480);
    // Set the aspect ratio (min = 0, max = 1).
    thumbnailPara.setAspectRatio(0);
    // Set the snapshot file format. The value 0 indicates the default format, and the value 1 indicates the JPG format.
    thumbnailPara.setFormat(1);
    createThumbnailRequest.setThumbnailPara(thumbnailPara);
    

    Note: A snapshot file is named based on the timestamp. Capture the first and last frames. The middle part is captured by interval. For example, if a video lasts 20s and the snapshot interval is 11s, the generated snapshot files are named as 0.jpg, 11.jpg, and 20.jpg.

  5. Send a request and return a message.
    1
    2
    3
    4
    5
    // Send a request to MPC.
    CreateThumbnailResponse createThumbnailResponse = mpcClient.createThumbnailsTask(createThumbnailRequest);
    
    // Return a message.
    System.out.println(new Gson().toJson(createThumbnailResponse));
    

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import com.google.gson.Gson;
import com.huawei.mpc.client.ClientConfig;
import com.huawei.mpc.client.MpcClient;
import com.huawei.mpc.client.MpcConfig;
import com.huawei.mpc.model.ObsObjInfo;
import com.huawei.mpc.model.thumbnail.CreateThumbnailRequest;
import com.huawei.mpc.model.thumbnail.CreateThumbnailResponse;
import java.util.ArrayList;
import java.util.List;
// Set the method for constructing MPC configuration items.
MpcConfig mpcConfig = new MpcConfig();
// Mandatory. Set the endpoint of MPC. For details, see section "Obtaining Key Parameters."
mpcConfig.setEndPoint(endPoint);
// Mandatory. Set the user's project ID. For details, see section "Obtaining Key Parameters."
mpcConfig.setProjectId(projectId);

// Mandatory. Set the SK. For details, see section "Obtaining Key Parameters."
mpcConfig.setSk(sk);

// Mandatory. Set the AK. For details, see section "Obtaining Key Parameters."
mpcConfig.setAk(ak);
       
/*if you need proxy*/
// Optional. Set the proxy if necessary.
/*
ClientConfig clientConfig = new ClientConfig();
// Set the IP address of the proxy server.
clientConfig.setProxyHost(proxyHost);
// Set the port number of the proxy server.
clientConfig.setProxyPort(Integer.parseInt(proxyPort));
// Set the username for accessing the proxy server.
clientConfig.setProxyUserName(proxyUserName);
// Set the password for accessing the proxy server.
clientConfig.setProxyPassword(proxyPassword);
*/

// Set request parameters.
CreateThumbnailRequest createThumbnailRequest = new CreateThumbnailRequest();

// Set the storage location of an input file.
ObsObjInfo input  = new ObsObjInfo();
// OBS bucket name
input.setBucket("bucketName");
// OBS path
input.setObject("objectKey");
// Region where an OBS bucket is deployed
input.setLocation("cn-north-4");
createThumbnailRequest.setInput(input);

// Set the storage location of an output file.
ObsObjInfo output  = new ObsObjInfo();
output.setBucket("bucketName");
output.setObject("path");
output.setLocation("cn-north-4");
createThumbnailRequest.setOutput(output);

// Set snapshot parameters.
CreateThumbnailRequest.ThumbnailPara thumbnailPara = new CreateThumbnailRequest.ThumbnailPara();
// Sampling type. Three options are available: PERCENT, TIME, and DOTS. PERCENT indicates sampling based on a percentage of the video duration. Time indicates sampling by interval. DOTS indicating sampling based on a datetime array. Now only TIME and DOTS are supported.
thumbnailPara.setTime(12);
// Set the maximum length. Its value ranges from 380 to 3,840.
thumbnailPara.setMaxLength(480);
// Set the aspect ratio (min = 0, max = 1).
thumbnailPara.setAspectRatio(0);
// Set the snapshot file format. The value 0 indicates the default format, and the value 1 indicates the JPG format.
thumbnailPara.setFormat(1);
createThumbnailRequest.setThumbnailPara(thumbnailPara);

// Send a request to MPC.
CreateThumbnailResponse createThumbnailResponse = mpcClient.createThumbnailsTask(createThumbnailRequest);

// Return a message.
System.out.println(new Gson().toJson(createThumbnailResponse));