全部产品

媒体处理

本篇文档提供了.NET SDK媒体处理模块相关功能的API调用示例,包含提交转码作业、提交截图作业、查询截图数据、导播台视频预处理。

初始化客户端

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

提交转码作业

调用SubmitTranscodeJobs接口,完成提交转码作业功能。

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

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

namespace Aliyun.Acs.vod.Sdk.SubmitTranscodeJobs
{
    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
                SubmitTranscodeJobsResponse response = SubmitTranscodeJobs(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>转码JobId.</returns>
        /// <param name="client">Client</param>
        public static SubmitTranscodeJobsResponse SubmitTranscodeJobs(DefaultAcsClient client) 
        {
            // 构造请求
            SubmitTranscodeJobsRequest request = new SubmitTranscodeJobsRequest();
            // 需要转码的视ID
            request.VideoId = "1f7ed5623409489193e41d16f6c7****";
            // 转码模板ID
            request.TemplateGroupId = "3cac80cddb4455e37460f491aecca588";
            // 构建需要替换的水印参数(只有需要替换水印相关信息才需要构建)
            request.OverrideParams = BuildOverrideParams();
            // 覆盖参数,暂只支持水印部分参数替换(只有需要替换水印相关信息才需要传递)
            request.EncryptConfig = BuildEncryptConfig(client);
            return client.GetAcsResponse(request);
        }

        /// <summary>
        /// 生成加密需要的秘钥,response中包含密文秘钥和明文秘钥,用户只需要将密文秘钥传递给点播即可
        /// 注意:KeySpec 必须传递AES_128,且不能设置NumberOfBytes
        /// </summary>
        /// <returns> DataKey </returns>
        /// <param name="client">KMS-SDK Client.</param>
        /// <param name="serviceKey">点播提供生成秘钥的service key,在用户的秘钥管理服务中可看到描述为vod的加密key.</param>
        /// 
        public static GenerateDataKeyResponse GenerateDataKey(DefaultAcsClient client, string serviceKey)
        {
            GenerateDataKeyRequest request = new GenerateDataKeyRequest();
            request.KeyId = serviceKey;
            request.KeySpec = "AES_128";

            return client.GetAcsResponse(request);
        }

        /// <summary>
        /// 构建HLS标准加密的配置信息
        /// </summary>
        /// <returns>配置信息</returns>
        /// <param name="client"> VoD Client.</param>
        /// 
        public static string BuildEncryptConfig(DefaultAcsClient client)
        {
            // 点播给用户在KMS(秘钥管理服务)中的Service Key,可在用户秘钥管理服务对应的区域看到描述为vod的service key
            string serviceKey = "<Your Service Key>";

            // 随机生成一个加密的秘钥,返回的response包含明文秘钥以及密文秘钥,
            // 视频标准加密只需要传递密文秘钥即可
            GenerateDataKeyResponse response = GenerateDataKey(client, serviceKey);

            JObject encryptConfig = new JObject();

            // 解密接口地址,该参数需要将每次生成的密文秘钥与接口URL拼接生成,表示每个视频的解密的密文秘钥都不一样
            // 至于Ciphertext这个解密接口参数的名称,用户可自行制定,这里只作为参考参数名称
            encryptConfig.Add("DecryptKeyUri", "http://192.168.0.0/16/decrypt?" + "Ciphertext=" + response.CiphertextBlob);

            // 秘钥服务的类型,目前只支持KMS
            encryptConfig.Add("KeyServiceType", "KMS");
            // 密文秘钥
            encryptConfig.Add("CipherText", response.CiphertextBlob);

            return encryptConfig.ToString();
        }

        /// <summary>
        /// 1、构建覆盖参数,目前只支持图片水印文件地址、文字水印的内容覆盖;
        /// 2、需要替换的水印信息对应水印ID必须是关联在指定的模板ID(即TranscodeTemplateId)中;
        /// 3、不支持通过媒体处理接口去增加一个没有关联上的水印
        /// 注意:图片水印的文件存储源站需要和发起转码的视频存储源站一致
        /// </summary>
        /// <returns>The override parameters.</returns>
        /// 
        public static string BuildOverrideParams() {
            // 覆盖参数
            JObject overrideParams = new JObject();

            // 图片水印文件地址替换
            JArray watermarks = new JArray();
            JObject watermark1 = new JObject();
            // 模板上面关联需要替换的水印文件图片水印ID
            watermark1.Add("WatermarkId", "2ea587477c5a1bc8b5742d7");
            // 需要替换成对应图片水印文件的OSS地址,水印文件存储源站需要和视频存储源站一致
            watermark1.Add("FileUrl", "https://outin-40564284ef05113e1403e7.oss-cn-shanghai.aliyuncs.com/watermarks/02A1B22DF25D46C3C725A4-6-2.png");
            watermarks.Add(watermark1);

            // 文字水印内容替换
            JObject watermark2 = new JObject();
            // 模板上面关联需要替换内容的文字水印ID
            watermark2.Add("WatermarkId", "d297ba31ac5242d207****");
            // 需要替换成对应的内容
            watermark2.Add("Content", "用户ID:52****");
            watermarks.Add(watermark2);

            overrideParams.Add("Watermarks", watermarks);

            return overrideParams.ToString();
        }
    }
}

提交截图作业

调用SubmitSnapshotJob接口,完成提交截图作业功能。

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

说明

创建截图模板详细请参见截图模板

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.SubmitSnapshotJob
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // 发起请求,并得到 response
                SubmitSnapshotJobResponse response = SubmitSnapshotJob(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());
            }
        }

        public static SubmitSnapshotJobResponse SubmitSnapshotJob(DefaultAcsClient client) 
        {
            // 构造请求
            SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();
            //需要截图的视频ID(推荐传递截图模板ID)
            request.VideoId = "1f7ed5623409489193e41d16f6c7****";
            //截图模板ID
            request.SnapshotTemplateId = "3cac80cddb4455e37460f491aecc****";
            //如果设置了SnapshotTemplateId,会忽略下面参数
            request.Count = 50;
            request.SpecifiedOffsetTime = 0;
            request.Interval = 1;
            request.Width = "200";
            request.Height = "200";
            request.SpriteSnapshotConfig = BuildSnapshotTemplateConfig();
            return client.GetAcsResponse(request);
        }

        /// <summary>
        /// 构建雪碧图截图配置
        /// </summary>
        /// <returns>雪碧图截图配置.</returns>
        public static string BuildSnapshotTemplateConfig()
        {
            // 覆盖参数
            JObject spriteSnapshotConfig = new JObject();
            spriteSnapshotConfig.Add("CellWidth", "120");
            spriteSnapshotConfig.Add("CellHeight", "68");
            spriteSnapshotConfig.Add("Columns", "3");
            spriteSnapshotConfig.Add("Lines", "10");
            spriteSnapshotConfig.Add("Padding", "20");
            spriteSnapshotConfig.Add("Margin", "50");

            //保留雪碧图原始图
            spriteSnapshotConfig.Add("KeepCellPic", "keep");
            spriteSnapshotConfig.Add("Color", "tomato");
            return spriteSnapshotConfig.ToString();
        }
    }
}

查询截图数据

调用ListSnapshots接口,完成查询截图数据功能。

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

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

namespace Aliyun.Acs.vod.Sdk.ListSnapshots
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // 发起请求,并得到 response
                ListSnapshotsResponse response = ListSnapshots(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());
            }
        }

        public static ListSnapshotsResponse ListSnapshots(DefaultAcsClient client)
        {
            // 构造请求
            ListSnapshotsRequest request = new ListSnapshotsRequest();
            // 视频ID
            request.VideoId = "1f7ed5623409489193e41d16f6c7****";
            // 截图类型
            request.SnapshotType = "CoverSnapshot";
            request.PageNo = "1";
            request.PageSize = "20";
            return client.GetAcsResponse(request);
        }
    }
}

导播台视频预处理

调用SubmitPreprocessJobs接口,完成导播台视频预处理功能。

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

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

namespace Aliyun.Acs.vod.Sdk.SubmitPreprocessJobs
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            try
            {
                DefaultAcsClient client = InitVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
                // 发起请求,并得到 response
                SubmitPreprocessJobsResponse response = SubmitPreprocessJobs(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 preprocess jobs.</returns>
        /// <param name="client">Vod Client.</param>
        public static SubmitPreprocessJobsResponse SubmitPreprocessJobs(DefaultAcsClient client)
        {
            // 构造请求
            SubmitPreprocessJobsRequest request = new SubmitPreprocessJobsRequest();
            // 需要截图的视频ID(推荐传递截图模板ID)
            request.VideoId = "c86c0ceb8db54ae09796535****";
            // 截图类型
            request.PreprocessType = "PreprocessType";

            return client.GetAcsResponse(request);
        }
    }
}