更新时间:2024-07-29 GMT+08:00
分享

新建转码任务

您可以通过创建转码MpcClient实例并设置相关参数新建转码任务。

核心代码

  1. 创建转码MpcClient实例
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public static MpcClient initMpcClient() {
        //设置httpConfig
        HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig().withIgnoreSSLVerification(true).withTimeout(3);
        //根据实际需要,是否设置http代理
        //httpConfig.withProxyHost("xxxxxx").withProxyPort(xxxxxx).withProxyUsername("xxxxxx").
                //withProxyPassword("xxxxxx");
        //根据实际填写ak,sk,在控制台账号名下“我的凭证 > 访问密钥”上创建和查看您的AK/SK
        
        String ak = System.getenv("SDK_AK");
        String sk = System.getenv("SDK_SK");
        //根据实际填写项目ID,在控制台账号名下“我的凭证 > API凭证”下查看您的项目ID
        String projectId = System.getenv("PROJECT_ID");
        //根据实际填写所需Endpoint,这里以“region01”为例
        String endpoint = "https://mpc.region01.myhuaweicloud.com";
        BasicCredentials auth = new BasicCredentials().withAk(ak).withSk(sk).withProjectId(projectId);
        return MpcClient.newBuilder()
                .withHttpConfig(httpConfig)
                .withCredential(auth)
                .withEndpoint(endpoint)
                .build();
    }
    
  2. 创建转码请求,并设置请求体
    转码请求包括输入文件、输出文件和转码模板设置。具体参数含义请参考新建转码任务
     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
    //设置转码输入视频地址
    ObsObjInfo input = new ObsObjInfo()
        //设置桶名
        .withBucket("mpc-east-2")
        //设置OBS桶所在区域
        .withLocation("region01")
        //设置输入视频对象
        .withObject("input/ok.mp4");
    //设置转码输出视频路径
    ObsObjInfo output = new ObsObjInfo().withBucket("mpc-east-2").withLocation("region01")
            //设置输出路径
            .withObject("output");
    //创建转码请求
    CreateTranscodingTaskRequest request
            = new CreateTranscodingTaskRequest().withBody(new CreateTranscodingReq()
            .withInput(input)
            .withOutput(output)
            //设置转码模板,预置模板Id可以在MPC console页面“全局设置 > 预置模板”上查看
            .withTransTemplateId(Collections.singletonList(7000530))
            //设置输出名称,名称个数需要与模板个数一一对应
            .withOutputFilenames(Collections.singletonList("output_"))
            //设置截图参数,根据实际需要填充Thumbnail结构
            //.withThumbnail(new Thumbnail())
            //设置加密参数,根据实际需要填充Encryption结构
            //.withEncryption(new Encryption())
    );
    
  3. 发送转码请求。
    1
    2
    3
    4
    //发送媒体处理服务请求
    CreateTranscodingTaskResponse response = initMpcClient().createTranscodingTask(request);
    //返回消息
    System.out.println("CreateTranscodingTaskResponse=" + response);
    

示例代码

 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
74
75
import com.huaweicloud.sdk.core.auth.BasicCredentials;
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.CreateTranscodingReq;
import com.huaweicloud.sdk.mpc.v1.model.CreateTranscodingTaskRequest;
import com.huaweicloud.sdk.mpc.v1.model.CreateTranscodingTaskResponse;
import com.huaweicloud.sdk.mpc.v1.model.ListTranscodingTaskRequest;
import com.huaweicloud.sdk.mpc.v1.model.ListTranscodingTaskResponse;
import com.huaweicloud.sdk.mpc.v1.model.ObsObjInfo;
import com.obs.services.internal.ServiceException;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

public class TestTranscode {
    /**
     * 初始化 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 = System.getenv("SDK_AK");
        String sk = System.getenv("SDK_SK");
        //根据实际填写项目ID,在控制台账号名下“我的凭证 > API凭证”下查看您的项目ID
        String projectId = System.getenv("PROJECT_ID");
        //根据实际填写所需endpoint,这里以“region01”为例
        String endpoint = "https://mpc.region01.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("region01").withObject("ok.mp4");
        //设置转码输出视频路径
        ObsObjInfo output = new ObsObjInfo().withBucket("mpc-east-2").withLocation("region01").withObject("output");
        //创建转码请求
        CreateTranscodingTaskRequest request
                = new CreateTranscodingTaskRequest().withBody(new CreateTranscodingReq()
                .withInput(input)
                .withOutput(output)
                //设置转码模板,预置模板Id可以在MPC console页面“全局设置 > 预置模板”上查看
                .withTransTemplateId(Collections.singletonList(7000530))
                //设置输出名称,名称个数需要与模板个数一一对应
                .withOutputFilenames(Collections.singletonList("output_"))
                //设置截图参数
                //.withThumbnail(new Thumbnail())
                //设置加密参数
                //.withEncryption(new Encryption())
        );
        try {
            CreateTranscodingTaskResponse response = initMpcClient().createTranscodingTask(request);
            System.out.println("CreateTranscodingTaskResponse=" + response);
        } catch (ClientRequestException | ConnectionException | RequestTimeoutException | ServiceException e) {
            System.out.println(e);
        }
    }
}

相关文档