全部产品

视频水印

本篇文档提供了Java SDK视频水印模块相关功能的API调用示例。包含添加水印、修改水印、删除水印、查询水印等。

初始化客户端

使用前请先初始化客户端,请参见初始化

添加水印

接口参数和返回字段请参见AddWatermark

import com.aliyuncs.vod.model.v20170321.AddWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.AddWatermarkResponse;


/**
 * 添加水印配置信息函数
 */
public static AddWatermarkResponse addWatermark(DefaultAcsClient client) throws Exception {
    AddWatermarkRequest request = new AddWatermarkRequest();
    //水印名称
    request.setName("addwatermark");
    //获取水印文件在oss的URL
    String fileUrl = "http://test-bucket.oss-cn-shanghai.aliyuncs.com/watermark/test.png";
    //图片水印必传图片文件的oss文件地址,水印文件必须和视频在同一个区域,例如:华东2视频,水印文件必须存放在华东2
    request.setFileUrl(fileUrl);
    //水印配置数据
    JSONObject watermarkConfig = null;
    //图片水印的位置配置数据
    watermarkConfig = buildImageWatermarkConfig();

    //文字水印的位置配置数据
    //watermarkConfig = buildTextWatermarkConfig();
    request.setWatermarkConfig(watermarkConfig.toJSONString());

    //文字水印:Text; 图片水印:Image
    request.setType("Image");
    return client.getAcsResponse(request);
}


/**
 * 以下为调用示例
 * @param args
 * @throws ClientException
 */
public static void main(String[] args) throws ClientException {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    AddWatermarkResponse response = new AddWatermarkResponse();
    try {
        //添加水印信息
        response = addWatermark(client);
        //水印ID
        System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
        //水印的位置、效果配置数据
        System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
        //水印文件的FileUrl(文字水印该字段值为空)
        System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

/**
 * 构建图片水印的配置数据,根据具体设置需求修改对应的参数值
 * @return
 */
public static JSONObject buildImageWatermarkConfig() {
    JSONObject watermarkConfig = new JSONObject();
    //水印的横向偏移距离
    watermarkConfig.put("Dx", "8");
    //水印的纵向偏移距离
    watermarkConfig.put("Dy", "8");
    //水印显示的宽
    watermarkConfig.put("Width", "55");
    //水印显示的高
    watermarkConfig.put("Height", "55");
    //水印显示的相对位置(左上、右上、左下、右下)
    watermarkConfig.put("ReferPos", "BottomRight");
    //水印显示的时间线(开始显示和结束显示时间)
    JSONObject timeline = new JSONObject();
    //水印开始显示时间
    timeline.put("Start", "2");
    //水印结束显示时间
    timeline.put("Duration", "ToEND");
    watermarkConfig.put("Timeline", timeline);
    return watermarkConfig;
}

/**
 * 构建文字水印的配置数据,根据具体设置需求修改对应的参数值
 * @return
 */
public static JSONObject buildTextWatermarkConfig() {
    JSONObject watermarkConfig = new JSONObject();
    //文字水印显示的内容
    watermarkConfig.put("Content", "testwatermark");
    //文字水印的字体名称
    watermarkConfig.put("FontName", "SimSun");
    //文字水印的字体大小
    watermarkConfig.put("FontSize", 25);
    //文字水印的颜色(也可为RGB颜色取值,例如:#000000)
    watermarkConfig.put("FontColor", "Black");
    //文字水印的透明度
    watermarkConfig.put("FontAlpha", "0.2");
    //文字水印的字体描边颜色(也可为RGB颜色取值,例如:#ffffff)
    watermarkConfig.put("BorderColor", "White");
    //文字水印的描边宽度
    watermarkConfig.put("BorderWidth", 1);
    //文字水印距离视频画面上边的偏移距离
    watermarkConfig.put("Top", 20);
    //文字水印距离视频画面左边的偏移距离
    watermarkConfig.put("Left", 15);
    return watermarkConfig;
}

修改水印

调用UpdateWatermark接口,完成修改水印功能。

接口参数和返回字段请参见UpdateWatermark。调用示例如下:

import com.aliyuncs.vod.model.v20170321.UpdateWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.UpdateWatermarkResponse;

/**
 * 修改水印配置信息函数
 * 注意:不支持图片文件地址的修改,如果更换请创建新的水印信息
 */
public static UpdateWatermarkResponse updateWatermark(DefaultAcsClient client) throws Exception {
    UpdateWatermarkRequest request = new UpdateWatermarkRequest();
    request.setName("updatewatermark");
    //需要更新配置信息的水印ID
    request.setWatermarkId("421ddddd4f6e734a526fd2****");
    //水印配置数据
    JSONObject watermarkConfig = null;
    //图片水印的位置配置数据
    //watermarkConfig = buildImageWatermarkConfig();
    //文字水印的位置配置数据
    watermarkConfig = buildTextWatermarkConfig();
    request.setWatermarkConfig(watermarkConfig.toJSONString());
    return client.getAcsResponse(request);
}

/**
 * 以下为调用示例
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    UpdateWatermarkResponse response = new UpdateWatermarkResponse();
    try {
        response = updateWatermark(client);
        //水印ID
        System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
        //水印的位置、效果配置数据
        System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
        //水印文件的FileUrl(文字水印该字段值为空)
        System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

/**
 * 构建图片水印的配置数据,根据具体设置需求修改对应的参数值
 * @return
 */
public static JSONObject buildImageWatermarkConfig() {
    JSONObject watermarkConfig = new JSONObject();
    //水印的横向偏移距离
    watermarkConfig.put("Dx", "10");
    //水印的纵向偏移距离
    watermarkConfig.put("Dy", "10");
    //水印显示的宽
    watermarkConfig.put("Width", "66");
    //水印显示的高
    watermarkConfig.put("Height", "66");
    //水印显示的相对位置(左上、右上、左下、右下)
    watermarkConfig.put("ReferPos", "BottomLeft");
    //水印显示的时间线(开始显示和结束显示时间)
    JSONObject timeline = new JSONObject();
    //水印开始显示时间
    timeline.put("Start", "2");
    //水印结束显示时间
    timeline.put("Duration", "ToEND");
    watermarkConfig.put("Timeline", timeline);
    return watermarkConfig;
}

/**
 * 构建文字水印的配置数据,根据具体设置需求修改对应的参数值
 * @return
 */
public static JSONObject buildTextWatermarkConfig() {
    JSONObject watermarkConfig = new JSONObject();
    //文字水印显示的内容
    watermarkConfig.put("Content", "testwatermark");
    //文字水印的字体名称
    watermarkConfig.put("FontName", "SimSun");
    //文字水印的字体大小
    watermarkConfig.put("FontSize", 40);
    //文字水印的颜色(也可为RGB颜色取值,例如:#000000)
    watermarkConfig.put("FontColor", "Black");
    //文字水印的透明度
    watermarkConfig.put("FontAlpha", "0.2");
    //文字水印的字体描边颜色(也可为RGB颜色取值,例如:#ffffff)
    watermarkConfig.put("BorderColor", "White");
    //文字水印的描边宽度
    watermarkConfig.put("BorderWidth", 2);
    //文字水印距离视频画面上边的偏移距离
    watermarkConfig.put("Top", 20);
    //文字水印距离视频画面左边的偏移距离
    watermarkConfig.put("Left", 15);
    return watermarkConfig;
}

删除水印

调用DeleteWatermark接口,完成删除水印功能。

接口参数和返回字段请参见DeleteWatermark。调用示例如下:

import com.aliyuncs.vod.model.v20170321.DeleteWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.DeleteWatermarkResponse;

/**
 *删除水印配置信息函数
 */
public static DeleteWatermarkResponse deleteWatermark(DefaultAcsClient client) throws Exception {
    DeleteWatermarkRequest request = new DeleteWatermarkRequest();
    request.setWatermarkId("53f9d796fad9d7b862b2e****");
    return client.getAcsResponse(request);
}

/**
 * 以下为调用示例
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    DeleteWatermarkResponse response = new DeleteWatermarkResponse();
    try {
        response = deleteWatermark(client);
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

查询水印列表

调用ListWatermark接口,完成查询水印列表功能。

接口参数和返回字段请参见ListWatermark。调用示例如下:

import com.aliyuncs.vod.model.v20170321.ListWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.ListWatermarkResponse;

/**
 * 查询水印配置信息列表函数
 */
public static ListWatermarkResponse listWatermark(DefaultAcsClient client) throws Exception {
    ListWatermarkRequest request = new ListWatermarkRequest();
    return client.getAcsResponse(request);
}

/**
 * 以下为调用示例
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    ListWatermarkResponse response = new ListWatermarkResponse();
    try {
        response = listWatermark(client);
        for (ListWatermarkResponse.WatermarkInfo watermarkInfo : response.getWatermarkInfos()) {
            //水印ID
            System.out.println("WatermarkId = " + watermarkInfo.getWatermarkId());
            //水印的位置、效果配置数据
            System.out.println("WatermarkConfig = " + watermarkInfo.getWatermarkConfig());
            //水印文件的FileUrl(文字水印该字段值为空)
            System.out.println("FileUrl = " + watermarkInfo.getFileUrl());
        }
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

查询单个水印

调用GetWatermark接口,完成查询单个水印功能。

接口参数和返回字段请参见GetWatermark。调用示例如下:

import com.aliyuncs.vod.model.v20170321.GetWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.GetWatermarkResponse;

/**
 * 查询单个水印配置信息函数
 */
public static GetWatermarkResponse getWatermark(DefaultAcsClient client) throws Exception {
    GetWatermarkRequest request = new GetWatermarkRequest();
    //需要查询水印信息的水印ID
    request.setWatermarkId("96d4f6e734a526fd2****");
    return client.getAcsResponse(request);
}

/**
 * 以下为调用示例
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    GetWatermarkResponse response = new GetWatermarkResponse();
    try {
        response = getWatermark(client);
        //水印ID
        System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
        //水印的位置、效果配置数据
        System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
        //水印文件的FileUrl(文字水印该字段值为空)
        System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}

设置默认水印

调用SetDefaultWatermark接口,完成设置默认水印功能。

接口参数和返回字段请参见SetDefaultWatermark。调用示例如下:

import com.aliyuncs.vod.model.v20170321.SetDefaultWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.SetDefaultWatermarkResponse;

/**
 * 设置默认水印配置信息函数
 */
public static SetDefaultWatermarkResponse setDefaultWatermark(DefaultAcsClient client) throws Exception {
    SetDefaultWatermarkRequest request = new SetDefaultWatermarkRequest();
    //设置默认的水印ID
    request.setWatermarkId("82105a29c6e96d4f6****");
    return client.getAcsResponse(request);
}

/**
 * 以下为调用示例
 */
public static void main(String[] args) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    SetDefaultWatermarkResponse response = new SetDefaultWatermarkResponse();
    try {
        response = setDefaultWatermark(client);
    } catch (Exception e) {
        System.out.println("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.println("RequestId = " + response.getRequestId());
}