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

新建转码模板

您可以通过SDK新建转码模板,用于转码时的转码模板设置。具体的模板参数可以参考新建转码模板接口。

核心代码

  1. 设置转码模板的参数。
     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
    //创建转码模板请求
    CreateTransTemplateRequest req = new CreateTransTemplateRequest()
            .withBody(new TransTemplate().withTemplateName("test_123")
                    //设置视频参数
                    .withVideo(new Video()
                            // 视频编码格式,1表示H264,2表示H265
                            .withCodec(1)
                            // 设置视频码率,单位:kbit/s
                            .withBitrate(6000)
                            // 编码档次,建议设为3
                            .withProfile(3)
                            .withLevel(15)
                            // 编码质量, 值越大质量越高,耗时越长
                            .withPreset(3)
                            .withRefFramesCount(4)
                            .withMaxIframesInterval(5)
                            .withBframesCount(4)
                            .withHeight(1080)
                            .withWidth(1920))
                    //设置音频参数
                    .withAudio(new Audio()
                            //设置音频编码格式,1:AAC,2:HEAAC1,3:HEAAC2,4:MP3
                            .withCodec(1)
                            //采样率,1:AUDIO_SAMPLE_AUTO,2:22050Hz,3:32000Hz,4:44100Hz,5:48000Hz,6:96000Hz
                            .withSampleRate(4)
                            //音频码率,单位:kbit/s
                            .withBitrate(128)
                            //声道数
                            .withChannels(2))
                    //设置公共参数
                    .withCommon(new Common()
                            .withDashInterval(5)
                            .withHlsInterval(5)
                            //高清低码开关
                            .withPvc(false)
                            //封装类型,1:HLS,2:DASH,3:HLS+DASH,4:MP4,5:MP3,6:ADTS
                            .withPackType(1)));
    
  2. 发送新建转码模板请求,并显示返回消息
    1
    2
    3
    4
    //发送创建转码模板请求
    CreateTransTemplateResponse rsp = initMpcClient().createTransTemplate(req);
    //打印返回消息
    System.out.println("CreateTransTemplateResponse=" + 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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.Audio;
import com.huaweicloud.sdk.mpc.v1.model.Common;
import com.huaweicloud.sdk.mpc.v1.model.CreateTransTemplateRequest;
import com.huaweicloud.sdk.mpc.v1.model.CreateTransTemplateResponse;
import com.huaweicloud.sdk.mpc.v1.model.TransTemplate;
import com.huaweicloud.sdk.mpc.v1.model.Video;
import com.obs.services.internal.ServiceException;

public class TestTranscodeTemplate {
    /**
     * 初始化 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) {
        //创建转码模板请求
        CreateTransTemplateRequest req = new CreateTransTemplateRequest()
                .withBody(new TransTemplate().withTemplateName("test_123")
                        //设置视频参数
                        .withVideo(new Video()
                                // 视频编码格式,1表示H264,2表示H265
                                .withCodec(1)
                                // 设置视频码率,单位:kbit/s
                                .withBitrate(6000)
                                // 编码档次,建议设为3
                                .withProfile(3)
                                .withLevel(15)
                                // 编码质量, 值越大质量越高,耗时越长
                                .withPreset(3)
                                .withRefFramesCount(4)
                                .withMaxIframesInterval(5)
                                .withBframesCount(4)
                                .withHeight(1080)
                                .withWidth(1920))
                        //设置音频参数
                        .withAudio(new Audio()
                                //设置音频编码格式,1:AAC,2:HEAAC1,3:HEAAC2,4:MP3
                                .withCodec(1)
                                //采样率,1:AUDIO_SAMPLE_AUTO,2:22050Hz,3:32000Hz,4:44100Hz,5:48000Hz,6:96000Hz
                                .withSampleRate(4)
                                //音频码率,单位:kbit/s
                                .withBitrate(128)
                                //声道数
                                .withChannels(2))
                        //设置公共参数
                        .withCommon(new Common()
                                .withDashInterval(5)
                                .withHlsInterval(5)
                                //高清低码开关
                                .withPvc(false)
                                //封装类型,1:HLS,2:DASH,3:HLS+DASH,4:MP4,5:MP3,6:ADTS
                                .withPackType(1)));
        //发送转码模板请求
        try {
            CreateTransTemplateResponse rsp = initMpcClient().createTransTemplate(req);
            System.out.println("CreateTransTemplateResponse=" + JsonUtils.toJSON(rsp));
        } catch (ClientRequestException | ConnectionException | RequestTimeoutException | ServiceException e) {
            System.out.println(e);
        }
    }
}