全部产品
云市场

视频水印

更新时间:2018-12-27 14:58:43

初始化客户端

使用前请先初始化客户端,参考 initVodClient 接口。

添加水印

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

  1. import com.aliyuncs.vod.model.v20170321.AddWatermarkRequest;
  2. import com.aliyuncs.vod.model.v20170321.AddWatermarkResponse;
  3. /**
  4. * 添加水印配置信息函数
  5. */
  6. public static AddWatermarkResponse addWatermark(DefaultAcsClient client) throws Exception {
  7. AddWatermarkRequest request = new AddWatermarkRequest();
  8. //水印名称
  9. request.setName("addwatermark");
  10. //获取水印文件在oss的URL
  11. String fileUrl = "http://test-bucket.oss-cn-shanghai.aliyuncs.com/watermark/test.png";
  12. //图片水印必传图片文件的oss文件地址,水印文件必须和视频在同一个区域,例如:华东2视频,水印文件必须存放在华东2
  13. request.setFileUrl(fileUrl);
  14. //水印配置数据
  15. JSONObject watermarkConfig = null;
  16. //图片水印的位置配置数据
  17. watermarkConfig = buildImageWatermarkConfig();
  18. //文字水印的位置配置数据
  19. //watermarkConfig = buildTextWatermarkConfig();
  20. request.setWatermarkConfig(watermarkConfig.toJSONString());
  21. //文字水印:Text; 图片水印:Image
  22. request.setType("Image");
  23. return client.getAcsResponse(request);
  24. }
  25. /**
  26. * 以下为调用示例
  27. * @param args
  28. * @throws ClientException
  29. */
  30. public static void main(String[] args) throws ClientException {
  31. DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
  32. AddWatermarkResponse response = new AddWatermarkResponse();
  33. try {
  34. //添加水印信息
  35. response = addWatermark(client);
  36. //水印ID
  37. System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
  38. //水印的位置、效果配置数据
  39. System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
  40. //水印文件的FileUrl(文字水印该字段值为空)
  41. System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
  42. } catch (Exception e) {
  43. System.out.println("ErrorMessage = " + e.getLocalizedMessage());
  44. }
  45. System.out.println("RequestId = " + response.getRequestId());
  46. }
  47. /**
  48. * 构建图片水印的配置数据,根据具体设置需求修改对应的参数值
  49. * @return
  50. */
  51. public static JSONObject buildImageWatermarkConfig() {
  52. JSONObject watermarkConfig = new JSONObject();
  53. //水印的横向偏移距离
  54. watermarkConfig.put("Dx", "8");
  55. //水印的纵向偏移距离
  56. watermarkConfig.put("Dy", "8");
  57. //水印显示的宽
  58. watermarkConfig.put("Width", "55");
  59. //水印显示的高
  60. watermarkConfig.put("Height", "55");
  61. //水印显示的相对位置(左上、右上、左下、右下)
  62. watermarkConfig.put("ReferPos", "BottomRight");
  63. //水印显示的时间线(开始显示和结束显示时间)
  64. JSONObject timeline = new JSONObject();
  65. //水印开始显示时间
  66. timeline.put("Start", "2");
  67. //水印结束显示时间
  68. timeline.put("Duration", "ToEND");
  69. watermarkConfig.put("Timeline", timeline);
  70. return watermarkConfig;
  71. }
  72. /**
  73. * 构建文字水印的配置数据,根据具体设置需求修改对应的参数值
  74. * @return
  75. */
  76. public static JSONObject buildTextWatermarkConfig() {
  77. JSONObject watermarkConfig = new JSONObject();
  78. //文字水印显示的内容
  79. watermarkConfig.put("Content", "testwatermark");
  80. //文字水印的字体名称
  81. watermarkConfig.put("FontName", "SimSun");
  82. //文字水印的字体大小
  83. watermarkConfig.put("FontSize", 25);
  84. //文字水印的颜色(也可为RGB颜色取值,例如:#000000)
  85. watermarkConfig.put("FontColor", "Black");
  86. //文字水印的透明度
  87. watermarkConfig.put("FontAlpha", "0.2");
  88. //文字水印的字体描边颜色(也可为RGB颜色取值,例如:#ffffff)
  89. watermarkConfig.put("BorderColor", "White");
  90. //文字水印的描边宽度
  91. watermarkConfig.put("BorderWidth", 1);
  92. //文字水印距离视频画面上边的偏移距离
  93. watermarkConfig.put("Top", 20);
  94. //文字水印距离视频画面左边的偏移距离
  95. watermarkConfig.put("Left", 15);
  96. return watermarkConfig;
  97. }

修改水印

接口参数和返回字段请参考 UpdateWatermark

  1. import com.aliyuncs.vod.model.v20170321.UpdateWatermarkRequest;
  2. import com.aliyuncs.vod.model.v20170321.UpdateWatermarkResponse;
  3. /**
  4. * 修改水印配置信息函数
  5. * 注意:不支持图片文件地址的修改,如果更换请创建新的水印信息
  6. */
  7. public static UpdateWatermarkResponse updateWatermark(DefaultAcsClient client) throws Exception {
  8. UpdateWatermarkRequest request = new UpdateWatermarkRequest();
  9. request.setName("updatewatermark");
  10. //需要更新配置信息的水印ID
  11. request.setWatermarkId("421ddddd4f6e734a526fd2e442");
  12. //水印配置数据
  13. JSONObject watermarkConfig = null;
  14. //图片水印的位置配置数据
  15. //watermarkConfig = buildImageWatermarkConfig();
  16. //文字水印的位置配置数据
  17. watermarkConfig = buildTextWatermarkConfig();
  18. request.setWatermarkConfig(watermarkConfig.toJSONString());
  19. return client.getAcsResponse(request);
  20. }
  21. /**
  22. * 以下为调用示例
  23. */
  24. public static void main(String[] args) {
  25. DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
  26. UpdateWatermarkResponse response = new UpdateWatermarkResponse();
  27. try {
  28. response = updateWatermark(client);
  29. //水印ID
  30. System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
  31. //水印的位置、效果配置数据
  32. System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
  33. //水印文件的FileUrl(文字水印该字段值为空)
  34. System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
  35. } catch (Exception e) {
  36. System.out.println("ErrorMessage = " + e.getLocalizedMessage());
  37. }
  38. System.out.println("RequestId = " + response.getRequestId());
  39. }
  40. /**
  41. * 构建图片水印的配置数据,根据具体设置需求修改对应的参数值
  42. * @return
  43. */
  44. public static JSONObject buildImageWatermarkConfig() {
  45. JSONObject watermarkConfig = new JSONObject();
  46. //水印的横向偏移距离
  47. watermarkConfig.put("Dx", "10");
  48. //水印的纵向偏移距离
  49. watermarkConfig.put("Dy", "10");
  50. //水印显示的宽
  51. watermarkConfig.put("Width", "66");
  52. //水印显示的高
  53. watermarkConfig.put("Height", "66");
  54. //水印显示的相对位置(左上、右上、左下、右下)
  55. watermarkConfig.put("ReferPos", "BottomLeft");
  56. //水印显示的时间线(开始显示和结束显示时间)
  57. JSONObject timeline = new JSONObject();
  58. //水印开始显示时间
  59. timeline.put("Start", "2");
  60. //水印结束显示时间
  61. timeline.put("Duration", "ToEND");
  62. watermarkConfig.put("Timeline", timeline);
  63. return watermarkConfig;
  64. }
  65. /**
  66. * 构建文字水印的配置数据,根据具体设置需求修改对应的参数值
  67. * @return
  68. */
  69. public static JSONObject buildTextWatermarkConfig() {
  70. JSONObject watermarkConfig = new JSONObject();
  71. //文字水印显示的内容
  72. watermarkConfig.put("Content", "testwatermark");
  73. //文字水印的字体名称
  74. watermarkConfig.put("FontName", "SimSun");
  75. //文字水印的字体大小
  76. watermarkConfig.put("FontSize", 40);
  77. //文字水印的颜色(也可为RGB颜色取值,例如:#000000)
  78. watermarkConfig.put("FontColor", "Black");
  79. //文字水印的透明度
  80. watermarkConfig.put("FontAlpha", "0.2");
  81. //文字水印的字体描边颜色(也可为RGB颜色取值,例如:#ffffff)
  82. watermarkConfig.put("BorderColor", "White");
  83. //文字水印的描边宽度
  84. watermarkConfig.put("BorderWidth", 2);
  85. //文字水印距离视频画面上边的偏移距离
  86. watermarkConfig.put("Top", 20);
  87. //文字水印距离视频画面左边的偏移距离
  88. watermarkConfig.put("Left", 15);
  89. return watermarkConfig;
  90. }

删除水印

接口参数和返回字段请参考 DeleteWatermark

  1. import com.aliyuncs.vod.model.v20170321.DeleteWatermarkRequest;
  2. import com.aliyuncs.vod.model.v20170321.DeleteWatermarkResponse;
  3. /**
  4. *删除水印配置信息函数
  5. */
  6. public static DeleteWatermarkResponse deleteWatermark(DefaultAcsClient client) throws Exception {
  7. DeleteWatermarkRequest request = new DeleteWatermarkRequest();
  8. request.setWatermarkId("53f9d796fad9d7b862b2e5e5b");
  9. return client.getAcsResponse(request);
  10. }
  11. /**
  12. * 以下为调用示例
  13. */
  14. public static void main(String[] args) {
  15. DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
  16. DeleteWatermarkResponse response = new DeleteWatermarkResponse();
  17. try {
  18. response = deleteWatermark(client);
  19. } catch (Exception e) {
  20. System.out.println("ErrorMessage = " + e.getLocalizedMessage());
  21. }
  22. System.out.println("RequestId = " + response.getRequestId());
  23. }

查询水印列表

接口参数和返回字段请参考 ListWatermark

  1. import com.aliyuncs.vod.model.v20170321.ListWatermarkRequest;
  2. import com.aliyuncs.vod.model.v20170321.ListWatermarkResponse;
  3. /**
  4. * 查询水印配置信息列表函数
  5. */
  6. public static ListWatermarkResponse listWatermark(DefaultAcsClient client) throws Exception {
  7. ListWatermarkRequest request = new ListWatermarkRequest();
  8. return client.getAcsResponse(request);
  9. }
  10. /**
  11. * 以下为调用示例
  12. */
  13. public static void main(String[] args) {
  14. DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
  15. ListWatermarkResponse response = new ListWatermarkResponse();
  16. try {
  17. response = listWatermark(client);
  18. for (ListWatermarkResponse.WatermarkInfo watermarkInfo : response.getWatermarkInfos()) {
  19. //水印ID
  20. System.out.println("WatermarkId = " + watermarkInfo.getWatermarkId());
  21. //水印的位置、效果配置数据
  22. System.out.println("WatermarkConfig = " + watermarkInfo.getWatermarkConfig());
  23. //水印文件的FileUrl(文字水印该字段值为空)
  24. System.out.println("FileUrl = " + watermarkInfo.getFileUrl());
  25. }
  26. } catch (Exception e) {
  27. System.out.println("ErrorMessage = " + e.getLocalizedMessage());
  28. }
  29. System.out.println("RequestId = " + response.getRequestId());
  30. }

查询单个水印

接口参数和返回字段请参考 GetWatermark

  1. import com.aliyuncs.vod.model.v20170321.GetWatermarkRequest;
  2. import com.aliyuncs.vod.model.v20170321.GetWatermarkResponse;
  3. /**
  4. * 查询单个水印配置信息函数
  5. */
  6. public static GetWatermarkResponse getWatermark(DefaultAcsClient client) throws Exception {
  7. GetWatermarkRequest request = new GetWatermarkRequest();
  8. //需要查询水印信息的水印ID
  9. request.setWatermarkId("96d4f6e734a526fd2e442");
  10. return client.getAcsResponse(request);
  11. }
  12. /**
  13. * 以下为调用示例
  14. */
  15. public static void main(String[] args) {
  16. DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
  17. GetWatermarkResponse response = new GetWatermarkResponse();
  18. try {
  19. response = getWatermark(client);
  20. //水印ID
  21. System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
  22. //水印的位置、效果配置数据
  23. System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
  24. //水印文件的FileUrl(文字水印该字段值为空)
  25. System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
  26. } catch (Exception e) {
  27. System.out.println("ErrorMessage = " + e.getLocalizedMessage());
  28. }
  29. System.out.println("RequestId = " + response.getRequestId());
  30. }

设置默认水印

接口参数和返回字段请参考 SetDefaultWatermark

  1. import com.aliyuncs.vod.model.v20170321.SetDefaultWatermarkRequest;
  2. import com.aliyuncs.vod.model.v20170321.SetDefaultWatermarkResponse;
  3. /**
  4. * 设置默认水印配置信息函数
  5. */
  6. public static SetDefaultWatermarkResponse setDefaultWatermark(DefaultAcsClient client) throws Exception {
  7. SetDefaultWatermarkRequest request = new SetDefaultWatermarkRequest();
  8. //设置默认的水印ID
  9. request.setWatermarkId("82105a29c6e96d4f6e734a");
  10. return client.getAcsResponse(request);
  11. }
  12. /**
  13. * 以下为调用示例
  14. */
  15. public static void main(String[] args) {
  16. DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
  17. SetDefaultWatermarkResponse response = new SetDefaultWatermarkResponse();
  18. try {
  19. response = setDefaultWatermark(client);
  20. } catch (Exception e) {
  21. System.out.println("ErrorMessage = " + e.getLocalizedMessage());
  22. }
  23. System.out.println("RequestId = " + response.getRequestId());
  24. }