All Products
Search
Document Center

Media processing

Last Updated: Aug 01, 2019

Initialize a client

Initialize a client before using the SDK. For more information, see InitVodClient.

Submit a transcoding job

For more information about the request and response parameters, see SubmitTranscodeJobs.

  1. using System;
  2. using Aliyun.Acs.Core;
  3. using Aliyun.Acs.Core.Exceptions;
  4. using Aliyun.Acs.Kms.Model.V20160120;
  5. using Aliyun.Acs.vod.Model.V20170321;
  6. using Newtonsoft.Json.Linq;
  7. namespace Aliyun.Acs.vod.Sdk.SubmitTranscodeJobs
  8. {
  9. class MainClass
  10. {
  11. /// <summary>
  12. /// The main method.
  13. /// </summary>
  14. /// <param name="args">The command-line arguments. </param>
  15. public static void Main(string[] args)
  16. {
  17. try
  18. {
  19. DefaultAcsClient client = InitVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
  20. // Initiates the request and obtains a response.
  21. SubmitTranscodeJobsResponse response = SubmitTranscodeJobs(client);
  22. Console.WriteLine("RequestId = " + response.RequestId);
  23. }
  24. catch (ServerException e)
  25. {
  26. if (e.RequestId ! = null)
  27. {
  28. Console.WriteLine("RequestId = " + e.RequestId);
  29. }
  30. Console.WriteLine("ErrorCode = " + e.ErrorCode);
  31. Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
  32. }
  33. catch (ClientException e)
  34. {
  35. if (e.RequestId ! = null)
  36. {
  37. Console.WriteLine("RequestId = " + e.RequestId);
  38. }
  39. Console.WriteLine("ErrorCode = " + e.ErrorCode);
  40. Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
  41. }
  42. catch (Exception e)
  43. {
  44. Console.WriteLine("ErrorMessage = " + e.ToString());
  45. }
  46. }
  47. /// <summary>
  48. /// Submits a transcoding job.
  49. /// </summary>
  50. /// <returns>The ID of the transcoding job.</returns>
  51. /// <param name="client">The client.</param>
  52. public static SubmitTranscodeJobsResponse SubmitTranscodeJobs(DefaultAcsClient client)
  53. {
  54. // Constructs a request.
  55. SubmitTranscodeJobsRequest request = new SubmitTranscodeJobsRequest();
  56. // Specifies the ID of the video to be transcoded.
  57. request.VideoId = "1f7ed5623409489193e41d16f6c7e0fc";
  58. // Sets the ID of the transcoding template group.
  59. request.TemplateGroupId = "3cac80cddb4455e37460f491aecca588";
  60. // Constructs a watermark replacement parameter (applicable only when the watermark information needs to be replaced).
  61. request.OverrideParams = BuildOverrideParams();
  62. // Sets the watermark replacement parameter (applicable only when the watermark information needs to be replaced).
  63. request.EncryptConfig = BuildEncryptConfig(client);
  64. return client.GetAcsResponse(request);
  65. }
  66. /// <summary>
  67. /// Generates an encryption key. The response contains a plaintext key and a ciphertext key. You need to specify only the ciphertext key for ApsaraVideo for VOD.
  68. /// Note: Set the KeySpec parameter to AES_128 and do not set the NumberOfBytes parameter.
  69. /// </summary>
  70. /// <returns>The DataKey. </returns>
  71. /// <param name="client">The Key Management Service (KMS) SDK client.</param>
  72. /// <param name="serviceKey">The service key that ApsaraVideo for VOD grants to you for generating a key. You can view the service key whose description is vod on the Keys page in the KMS console.</param>
  73. ///
  74. public static GenerateDataKeyResponse GenerateDataKey(DefaultAcsClient client, string serviceKey)
  75. {
  76. GenerateDataKeyRequest request = new GenerateDataKeyRequest();
  77. request.KeyId = serviceKey;
  78. request.KeySpec = "AES_128";
  79. return client.GetAcsResponse(request);
  80. }
  81. /// <summary>
  82. /// Constructs the standard HLS encryption configuration.
  83. /// </summary>
  84. /// <returns>The configuration information.</returns>
  85. /// <param name="client">The ApsaraVideo for VOD client.</param>
  86. ///
  87. public static string BuildEncryptConfig(DefaultAcsClient client)
  88. {
  89. // Specifies the service key that ApsaraVideo for VOD grants to you in KMS. You can view the service key whose description is vod on the Keys page in the KMS console.
  90. string serviceKey = "<Your service key>";
  91. // Generates a random encryption key. The response contains a plaintext key and a ciphertext key.
  92. // Specifies only the ciphertext key for standard video encryption.
  93. GenerateDataKeyResponse response = GenerateDataKey(client, serviceKey);
  94. JObject encryptConfig = new JObject();
  95. // Specifies the URL of the decryption API. The ciphertext key generated each time and the API URL are concatenated to generate the value of this parameter to provide a unique ciphertext decryption key for each video.
  96. // You can customize the name of the decryption API parameter. Ciphertext is used as an example.
  97. encryptConfig.Add("DecryptKeyUri", "http://decrypt.demo.com/decrypt?" + "Ciphertext=" + response.CiphertextBlob);
  98. // Sets the key service type. Currently, only KMS is supported.
  99. encryptConfig.Add("KeyServiceType", "KMS");
  100. // Specifies the ciphertext key.
  101. encryptConfig.Add("CipherText", response.CiphertextBlob);
  102. return encryptConfig.ToString();
  103. }
  104. /// <summary>
  105. /// Currently, the constructed watermark replacement parameter can be used to replace only the file URL of an image watermark or the content of a text watermark.
  106. /// The watermark ID of the source watermark information (to be replaced) must be associated with the transcoding template ID specified by the TranscodeTemplateId parameter.
  107. /// You can call the SubmitTranscodeJobs operation to add only watermarks whose IDs are associated with template IDs.
  108. /// Note: The origin that stores the image watermark file must be the same as the origin that stores the video to be transcoded.
  109. /// </summary>
  110. /// <returns>The watermark replacement parameter.</returns>
  111. ///
  112. public static string BuildOverrideParams() {
  113. // Sets the watermark replacement parameter.
  114. JObject overrideParams = new JObject();
  115. // Replaces the file URL of an image watermark.
  116. JArray watermarks = new JArray();
  117. JObject watermark1 = new JObject();
  118. // Associates the ID of the source image watermark with a template ID.
  119. watermark1.Add("WatermarkId", "2ea587477c5a1bc8b5742d7");
  120. // Specifies the target OSS URL of the image watermark file. The origin that stores the image watermark file must be the same as the origin that stores the video to be transcoded.
  121. watermark1.Add("FileUrl", "https://outin-40564284ef05113e1403e7.oss-cn-shanghai.aliyuncs.com/watermarks/02A1B22DF25D46C3C725A4-6-2.png");
  122. watermarks.Add(watermark1);
  123. // Replaces the content of a text watermark.
  124. JObject watermark2 = new JObject();
  125. // Associates the ID of the source text watermark with a template ID.
  126. watermark2.Add("WatermarkId", "d297ba31ac5242d2071bf7");
  127. // Specifies the target content of the text watermark.
  128. watermark2.Add("Content", "User ID: 66666");
  129. watermarks.Add(watermark2);
  130. overrideParams.Add("Watermarks", watermarks);
  131. return overrideParams.ToString();
  132. }
  133. }
  134. }

Submit a video snapshot job

For more information about the request and response parameters, see SubmitSnapshotJob.

For more information about how to create a snapshot template, see AddVodTemplate.

  1. using System;
  2. using Aliyun.Acs.Core;
  3. using Aliyun.Acs.Core.Exceptions;
  4. using Aliyun.Acs.vod.Model.V20170321;
  5. using Newtonsoft.Json.Linq;
  6. namespace Aliyun.Acs.vod.Sdk.SubmitSnapshotJob
  7. {
  8. class MainClass
  9. {
  10. public static void Main(string[] args)
  11. {
  12. try
  13. {
  14. DefaultAcsClient client = InitVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
  15. // Initiates the request and obtains a response.
  16. SubmitSnapshotJobResponse response = SubmitSnapshotJob(client);
  17. Console.WriteLine("RequestId = " + response.RequestId);
  18. }
  19. catch (ServerException e)
  20. {
  21. if (e.RequestId ! = null)
  22. {
  23. Console.WriteLine("RequestId = " + e.RequestId);
  24. }
  25. Console.WriteLine("ErrorCode = " + e.ErrorCode);
  26. Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
  27. }
  28. catch (ClientException e)
  29. {
  30. if (e.RequestId ! = null)
  31. {
  32. Console.WriteLine("RequestId = " + e.RequestId);
  33. }
  34. Console.WriteLine("ErrorCode = " + e.ErrorCode);
  35. Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
  36. }
  37. catch (Exception e)
  38. {
  39. Console.WriteLine("ErrorMessage = " + e.ToString());
  40. }
  41. }
  42. public static SubmitSnapshotJobResponse SubmitSnapshotJob(DefaultAcsClient client)
  43. {
  44. // Constructs a request.
  45. SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();
  46. // Specifies the ID of the video to be snapshot. (We recommend that you use a snapshot template.)
  47. request.VideoId = "1f7ed5623409489193e41d16f6c7e0fc";
  48. // Specifies the snapshot template ID.
  49. request.SnapshotTemplateId = "3cac80cddb4455e37460f491aecca588";
  50. // If you set the SnapshotTemplateId parameter, ignore the following parameters:
  51. request.Count = 50;
  52. request.SpecifiedOffsetTime = 0;
  53. request.Interval = 1;
  54. request.Width = "200";
  55. request.Height = "200";
  56. request.SpriteSnapshotConfig = BuildSnapshotTemplateConfig();
  57. return client.GetAcsResponse(request);
  58. }
  59. /// <summary>
  60. /// Constructs the sprite snapshot configuration.
  61. /// </summary>
  62. /// <returns>The sprite snapshot configuration.</returns>
  63. public static string BuildSnapshotTemplateConfig()
  64. {
  65. // Specifies the sprite snapshot configuration.
  66. JObject spriteSnapshotConfig = new JObject();
  67. spriteSnapshotConfig.Add("CellWidth", "120");
  68. spriteSnapshotConfig.Add("CellHeight", "68");
  69. spriteSnapshotConfig.Add("Columns", "3");
  70. spriteSnapshotConfig.Add("Lines", "10");
  71. spriteSnapshotConfig.Add("Padding", "20");
  72. spriteSnapshotConfig.Add("Margin", "50");
  73. // Keeps the original sprite snapshots.
  74. spriteSnapshotConfig.Add("KeepCellPic", "keep");
  75. spriteSnapshotConfig.Add("Color", "tomato");
  76. return spriteSnapshotConfig.ToString();
  77. }
  78. }
  79. }

Query snapshot data

For more information about the request and response parameters, see ListSnapshots.

  1. using System;
  2. using Aliyun.Acs.Core;
  3. using Aliyun.Acs.Core.Exceptions;
  4. using Aliyun.Acs.vod.Model.V20170321;
  5. namespace Aliyun.Acs.vod.Sdk.ListSnapshots
  6. {
  7. class MainClass
  8. {
  9. public static void Main(string[] args)
  10. {
  11. try
  12. {
  13. DefaultAcsClient client = InitVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
  14. // Initiates the request and obtains a response.
  15. ListSnapshotsResponse response = ListSnapshots(client);
  16. Console.WriteLine("RequestId = " + response.RequestId);
  17. }
  18. catch (ServerException e)
  19. {
  20. if (e.RequestId ! = null)
  21. {
  22. Console.WriteLine("RequestId = " + e.RequestId);
  23. }
  24. Console.WriteLine("ErrorCode = " + e.ErrorCode);
  25. Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
  26. }
  27. catch (ClientException e)
  28. {
  29. if (e.RequestId ! = null)
  30. {
  31. Console.WriteLine("RequestId = " + e.RequestId);
  32. }
  33. Console.WriteLine("ErrorCode = " + e.ErrorCode);
  34. Console.WriteLine("ErrorMessage = " + e.ErrorMessage);
  35. }
  36. catch (Exception e)
  37. {
  38. Console.WriteLine("ErrorMessage = " + e.ToString());
  39. }
  40. }
  41. public static ListSnapshotsResponse ListSnapshots(DefaultAcsClient client)
  42. {
  43. // Constructs a request.
  44. ListSnapshotsRequest request = new ListSnapshotsRequest();
  45. // Specifies the video ID.
  46. request.VideoId = "1f7ed5623409489193e41d16f6c7e1fc";
  47. // Sets the snapshot type.
  48. request.SnapshotType = "CoverSnapshot";
  49. request.PageNo = "1";
  50. request.PageSize = "20";
  51. return client.GetAcsResponse(request);
  52. }
  53. }
  54. }