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. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "encoding/json"
  7. "fmt"
  8. )
  9. func NormalSubmitTranscodeJobs(client *vod.Client) (response *vod.SubmitTranscodeJobsResponse, err error) {
  10. request := vod.CreateSubmitTranscodeJobsRequest()
  11. // Specifies the ID of the video to be transcoded.
  12. request.VideoId = "<VideoId>"
  13. // Specifies the ID of the transcoding template group.
  14. request.TemplateGroupId = "<TemplateGroupId>"
  15. /*
  16. // (Optional) Constructs a replacement parameter, for example, a watermark replacement parameter.
  17. overrideParams := BuildOverrideParams()
  18. jsonParams, err := json.Marshal(overrideParams)
  19. if err ! = nil {
  20. fmt.Println("json.Marshal failed:", err)
  21. return
  22. }
  23. request.OverrideParams = string(jsonParams)
  24. fmt.Println(request.OverrideParams)
  25. */
  26. request.AcceptFormat = "JSON"
  27. return client.SubmitTranscodeJobs(request)
  28. }
  29. func main() {
  30. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  31. if err ! = nil {
  32. panic(err)
  33. }
  34. response, err := NormalSubmitTranscodeJobs(client)
  35. if err ! = nil {
  36. panic(err)
  37. }
  38. fmt.Println(response.GetHttpContentString())
  39. fmt.Println(response.RequestId)
  40. for _, job := range response.TranscodeJobs.TranscodeJob {
  41. fmt.Printf("%s\n", job.JobId)
  42. }
  43. }
  44. /*
  45. * 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.
  46. * The watermark ID of the source watermark information (to be replaced) must be associated with the transcoding template ID specified by the TranscodeTemplateId parameter.
  47. * You can call the SubmitTranscodeJobs operation to add only watermarks whose IDs are associated with template IDs.
  48. */
  49. func BuildOverrideParams() (overrideParams map[string]interface{}) {
  50. // For example, replace a watermark.
  51. watermarks := []map[string]interface{}{}
  52. // Replaces the file URL of an image watermark.
  53. watermark1 := map[string]interface{}{"WatermarkId": "<WatermarkId>", "FileUrl": "https://sample.oss-cn-shanghai.aliyuncs.com/watermarks/sample.png"}
  54. watermarks = append(watermarks, watermark1)
  55. // Replaces the content of a text watermark.
  56. watermark2 := map[string]interface{}{"WatermarkId": "<WatermarkId>", "Content": "new Text"}
  57. watermarks = append(watermarks, watermark2)
  58. overrideParams = map[string]interface{}{"Watermarks": watermarks}
  59. return overrideParams
  60. }

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. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "encoding/json"
  7. "github.com/aliyun/alibaba-cloud-sdk-go/services/kms"
  8. "fmt"
  9. )
  10. func HLSEncryptSubmitTranscodeJobs(client *vod.Client, kmsClient *kms.Client) (response *vod.SubmitTranscodeJobsResponse, err error) {
  11. request := vod.CreateSubmitTranscodeJobsRequest()
  12. // Specifies the ID of the video to be transcoded.
  13. request.VideoId = "<VideoId>"
  14. // Specifies the ID of the transcoding template group.
  15. request.TemplateGroupId = "<TemplateGroupId>"
  16. // Uses KMS to generate a random encryption key.
  17. encryptConfig, err := BuildEncryptConfig(kmsClient)
  18. if err ! = nil {
  19. panic(err)
  20. }
  21. jsonConfig, err := json.Marshal(encryptConfig)
  22. if err ! = nil {
  23. fmt.Println("json.Marshal failed:", err)
  24. return
  25. }
  26. request.EncryptConfig = string(jsonConfig)
  27. request.AcceptFormat = "JSON"
  28. return client.SubmitTranscodeJobs(request)
  29. }
  30. func main() {
  31. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  32. if err ! = nil {
  33. panic(err)
  34. }
  35. kmsClient, err := InitKmsClient("<accessKeyId>", "<accessKeySecret>")
  36. if err ! = nil {
  37. panic(err)
  38. }
  39. response, err := HLSEncryptSubmitTranscodeJobs(client, kmsClient)
  40. if err ! = nil {
  41. panic(err)
  42. }
  43. fmt.Println(response.GetHttpContentString())
  44. fmt.Println(response.RequestId)
  45. for _, job := range response.TranscodeJobs.TranscodeJob {
  46. fmt.Printf("%s\n", job.JobId)
  47. }
  48. }
  49. /*
  50. * The standard HLS encryption configuration is optional. Ignore this configuration if standard HLS encryption is not required.
  51. * This operation is dependent on KMS. You need to install the KMS dependency package aliyun-python-sdk-kms and reference the relevant class.
  52. * For more information about the operation used to generate a key, see https://help.aliyun.com/document_detail/28948.html.
  53. */
  54. // Initializes a KMS client, the method for which is similar to that for initializing an ApsaraVideo for VOD client.
  55. func InitKmsClient(accessKeyId string, accessKeySecret string) (client *kms.Client, err error) {
  56. // Set the region of KMS, which must be the same as the region of ApsaraVideo for VOD.
  57. regionId := "cn-shanghai"
  58. // Creates an authorization object.
  59. credential := &credentials.AccessKeyCredential{
  60. accessKeyId,
  61. accessKeySecret,
  62. }
  63. // Customizes the connection configuration.
  64. config := sdk.NewConfig()
  65. config.AutoRetry = true // Indicates whether the system automatically retries to connect to KMS upon a connection failure.
  66. config.MaxRetryTime = 3 // The maximum number of retries allowed.
  67. config.Timeout = 3000000000 // The connection timeout duration, in nanoseconds. The default value 3000000000 indicates 3 seconds.
  68. // Creates a kmsClient instance.
  69. return kms.NewClientWithOptions(regionId, config, credential)
  70. }
  71. // Generates a random encryption key.
  72. func BuildEncryptConfig(client *kms.Client) (encryptConfig map[string]interface{}, err error) {
  73. request := kms.CreateGenerateDataKeyRequest()
  74. // Generates a random encryption key. The response contains a plaintext key and a ciphertext key.
  75. // Specifies only the ciphertext key for standard video encryption to ensure security.
  76. request.KeyId = "<serviceKey>"
  77. request.KeySpec = "AES_128"
  78. request.AcceptFormat = "JSON"
  79. request.Scheme = "HTTPS" // Sets the protocol type to HTTPS if you call a KMS operation.
  80. response, err := client.GenerateDataKey(request)
  81. if err ! = nil {
  82. panic(err)
  83. }
  84. // 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. Note that you need to deploy a decryption service.
  85. // You can customize the name of the decryption API parameter. Ciphertext is used as an example.
  86. decryptKeyUri := "http://decrypt.demo.com/decrypt?" + "Ciphertext=" + response.CiphertextBlob
  87. encryptConfig = map[string]interface{}{"DecryptKeyUri": decryptKeyUri, "KeyServiceType": "KMS",
  88. "CipherText": response.CiphertextBlob}
  89. return encryptConfig, nil
  90. }

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. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "encoding/json"
  7. "fmt"
  8. )
  9. func MySubmitSnapshotJob(client *vod.Client) (response *vod.SubmitSnapshotJobResponse, err error) {
  10. request := vod.CreateSubmitSnapshotJobRequest()
  11. // Specifies the ID of the video to be snapshot.
  12. request.VideoId = "<VideoId>"
  13. // Specifies the snapshot template ID.
  14. //request.SnapshotTemplateId = "<snapshotTemplateId>"
  15. // If you set the SnapshotTemplateId parameter, ignore the following parameters:
  16. request.Count = "50"
  17. request.SpecifiedOffsetTime = "0"
  18. request.Interval = "1"
  19. request.Width = "200"
  20. request.Height = "200"
  21. // Specifies the sprite snapshot configuration if required.
  22. spriteSnapshotConfig := map[string]interface{}{"CellWidth": 120, "CellHeight": 68, "Columns": 3,
  23. "Lines": 10, "Padding": 20, "Margin": 50}
  24. // Keeps the original sprite snapshots.
  25. spriteSnapshotConfig["KeepCellPic"] = "keep"
  26. spriteSnapshotConfig["Color"] = "tomato"
  27. jsonSpriteConfig, err := json.Marshal(spriteSnapshotConfig)
  28. if err ! = nil {
  29. fmt.Println("json.Marshal failed:", err)
  30. return
  31. }
  32. request.SpriteSnapshotConfig = string(jsonSpriteConfig)
  33. request.AcceptFormat = "JSON"
  34. return client.SubmitSnapshotJob(request)
  35. }
  36. func main() {
  37. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  38. if err ! = nil {
  39. panic(err)
  40. }
  41. response, err := MySubmitSnapshotJob(client)
  42. if err ! = nil {
  43. panic(err)
  44. }
  45. fmt.Println(response.GetHttpContentString())
  46. fmt.Println(response.RequestId)
  47. fmt.Println(response.SnapshotJob.JobId)
  48. }

Query snapshot data

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

  1. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "fmt"
  7. )
  8. func MyListSnapshots(client *vod.Client) (response *vod.ListSnapshotsResponse, err error) {
  9. request := vod.CreateListSnapshotsRequest()
  10. request.VideoId = "<VideoId>"
  11. request.SnapshotType = "CoverSnapshot"
  12. request.PageNo = "1"
  13. request.PageSize = "20"
  14. request.AuthTimeout = "86400"
  15. request.AcceptFormat = "JSON"
  16. return client.ListSnapshots(request)
  17. }
  18. func main() {
  19. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  20. if err ! = nil {
  21. panic(err)
  22. }
  23. response, err := MyListSnapshots(client)
  24. if err ! = nil {
  25. panic(err)
  26. }
  27. fmt.Println(response.GetHttpContentString())
  28. fmt.Println(response.RequestId)
  29. fmt.Println(response.MediaSnapshot.Total)
  30. for _, snapshot := range response.MediaSnapshot.Snapshots.Snapshot {
  31. fmt.Printf("%d: %s\n", snapshot.Index, snapshot.Url)
  32. }
  33. }