Updated on 2022-12-07 GMT+08:00

Creating an Animated GIF Task

You can create an MPC client instance and configure related parameters to create an animated GIF task, which is used to convert a video to an animated GIF.

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 an animated GIF task.
    Configure parameters such as the paths of the input and output files, as well as frame rate and width and height of the output file.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    // Set the paths of the input and output files.
    ObsObjInfo input = new ObsObjInfo().withBucket("mpc-east-2").withLocation("cn-east-2").withObject("ok.mp4");
    ObsObjInfo output = new ObsObjInfo().withBucket("mpc-east-2").withLocation("cn-east-2").withObject("output");
    // Create an animated GIF request.
    CreateAnimatedGraphicsTaskRequest req = new CreateAnimatedGraphicsTaskRequest()
            .withBody(new CreateAnimatedGraphicsTaskReq().withInput(input).withOutput(output)
                    .withOutputParam(new AnimatedGraphicsOutputParam()
                            // Set the output image format.
                            .withFormat(AnimatedGraphicsOutputParam.FormatEnum.GIF)
                            // Set the output image frame rate.
                            .withFrameRate(15)
                            // Set the start time, in milliseconds.
                            .withStart(0)
                            // Set the end time, in milliseconds. The maximum interval is 60 seconds.
                            .withEnd(3_000)));
    
    // Send the request.
    CreateAnimatedGraphicsTaskResponse rsp = initMpcClient().createAnimatedGraphicsTask(req);
    // Print the result.
    System.out.println("CreateAnimatedGraphicsTaskResponse=" + JsonUtils.toJSON(rsp)); 
    

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
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.utils.JsonUtils;
import com.huaweicloud.sdk.mpc.v1.MpcClient;
import com.huaweicloud.sdk.mpc.v1.model.AnimatedGraphicsOutputParam;
import com.huaweicloud.sdk.mpc.v1.model.CreateAnimatedGraphicsTaskReq;
import com.huaweicloud.sdk.mpc.v1.model.CreateAnimatedGraphicsTaskRequest;
import com.huaweicloud.sdk.mpc.v1.model.CreateAnimatedGraphicsTaskResponse;
import com.huaweicloud.sdk.mpc.v1.model.ObsObjInfo;
import com.obs.services.internal.ServiceException;

public class TestAnimation {
    /**
     * Initialize the MPC client.
     * @return
     */
    public static MpcClient initMpcClient() {
        HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig().withIgnoreSSLVerification(true).withTimeout(3);
        // Configure the HTTP proxy.
        //httpConfig.withProxyHost("xxxxxx").withProxyPort(xxxxxx).withProxyUsername("xxxxxx").
        //        withProxyPassword("xxxxxx");
        // Enter the AK and SK. To view your AK and SK, choose My CredentialsAccess Keys under your Huawei Cloud account on the management console.
        String ak = "xxxxxx";
        String sk = "xxxxxx";
        // Enter the project ID. To view your project ID, choose My CredentialsAPI Credentials under your Huawei Cloud account on the management console.
        String projectId = "xxxxxx";
        // Enter the endpoint. The following uses cn-east-2 as an example.
        String endpoint = "https://mpc.cn-east-2.myhuaweicloud.com";
        BasicCredentials auth = new BasicCredentials().withAk(ak).withSk(sk).withProjectId(projectId);
        return MpcClient.newBuilder()
                .withHttpConfig(httpConfig)
                .withCredential(auth)
                .withEndpoint(endpoint)
                .build();
    }

    /**
     * Create an animated GIF task.
     * @param args
     */
    public static void main(String[] args) {
        // Set the paths of the input and output files.
        ObsObjInfo input = new ObsObjInfo().withBucket("mpc-east-2").withLocation("cn-east-2").withObject("ok.mp4");
        ObsObjInfo output = new ObsObjInfo().withBucket("mpc-east-2").withLocation("cn-east-2").withObject("output");
        // Create an animated GIF request.
        CreateAnimatedGraphicsTaskRequest req = new CreateAnimatedGraphicsTaskRequest()
                .withBody(new CreateAnimatedGraphicsTaskReq().withInput(input).withOutput(output)
                        .withOutputParam(new AnimatedGraphicsOutputParam()
                                // Set the output image format.
                                .withFormat(AnimatedGraphicsOutputParam.FormatEnum.GIF)
                                // Set the output image frame rate.
                                .withFrameRate(15)
                                // Set the start time, in milliseconds.
                                .withStart(0)
                                // Set the end time, in milliseconds. The maximum interval is 60 seconds.
                                .withEnd(3_000)));
        try {
            CreateAnimatedGraphicsTaskResponse rsp = initMpcClient().createAnimatedGraphicsTask(req);
            System.out.println("CreateAnimatedGraphicsTaskResponse=" + JsonUtils.toJSON(rsp));
        } catch (ClientRequestException | ConnectionException | RequestTimeoutException | ServiceException e) {
            System.out.println(e);
        }
    }
}