全部产品

视频水印

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

初始化客户端

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

添加水印

调用AddWatermark接口,完成添加水印功能。

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

说明
using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;
using Newtonsoft.Json.Linq;

namespace Aliyun.Acs.vod.Sdk.AddWatermark
{
    class MainClass
    {
        /// <summary>
        /// 主函数
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            try
            {
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // 发起请求,并得到 response
                AddWatermarkResponse response = AddWatermark(client);
                Console.WriteLine("RequestId = " + response.RequestId);
            }
            catch (ServerException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (ClientException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (Exception e)
            {
                Console.WriteLine("ErrorMessage = " + e.ToString());
            }
        }

        /// <summary>
        /// 添加水印方法
        /// </summary>
        /// <returns>The watermark.</returns>
        /// <param name="client">Client.</param>
        public static AddWatermarkResponse AddWatermark(DefaultAcsClient client)
        {
            // 构造请求
            AddWatermarkRequest request = new AddWatermarkRequest();
            // 水印名称
            request.Name = "addwatermark";
            // 图片水印必传图片文件的oss文件地址,水印文件必须和视频在同一个区域,例如:华东2视频,水印文件必须存放在华东2
            request.FileUrl = "http://192.168.0.0/16/watermark/test.png";

            // 图片水印的位置配置数据
            request.WatermarkConfig = BuildImageWatermarkConfig();

            // 文字水印的位置配置数据
            request.WatermarkConfig = BuildTextWatermarkConfig();

            // 文字水印:Text; 图片水印:Image
            request.Type = "Image";

            return client.GetAcsResponse(request);
        }

        /// <summary>
        /// 构建图片水印的配置数据,根据具体设置需求修改对应的参数值
        /// </summary>
        /// <returns>The image watermark config.</returns>
        public static string BuildImageWatermarkConfig()
        {
            JObject watermarkConfig = new JObject();
            // 水印的横向偏移距离
            watermarkConfig.Add("Dx", "8");
            // 水印的纵向偏移距离
            watermarkConfig.Add("Dy", "8");
            // 水印显示的宽
            watermarkConfig.Add("Width", "55");
            // 水印显示的高
            watermarkConfig.Add("Height", "55");
            // 水印显示的相对位置(左上、右上、左下、右下)
            watermarkConfig.Add("ReferPos", "BottomRight");

            // 水印显示的时间线(开始显示和结束显示时间)
            JObject timeline = new JObject();
            // 水印开始显示时间
            timeline.Add("Start", "2");
            // 水印结束显示时间
            timeline.Add("Duration", "ToEND");

            watermarkConfig.Add("Timeline", timeline);

            return watermarkConfig.ToString();
        }

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

            return watermarkConfig.ToString();
        }
    }
}

修改水印

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

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

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;
using Newtonsoft.Json.Linq;

namespace Aliyun.Acs.vod.Sdk.UpdateWatermark
{
    class MainClass
    {
        /// <summary>
        /// 主函数
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            try
            {
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // 发起请求,并得到 response
                UpdateWatermarkResponse response = UpdateWatermark(client);
                Console.WriteLine("RequestId = " + response.RequestId);
            }
            catch (ServerException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (ClientException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (Exception e)
            {
                Console.WriteLine("ErrorMessage = " + e.ToString());
            }
        }

        /// <summary>
        /// 更新水印
        /// </summary>
        /// <returns>The watermark.</returns>
        /// <param name="client">Client.</param>
        public static UpdateWatermarkResponse UpdateWatermark(DefaultAcsClient client)
        {
            // 构造请求
            UpdateWatermarkRequest request = new UpdateWatermarkRequest();
            // 水印名称
            request.Name = "updateWatermark";
            // 需要更新配置信息的水印ID
            request.WatermarkId = "421ddddd4f6e734a526fd2****";

            // 图片水印的位置配置数据
            request.WatermarkConfig = BuildImageWatermarkConfig();

            // 文字水印的位置配置数据
            // request.WatermarkConfig = BuildTextWatermarkConfig();

            return client.GetAcsResponse(request);
        }

        /// <summary>
        /// 构建图片水印的配置数据,根据具体设置需求修改对应的参数值
        /// </summary>
        /// <returns>The image watermark config.</returns>
        public static string BuildImageWatermarkConfig()
        {
            JObject watermarkConfig = new JObject();
            // 水印的横向偏移距离
            watermarkConfig.Add("Dx", "8");
            // 水印的纵向偏移距离
            watermarkConfig.Add("Dy", "8");
            // 水印显示的宽
            watermarkConfig.Add("Width", "55");
            // 水印显示的高
            watermarkConfig.Add("Height", "55");
            // 水印显示的相对位置(左上、右上、左下、右下)
            watermarkConfig.Add("ReferPos", "BottomRight");

            // 水印显示的时间线(开始显示和结束显示时间)
            JObject timeline = new JObject();
            // 水印开始显示时间
            timeline.Add("Start", "2");
            // 水印结束显示时间
            timeline.Add("Duration", "ToEND");

            watermarkConfig.Add("Timeline", timeline);

            return watermarkConfig.ToString();
        }

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

            return watermarkConfig.ToString();
        }
    }
}

删除水印

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

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

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;

namespace Aliyun.Acs.vod.Sdk.DeleteWatermark
{
    class MainClass
    {
        /// <summary>
        /// 主函数
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            try
            {
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // 发起请求,并得到 response
                DeleteWatermarkResponse response = DeleteWatermark(client);
                Console.WriteLine("RequestId = " + response.RequestId);
            }
            catch (ServerException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (ClientException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (Exception e)
            {
                Console.WriteLine("ErrorMessage = " + e.ToString());
            }
        }

        /// <summary>
        /// 删除水印配置信息函数
        /// </summary>
        /// <returns>The watermark response.</returns>
        /// <param name="client">Client.</param>
        public static DeleteWatermarkResponse DeleteWatermark(DefaultAcsClient client)
        {
            // 构造请求
            DeleteWatermarkRequest request = new DeleteWatermarkRequest();
            // 水印ID
            request.WatermarkId = "53f9d796fad9d7b862b2e****";
            return client.GetAcsResponse(request);
        }
    }
}

查询水印列表

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

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

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;

namespace Aliyun.Acs.vod.Sdk.ListWatermark
{
    class MainClass
    {
        /// <summary>
        /// 主函数
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            try
            {
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // 发起请求,并得到 response
                ListWatermarkResponse response = ListWatermark(client);

                if (response.WatermarkInfos != null && response.WatermarkInfos.Count > 0)
                {
                    foreach (ListWatermarkResponse.ListWatermark_WatermarkInfo watermarkInfo in response.WatermarkInfos) 
                    {
                        //水印ID
                        Console.WriteLine("WatermarkId = " + watermarkInfo.WatermarkId);
                        //水印的位置、效果配置数据
                        Console.WriteLine("WatermarkConfig = " + watermarkInfo.WatermarkConfig);
                        //水印文件的FileUrl(文字水印该字段值为空)
                        Console.WriteLine("FileUrl = " + watermarkInfo.FileUrl);
                    }
                }
                Console.WriteLine("RequestId = " + response.RequestId);
            }
            catch (ServerException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (ClientException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (Exception e)
            {
                Console.WriteLine("ErrorMessage = " + e.ToString());
            }
        }

        /// <summary>
        /// 获取水印列表
        /// </summary>
        /// <returns>The watermark list.</returns>
        /// <param name="client">Client.</param>
        public static ListWatermarkResponse ListWatermark(DefaultAcsClient client)
        {
            // 构造请求
            ListWatermarkRequest request = new ListWatermarkRequest();

            return client.GetAcsResponse(request);
        }

    }
}

查询单个水印

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

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

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;

namespace Aliyun.Acs.vod.Sdk.GetWatermark
{
    class MainClass
    {
        /// <summary>
        /// 主函数
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            try
            {
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // 发起请求,并得到 response
                GetWatermarkResponse response = GetWatermark(client);
                if (response.WatermarkInfo != null)
                {
                    //水印ID
                    Console.WriteLine("WatermarkId = " + response.WatermarkInfo.WatermarkId);
                    //水印的位置、效果配置数据
                    Console.WriteLine("WatermarkConfig = " + response.WatermarkInfo.WatermarkConfig);
                    //水印文件的FileUrl(文字水印该字段值为空)
                    Console.WriteLine("FileUrl = " + response.WatermarkInfo.FileUrl);
                }
                Console.WriteLine("RequestId = " + response.RequestId);
            }
            catch (ServerException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (ClientException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (Exception e)
            {
                Console.WriteLine("ErrorMessage = " + e.ToString());
            }
        }

        /// <summary>
        /// 查询单个水印配置信息函数
        /// </summary>
        /// <returns>The watermark.</returns>
        /// <param name="client">Client.</param>
        public static GetWatermarkResponse GetWatermark(DefaultAcsClient client)
        {
            // 构造请求
            GetWatermarkRequest request = new GetWatermarkRequest();
            //需要查询水印信息的水印ID
            request.WatermarkId = "96d4f6e734a526fd2e442";
            return client.GetAcsResponse(request);
        }
    }
}

设置默认水印

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

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

using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.vod.Model.V20170321;

namespace Aliyun.Acs.vod.Sdk.SetDefaultWatermark
{
    class MainClass
    {
        /// <summary>
        /// 主函数
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            try
            {
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // 发起请求,并得到 response
                SetDefaultWatermarkResponse response = SetDefaultWatermark(client);

                Console.WriteLine("RequestId = " + response.RequestId);
            }
            catch (ServerException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (ClientException e)
            {
                if (e.RequestId != null)
                {
                    Console.WriteLine("RequestId = " + e.RequestId);
                }
                Console.WriteLine("ErrorCode = " + e.ErrorCode);
                Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
            }
            catch (Exception e)
            {
                Console.WriteLine("ErrorMessage = " + e.ToString());
            }
        }

        /// <summary>
        /// 设置默认水印配置信息函数
        /// </summary>
        /// <returns>The default watermark.</returns>
        /// <param name="client">Client.</param>
        public static SetDefaultWatermarkResponse SetDefaultWatermark(DefaultAcsClient client)
        {
            // 构造请求
            SetDefaultWatermarkRequest request = new SetDefaultWatermarkRequest();
            // 设置默认的水印ID
            request.WatermarkId = "82105a29c6e96d4f6e****";

            return client.GetAcsResponse(request);
        }
    }
}