更新时间:2021-06-28 GMT+08:00
分享

新增动图任务

您可以通过创建MpcClient实例并设置相关参数新建动图任务,动图任务用于将视频转换为动态图。

前提条件

  • 已购买对象存储服务,并参考上传媒体文件在媒体处理服务同区域(如华北-北京四)上传媒体处理的源视频。
  • 已参考获取云资源授权,完成媒体处理服务授权。

核心代码

  1. 创建动图任务
    动图任务需要设置输入视频文件、输出动图路径、动图帧率、动图宽高等参数。
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    //设置输入视频地址和输出路径
    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");
    //创建动图请求
    CreateAnimatedGraphicsTaskRequest req = new CreateAnimatedGraphicsTaskRequest()
            .withBody(new CreateAnimatedGraphicsTaskReq().withInput(input).withOutput(output)
                    .withOutputParam(new AnimatedGraphicsOutputParam()
                            //设置动图格式
                            .withFormat(AnimatedGraphicsOutputParam.FormatEnum.GIF)
                            //设置动图帧率
                            .withFrameRate(15)
                            //设置起始时间,单位毫秒
                            .withStart(0)
                            //设置结束时间,单位毫秒,最大时间间隔60s
                            .withEnd(3_000)));
    
    // 发起请求
    CreateAnimatedGraphicsTaskResponse rsp = initMpcClient().createAnimatedGraphicsTask(req);
    // 打印结果
    System.out.println("CreateAnimatedGraphicsTaskResponse=" + JsonUtils.toJSON(rsp)); 
    

完整代码

 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 {
    /**
     * 初始化 MpcClient
     * @return
     */
    public static MpcClient initMpcClient() {
        HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig().withIgnoreSSLVerification(true).withTimeout(3);
        //http代理设置,请根据实际情况设置
        //httpConfig.withProxyHost("xxxxxx").withProxyPort(xxxxxx).withProxyUsername("xxxxxx").
        //        withProxyPassword("xxxxxx");
        //根据实际填写ak,sk,华为云控制台账号名下“我的凭证”>访问密钥上创建和查看您的AK/SK
        String ak = "xxxxxx";
        String sk = "xxxxxx";
        //根据实际填写项目ID,华为云控制台账号名下“我的凭证”>API凭证下查看您的项目ID
        String projectId = "xxxxxx";
        //根据实际填写所需endpoint,这里以华东2为例
        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();
    }

    /**
     * 创建动图任务
     * @param args
     */
    public static void main(String[] args) {
        //设置输入视频地址和输出路径
        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");
        //创建动图请求
        CreateAnimatedGraphicsTaskRequest req = new CreateAnimatedGraphicsTaskRequest()
                .withBody(new CreateAnimatedGraphicsTaskReq().withInput(input).withOutput(output)
                        .withOutputParam(new AnimatedGraphicsOutputParam()
                                //设置动图格式
                                .withFormat(AnimatedGraphicsOutputParam.FormatEnum.GIF)
                                //设置动图帧率
                                .withFrameRate(15)
                                //设置起始时间,单位毫秒
                                .withStart(0)
                                //设置结束时间,单位毫秒,最大时间间隔60s
                                .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);
        }
    }
}
分享:

    相关文档

    相关产品

close