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 based on normal transcoding or Alibaba Cloud video encryption

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

This sample code applies when you submit a transcoding job based on normal transcoding (without encryption) or Alibaba Cloud video encryption.

  1. // Call example
  2. var client = initVodClient('<Your AccessKey ID>','<Your AccessKey Secret>');
  3. /**
  4. * 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. Ignore this configuration if watermark replacement is not required.
  5. * The watermark ID of the source watermark information (to be replaced) must be associated with the transcoding template ID specified by the TranscodeTemplateId parameter.
  6. * You can call the SubmitTranscodeJobs operation to add only watermarks whose IDs are associated with template IDs.
  7. */
  8. var overrideParams = {
  9. Watermarks: [{
  10. // Associates the ID of the source image watermark with a template ID.
  11. WatermarkId: 'WatermarkId1',
  12. // 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.
  13. FileUrl: 'https://sample.oss-cn-shanghai.aliyuncs.com/watermarks/sample.png'
  14. },{
  15. // Associates the ID of the source text watermark with a template ID.
  16. WatermarkId: 'WatermarkId2',
  17. // Specifies the target content of the text watermark.
  18. Content: 'User ID: 66666'
  19. }]
  20. };
  21. client.request("SubmitTranscodeJobs", {
  22. VideoId: '34a6ca54f5c140eece85a289096d',
  23. TemplateGroupId: 'e8aa925a9798c630d30cd737d4',
  24. OverrideParams: JSON.stringify(overrideParams)
  25. }, {}).then(function (response) {
  26. if (response.TranscodeJobs && response.TranscodeJobs.TranscodeJob && response.TranscodeJobs.TranscodeJob.length > 0){
  27. for(var i=0; i<response.TranscodeJobs.TranscodeJob.length; i++){
  28. console.log('JobId = ' + response.TranscodeJobs.TranscodeJob[i].JobId);
  29. }
  30. }
  31. console.log('RequestId = ' + response.RequestId);
  32. }).catch(function (response) {
  33. console.log('ErrorCode = ' + response.data.Code);
  34. console.log('ErrorMessage = ' + response.data.Message);
  35. console.log('RequestId = ' + response.data.RequestId);
  36. });

Submit a transcoding job based on standard HLS encryption

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

This sample code applies when you submit a transcoding job based on standard HLS encryption.

  1. // Call example
  2. var client = initVodClient('<Your AccessKey ID>','<Your AccessKey Secret>');
  3. /**
  4. * Method for submitting a media processing job
  5. */
  6. function submitTranscodeJobs(overrideParams, encryptConfig) {
  7. var paras = {
  8. VideoId: '34a6ca54f5c140eece85a289096d',
  9. TemplateGroupId: 'e8aa925a9798c630d30cd737d4'
  10. };
  11. // Sets the watermark replacement parameter (applicable only when the watermark information needs to be replaced).
  12. if (! overrideParams){
  13. paras.OverrideParams = JSON.stringify(overrideParams);
  14. }
  15. // Specifies the standard HLS encryption configuration (applicable only to standard encryption).
  16. if (! encryptConfig){
  17. paras.EncryptConfig = JSON.stringify(encryptConfig);
  18. }
  19. client.request("SubmitTranscodeJobs", paras, {}).then(function (response) {
  20. console.log(response);
  21. if (response.TranscodeJobs && response.TranscodeJobs.TranscodeJob && response.TranscodeJobs.TranscodeJob.length > 0){
  22. for(var i=0; i<response.TranscodeJobs.TranscodeJob.length; i++){
  23. console.log('JobId = ' + response.TranscodeJobs.TranscodeJob[i].JobId);
  24. }
  25. }
  26. console.log('RequestId = ' + response.RequestId);
  27. }).catch(function (response) {
  28. console.log('ErrorCode = ' + response.data.Code);
  29. console.log('ErrorMessage = ' + response.data.Message);
  30. console.log('RequestId = ' + response.data.RequestId);
  31. });
  32. }
  33. /**
  34. * 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.
  35. * Note: Set the KeySpec parameter to AES_128 and do not set the NumberOfBytes parameter.
  36. */
  37. client.request("GenerateDataKey", {
  38. KeyId: 'KeyId', // Specifies 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.
  39. KeySpec: 'AES_128'
  40. }, {}).then(function (response) {
  41. console.log('GenerateDataKey RequestId = ' + response.RequestId);
  42. var encryptConfig = {
  43. // 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.
  44. // You can customize the name of the decryption API parameter. Ciphertext is used as an example.
  45. DecryptKeyUri: 'http://decrypt.demo.com/decrypt?Ciphertext=' + response.CiphertextBlob,
  46. // Sets the key service type. Currently, only KMS is supported.
  47. KeyServiceType: 'KMS',
  48. // Specifies the ciphertext key.
  49. CipherText: response.CiphertextBlob
  50. };
  51. /**
  52. * 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. Ignore this configuration if watermark replacement is not required.
  53. * The watermark ID of the source watermark information (to be replaced) must be associated with the transcoding template ID specified by the TranscodeTemplateId parameter.
  54. * You can call the SubmitTranscodeJobs operation to add only watermarks whose IDs are associated with template IDs.
  55. */
  56. var overrideParams = {
  57. Watermarks: [{
  58. // Associates the ID of the source image watermark with a template ID.
  59. WatermarkId: 'WatermarkId1',
  60. // 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.
  61. FileUrl: 'https://sample.oss-cn-shanghai.aliyuncs.com/watermarks/sample.png'
  62. },{
  63. // Associates the ID of the source text watermark with a template ID.
  64. WatermarkId: 'WatermarkId2',
  65. // Specifies the target content of the text watermark.
  66. Content: 'User ID: 66666'
  67. }]
  68. };
  69. submitTranscodeJobs(overrideParams, encryptConfig);
  70. }).catch(function (response) {
  71. console.log('GenerateDataKey ErrorCode = ' + response.data.Code);
  72. console.log('GenerateDataKey ErrorMessage = ' + response.data.Message);
  73. console.log('GenerateDataKey RequestId = ' + response.data.RequestId);
  74. });

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. // Call example
  2. var client = initVodClient('<Your AccessKey ID>','<Your AccessKey Secret>');
  3. var spriteSnapshotConfig = {
  4. CellWidth: '120',
  5. CellHeight: '68',
  6. Columns: '3',
  7. Lines: '10',
  8. Padding: '20',
  9. Margin: '50',
  10. // Keeps the original sprite snapshots.
  11. KeepCellPic: 'keep',
  12. Color: 'tomato'
  13. };
  14. client.request("SubmitSnapshotJob", {
  15. VideoId: '52b440eabcc8ae1cbffcxxxxxx', // Specifies the ID of the video to be snapshot.
  16. SnapshotTemplateId: '1111e6b8baadf589cbffcxxxxxx', // Specifies the snapshot template ID.
  17. // If you set the SnapshotTemplateId parameter, ignore the following parameters:
  18. Count: 50,
  19. SpecifiedOffsetTime: 0,
  20. Interval: 1,
  21. Width: '200',
  22. Height: '200',
  23. SpriteSnapshotConfig: JSON.stringify(spriteSnapshotConfig)
  24. }, {}).then(function (response) {
  25. if (response.SnapshotJob){
  26. // The job ID.
  27. console.log('JobId = ' + response.SnapshotJob.JobId);
  28. }
  29. console.log('RequestId = ' + response.RequestId);
  30. }).catch(function (response) {
  31. console.log('ErrorCode = ' + response.data.Code);
  32. console.log('ErrorMessage = ' + response.data.Message);
  33. console.log('RequestId = ' + response.data.RequestId);
  34. });

Query snapshot data

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

  1. // Call example
  2. var client = initVodClient('<Your AccessKey ID>','<Your AccessKey Secret>');
  3. client.request("ListSnapshots", {
  4. VideoId: '1111121b52b440eabcc8ae1cbffxxxxx', // Sets the video ID.
  5. SnapshotType: 'CoverSnapshot', // Sets the snapshot type.
  6. PageNo: '1',
  7. PageSize: '20'
  8. }, {}).then(function (response) {
  9. if (response.MediaSnapshot && response.MediaSnapshot.Snapshots && response.MediaSnapshot.Snapshots.Snapshot){
  10. for (var i=0; i<response.MediaSnapshot.Snapshots.Snapshot.length; i++){
  11. // The snapshot URL.
  12. console.log('Url = ' + response.MediaSnapshot.Snapshots.Snapshot[i].Url);
  13. }
  14. }
  15. console.log('RequestId = ' + response.RequestId);
  16. }).catch(function (response) {
  17. console.log('ErrorCode = ' + response.data.Code);
  18. console.log('ErrorMessage = ' + response.data.Message);
  19. console.log('RequestId = ' + response.data.RequestId);
  20. });