全部产品
云市场

媒体处理

更新时间:2019-08-29 10:07:25

初始化客户端

使用前请先初始化客户端,参考 InitVodClient 接口

提交转码作业-普通&阿里云加密

接口参数和返回字段请参考 SubmitTranscodeJobs

此示例代码为使用 普通转码(不加密)、阿里云视频加密 提交转码作业的示例。

  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. // 需要转码的视频ID
  12. request.VideoId = "<VideoId>"
  13. // 指定转码模板组
  14. request.TemplateGroupId = "<TemplateGroupId>"
  15. /*
  16. // 可选设置覆盖参数,如替换水印
  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. * 构建覆盖参数,目前只支持图片水印文件地址、文字水印的内容覆盖;如不使用可忽略
  46. * 需要替换的水印信息对应水印ID必须是关联在指定的转码模板组ID上(即TranscodeTemplateId)
  47. * 不支持通过本接口去增加一个没有关联上的水印
  48. */
  49. func BuildOverrideParams() (overrideParams map[string]interface{}) {
  50. // 以替换水印为例
  51. watermarks := []map[string]interface{}{}
  52. // 替换图片水印文件
  53. watermark1 := map[string]interface{}{"WatermarkId": "<WatermarkId>", "FileUrl": "https://sample.oss-cn-shanghai.aliyuncs.com/watermarks/sample.png"}
  54. watermarks = append(watermarks, watermark1)
  55. // 替换文字水印
  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. }

提交转码作业-HLS标准加密

接口参数和返回字段请参考 SubmitTranscodeJobs

此示例代码为使用 HLS标准加密 提交转码作业的示例。

  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. // 需要转码的视频ID
  13. request.VideoId = "<VideoId>"
  14. // 指定转码模板组
  15. request.TemplateGroupId = "<TemplateGroupId>"
  16. // 使用KMS生成随机的加密密钥
  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. * 生成HLS标准加密参数的可选配置,如不使用可忽略
  51. * 此时依赖KMS服务,需要安装KMS依赖包 aliyun-python-sdk-kms,并引入相关类
  52. * 生成密钥API参考:https://help.aliyun.com/document_detail/28948.html
  53. */
  54. // 初始化KMS客户端,与点播客户端类似
  55. func InitKmsClient(accessKeyId string, accessKeySecret string) (client *kms.Client, err error) {
  56. // KMS接入区域,与点播位于同一区域
  57. regionId := "cn-shanghai"
  58. // 创建授权对象
  59. credential := &credentials.AccessKeyCredential{
  60. accessKeyId,
  61. accessKeySecret,
  62. }
  63. // 自定义config
  64. config := sdk.NewConfig()
  65. config.AutoRetry = true // 失败是否自动重试
  66. config.MaxRetryTime = 3 // 最大重试次数
  67. config.Timeout = 3000000000 // 连接超时,单位:纳秒;默认为3秒
  68. // 创建kmsClient实例
  69. return kms.NewClientWithOptions(regionId, config, credential)
  70. }
  71. // 随机生成加密的密钥
  72. func BuildEncryptConfig(client *kms.Client) (encryptConfig map[string]interface{}, err error) {
  73. request := kms.CreateGenerateDataKeyRequest()
  74. // 随机生成一个加密的密钥,返回的response包含明文密钥以及密文密钥,
  75. // 视频标准加密只需要传递密文秘钥即可,以保证安全
  76. request.KeyId = "<serviceKey>"
  77. request.KeySpec = "AES_128"
  78. request.AcceptFormat = "JSON"
  79. request.Scheme = "HTTPS" // KMS接口必须使用HTTPS
  80. response, err := client.GenerateDataKey(request)
  81. if err != nil {
  82. panic(err)
  83. }
  84. // 解密接口地址,该参数需要将每次生成的密文秘钥与接口URL拼接生成,表示每个视频的解密的密文秘钥都不一样;注意您需要自己部署解密服务
  85. // Ciphertext作为解密接口的参数名称,可自定义,此处只作为参考
  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. }

提交截图作业

接口参数和返回字段请参考 SubmitSnapshotJob

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

  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. // 需要截图的视频ID
  12. request.VideoId = "<VideoId>"
  13. // 截图模板ID
  14. //request.SnapshotTemplateId = "<snapshotTemplateId>"
  15. // 如果设置了截图模板ID,则会忽略以下参数
  16. request.Count = "50"
  17. request.SpecifiedOffsetTime = "0"
  18. request.Interval = "1"
  19. request.Width = "200"
  20. request.Height = "200"
  21. // 雪碧图参数,如不使用可不设置
  22. spriteSnapshotConfig := map[string]interface{}{"CellWidth": 120, "CellHeight": 68, "Columns": 3,
  23. "Lines": 10, "Padding": 20, "Margin": 50}
  24. // 保留雪碧图原始图
  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. }

查询截图数据

接口参数和返回字段请参考 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. }

导播台视频预处理

接口参数和返回字段请参考 SubmitPreprocessJobs

  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 MySubmitPreprocessJobs(client *vod.Client) (response *vod.SubmitPreprocessJobsResponse, err error) {
  10. request := vod.CreateSubmitPreprocessJobsRequest()
  11. request.VideoId = "<VideoId>"
  12. request.PreprocessType = "LivePreprocess"
  13. request.AcceptFormat = "JSON"
  14. return client.SubmitPreprocessJobs(request)
  15. }
  16. func main() {
  17. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  18. if err != nil {
  19. panic(err)
  20. }
  21. response, err := MySubmitPreprocessJobs(client)
  22. if err != nil {
  23. panic(err)
  24. }
  25. fmt.Println(response.GetHttpContentString())
  26. fmt.Println(response.RequestId)
  27. for _, job := range response.PreprocessJobs.PreprocessJob {
  28. fmt.Printf("%s\n", job.JobId)
  29. }
  30. }