更新时间:2024-01-31 GMT+08:00
分享

新建水印模板

水印模板可使转码后的视频自带水印,具体使用方式可以参考新建水印模板

核心代码

  1. 设置水印模板的参数。
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    //创建水印模板请求
    CreateWatermarkTemplateRequest req = new CreateWatermarkTemplateRequest()
            .withBody(new WatermarkTemplate()
                    //设置模板名称
                    .withTemplateName("watermark_name")
                    //设置模板类型
                    .withType("Image")
                    //设置图片水印处理方式
                    .withImageProcess("Grayed")
                    //水印宽度
                    .withWidth("1920")
                    //水印高度
                    .withHeight("1080")
                    //水印相对视频顶点水平偏移位置
                    .withDx("10")
                    //水印相对视频顶点垂直偏移位置
                    .withDy("10")
                    //水印的位置
                    //.withReferpos("BottomLeft")
                    //水印开始时间,与timeline_duration配合使用
                    .withTimelineStart("6")
                    //水印持续时间,默认值“ToEND”,表示持续到视频结束
                    .withTimelineDuration("8"));
    
  2. 发送新建水印模板请求,并显示返回消息
    1
    2
    3
    4
    // 发送新建水印模板请求给媒体处理服务
    CreateWatermarkTemplateResponse rsp = initMpcClient().createWatermarkTemplate(req);
    // 打印返回消息
    System.out.println("CreateWatermarkTemplateResponse=" + 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
import com.huaweicloud.sdk.core.auth.BasicCredentials;
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.CreateWatermarkTemplateRequest;
import com.huaweicloud.sdk.mpc.v1.model.CreateWatermarkTemplateResponse;
import com.huaweicloud.sdk.mpc.v1.model.WatermarkTemplate;

public class TestWatermarkTemplate {
    /**
     * 初始化 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) {
        //创建水印模板请求
        CreateWatermarkTemplateRequest req = new CreateWatermarkTemplateRequest().withBody(new WatermarkTemplate()
                        //设置模板名称
                        .withTemplateName("watermark_name")
                        //设置模板类型
                        .withType("Image")
                        //设置图片水印处理方式
                        .withImageProcess("Grayed")
                        //水印宽度
                        .withWidth("1920")
                        //水印高度
                        .withHeight("1080")
                        //水印相对视频顶点水平偏移位置
                        .withDx("10")
                        //水印相对视频顶点垂直偏移位置
                        .withDy("10")
                        //水印的位置
                        //.withReferpos("BottomLeft")
                        //水印开始时间,与timeline_duration配合使用
                        .withTimelineStart("6")
                        //水印持续时间,默认值“ToEND”,表示持续到视频结束
                        .withTimelineDuration("ToEND"));
        //发送水印模板请求
        try {
            CreateWatermarkTemplateResponse rsp = initMpcClient().createWatermarkTemplate(req);
            System.out.println("CreateWatermarkTemplateResponse=" + JsonUtils.toJSON(rsp));
        } catch (ClientRequestException | ConnectionException | RequestTimeoutException | ServiceException e) {
            System.out.println(e);
        }
    }
}
分享:

    相关文档

    相关产品