Creating an Animated GIF Task
You can create an animated GIF task by creating an MpcClient instance and setting related parameters. The task is used to convert a video to 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
- 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.
- 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);
- Create an animated GIF task.
Set the parameters such as the input video file, output file path, output image frame rate, and output image width and height.
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
CreateAnimatedGraphicsTaskReq req = new CreateAnimatedGraphicsTaskReq(); // Set the input file path. ObsObjInfo input = new ObsObjInfo(); input.setBucket("obs-test01"); input.setLocation("cn-north-4"); input.setObject("volume_auto_test.mp4"); req.setInput(input); // Set the output file path. ObsObjInfo output = new ObsObjInfo(); output.setBucket("obs-test01"); output.setLocation("cn-north-4"); output.setObject("/e7404a3fa1b547919d659b0cbfa268c1/animate-sdk-test/"); output.setFileName("my-test.gif"); req.setOutput(output); // Set output parameters. AnimatedGraphicsOutputParam outputParam = new AnimatedGraphicsOutputParam(); outputParam.setStart(1_000); outputParam.setEnd(5_000); outputParam.setFormat(AnimatedGraphicsOutputParam.FormatEnum.GIF); outputParam.setFrameRate(15); outputParam.setWidth(1024); outputParam.setHeight(768); req.setOutputParam(outputParam); // Send a request. CommonCreateTaskRsp rsp = mpcClient.createAnimatedGraphicsTask(req); // Print the result. System.out.println(gson.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 |
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.animated.AnimatedGraphicsOutputParam;
import com.huawei.mpc.model.animated.CreateAnimatedGraphicsTaskReq;
Gson gson = new Gson();
// Initialize the client.
MpcConfig mpcConfig = new MpcConfig();
mpcConfig.setEndPoint("mpc.cn-north-4.myhuaweicloud.com:443");
mpcConfig.setProjectId("Your project ID");
mpcConfig.setAk("*************");
mpcConfig.setSk("*****************************");
MpcClient mpcClient = new MpcClient(mpcConfig);
CreateAnimatedGraphicsTaskReq req = new CreateAnimatedGraphicsTaskReq();
// Set the input file path.
ObsObjInfo input = new ObsObjInfo();
input.setBucket("obs-test01");
input.setLocation("cn-north-4");
input.setObject("volume_auto_test.mp4");
req.setInput(input);
// Set the output file path.
ObsObjInfo output = new ObsObjInfo();
output.setBucket("obs-test01");
output.setLocation("cn-north-4");
output.setObject("/e7404a3fa1b547919d659b0cbfa268c1/animate-sdk-test/");
output.setBucket("my-test.gif");
req.setOutput(output);
// Set output parameters.
AnimatedGraphicsOutputParam outputParam = new AnimatedGraphicsOutputParam();
outputParam.setStart(1_000);
outputParam.setEnd(5_000);
outputParam.setFormat(AnimatedGraphicsOutputParam.FormatEnum.GIF);
outputParam.setFrameRate(15);
outputParam.setWidth(1024);
outputParam.setHeight(768);
req.setOutputParam(outputParam);
// Send a request.
CommonCreateTaskRsp rsp = mpcClient.createAnimatedGraphicsTask(req);
// Print the result.
System.out.println(gson.toJson(rsp));
|
Last Article: Animated GIF Management
Next Article: Querying Animated GIF Tasks
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.