全部产品
云市场

转码模板

更新时间:2019-02-14 17:09:22

初始化客户端

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

添加转码模板组

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

  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. "encoding/json"
  8. )
  9. func MyAddTranscodeTemplateGroup(client *vod.Client) (response *vod.AddTranscodeTemplateGroupResponse, err error) {
  10. request := vod.CreateAddTranscodeTemplateGroupRequest()
  11. request.Name = "SampleTranscodeTemplateGroup"
  12. transcodeTemplateList := BuildTranscodeTemplateList()
  13. jsonTranscodeConfig, err := json.Marshal(transcodeTemplateList)
  14. if err != nil {
  15. fmt.Println("json.Marshal failed:", err)
  16. return
  17. }
  18. request.TranscodeTemplateList = string(jsonTranscodeConfig)
  19. request.AcceptFormat = "JSON"
  20. return client.AddTranscodeTemplateGroup(request)
  21. }
  22. // 构建需要添加的转码模板配置数据
  23. func BuildTranscodeTemplateList() (transcodeTemplateList []map[string]interface{}) {
  24. transcodeTemplate := map[string]interface{}{}
  25. // 设置模板名称
  26. transcodeTemplate["TemplateName"] = "MP4-for-Low-Definition"
  27. // 清晰度
  28. transcodeTemplate["Definition"] = "LD"
  29. // 视频流转码配置
  30. videoConfig := map[string]interface{}{"Width": 640, "Bitrate": 400, "Fps": 25, "Remove": false, "Codec": "H.264", "Gop": "250"}
  31. transcodeTemplate["Video"] = videoConfig
  32. // 音频流转码配置
  33. audioConfig := map[string]interface{}{"Codec": "AAC", "Bitrate": "64", "Channels": "2", "Samplerate": "32000"}
  34. transcodeTemplate["Audio"] = audioConfig
  35. // 封装容器
  36. container := map[string]interface{}{"Format": "mp4"}
  37. transcodeTemplate["Container"] = container
  38. // 条件转码配置
  39. condition := map[string]interface{}{"IsCheckReso": false, "IsCheckResoFail": false, "IsCheckVideoBitrate": false,
  40. "IsCheckVideoBitrateFail": false, "IsCheckAudioBitrate": false, "IsCheckAudioBitrateFail": false}
  41. transcodeTemplate["TransConfig"] = condition
  42. /*
  43. // 加密配置(只支持HLS)
  44. encryptSetting := map[string]interface{}{"EncryptType": "Private"}
  45. transcodeTemplate["EncryptSetting"] = encryptSetting
  46. */
  47. // 水印ID(多水印关联)
  48. watermarkIdList := []string{"USER_DEFAULT_WATERMARK"}
  49. //watermarkIdList = append(watermarkIdList, "<WatermarkId>")
  50. transcodeTemplate["WatermarkIds"] = watermarkIdList
  51. transcodeTemplateList = append(transcodeTemplateList, transcodeTemplate)
  52. return transcodeTemplateList
  53. }
  54. func main() {
  55. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  56. if err != nil {
  57. panic(err)
  58. }
  59. response, err := MyAddTranscodeTemplateGroup(client)
  60. if err != nil {
  61. panic(err)
  62. }
  63. fmt.Println(response.GetHttpContentString())
  64. fmt.Println(response.TranscodeTemplateGroupId)
  65. }

修改转码模板组

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

  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. "encoding/json"
  8. )
  9. func MyUpdateTranscodeTemplateGroup(client *vod.Client) (response *vod.UpdateTranscodeTemplateGroupResponse, err error) {
  10. request := vod.CreateUpdateTranscodeTemplateGroupRequest()
  11. request.TranscodeTemplateGroupId = "<TranscodeTemplateGroupId>"
  12. // 修改模板组名称,可选
  13. request.Name = "new-SampleTranscodeTemplateGroup"
  14. // 修改模板组已有模板的配置,可选
  15. transcodeTemplateList := BuildNewTranscodeTemplateList()
  16. jsonTranscodeConfig, err := json.Marshal(transcodeTemplateList)
  17. if err != nil {
  18. fmt.Println("json.Marshal failed:", err)
  19. return
  20. }
  21. request.TranscodeTemplateList = string(jsonTranscodeConfig)
  22. request.AcceptFormat = "JSON"
  23. return client.UpdateTranscodeTemplateGroup(request)
  24. }
  25. // 构建需要修改的转码模板配置数据
  26. func BuildNewTranscodeTemplateList() (transcodeTemplateList []map[string]interface{}) {
  27. transcodeTemplate := map[string]interface{}{}
  28. // 设置转码模板ID,必填
  29. transcodeTemplate["TranscodeTemplateId"] = "<TranscodeTemplateId>"
  30. // 以下都是可选项
  31. // 设置新的模板名称(如果需要更改)
  32. transcodeTemplate["TemplateName"] = "new-MP4-for-High-Definition"
  33. // 视频流转码配置
  34. videoConfig := map[string]interface{}{"Width": 640, "Bitrate": 500, "Fps": 60, "Remove": false, "Codec": "H.264", "Gop": "250"}
  35. transcodeTemplate["Video"] = videoConfig
  36. // 音频流转码配置
  37. audioConfig := map[string]interface{}{"Codec": "AAC", "Bitrate": "128", "Channels": "2", "Samplerate": "32000"}
  38. transcodeTemplate["Audio"] = audioConfig
  39. // 封装容器
  40. container := map[string]interface{}{"Format": "mp4"}
  41. transcodeTemplate["Container"] = container
  42. // 条件转码配置
  43. condition := map[string]interface{}{"IsCheckReso": false, "IsCheckResoFail": false, "IsCheckVideoBitrate": false,
  44. "IsCheckVideoBitrateFail": false, "IsCheckAudioBitrate": false, "IsCheckAudioBitrateFail": false}
  45. transcodeTemplate["TransConfig"] = condition
  46. /*
  47. // 加密配置(只支持HLS)
  48. encryptSetting := map[string]interface{}{"EncryptType": "Private"}
  49. transcodeTemplate["EncryptSetting"] = encryptSetting
  50. */
  51. // 水印ID(多水印关联)
  52. watermarkIdList := []string{"USER_DEFAULT_WATERMARK"}
  53. //watermarkIdList = append(watermarkIdList, "<WatermarkId>")
  54. transcodeTemplate["WatermarkIds"] = watermarkIdList
  55. transcodeTemplateList = append(transcodeTemplateList, transcodeTemplate)
  56. return transcodeTemplateList
  57. }
  58. func main() {
  59. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  60. if err != nil {
  61. panic(err)
  62. }
  63. response, err := MyUpdateTranscodeTemplateGroup(client)
  64. if err != nil {
  65. panic(err)
  66. }
  67. fmt.Println(response.GetHttpContentString())
  68. fmt.Println(response.RequestId)
  69. }

查询转码模板组列表

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

  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 MyListTranscodeTemplateGroup(client *vod.Client) (response *vod.ListTranscodeTemplateGroupResponse, err error) {
  9. request := vod.CreateListTranscodeTemplateGroupRequest()
  10. request.AcceptFormat = "JSON"
  11. return client.ListTranscodeTemplateGroup(request)
  12. }
  13. func main() {
  14. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  15. if err != nil {
  16. panic(err)
  17. }
  18. response, err := MyListTranscodeTemplateGroup(client)
  19. if err != nil {
  20. panic(err)
  21. }
  22. fmt.Println(response.GetHttpContentString())
  23. for _, item := range response.TranscodeTemplateGroupList {
  24. fmt.Printf("%s: %s\n", item.TranscodeTemplateGroupId, item.Name)
  25. }
  26. }

查询单个转码模板组

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

  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 MyGetTranscodeTemplateGroup(client *vod.Client) (response *vod.GetTranscodeTemplateGroupResponse, err error) {
  9. request := vod.CreateGetTranscodeTemplateGroupRequest()
  10. request.TranscodeTemplateGroupId = "<TranscodeTemplateGroupId>"
  11. request.AcceptFormat = "JSON"
  12. return client.GetTranscodeTemplateGroup(request)
  13. }
  14. func main() {
  15. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  16. if err != nil {
  17. panic(err)
  18. }
  19. response, err := MyGetTranscodeTemplateGroup(client)
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Println(response.GetHttpContentString())
  24. for _, item := range response.TranscodeTemplateGroup.TranscodeTemplateList {
  25. fmt.Printf("%s: %s\n", item.TranscodeTemplateId, item.TemplateName)
  26. }
  27. }

设置默认转码模板组

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

  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 MySetDefaultTranscodeTemplateGroup(client *vod.Client) (response *vod.SetDefaultTranscodeTemplateGroupResponse, err error) {
  9. request := vod.CreateSetDefaultTranscodeTemplateGroupRequest()
  10. request.TranscodeTemplateGroupId = "<TranscodeTemplateGroupId>"
  11. request.AcceptFormat = "JSON"
  12. return client.SetDefaultTranscodeTemplateGroup(request)
  13. }
  14. func main() {
  15. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  16. if err != nil {
  17. panic(err)
  18. }
  19. response, err := MySetDefaultTranscodeTemplateGroup(client)
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Println(response.GetHttpContentString())
  24. fmt.Println(response.RequestId)
  25. }

删除转码模板组

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

  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 MyDeleteTranscodeTemplateGroup(client *vod.Client) (response *vod.DeleteTranscodeTemplateGroupResponse, err error) {
  9. request := vod.CreateDeleteTranscodeTemplateGroupRequest()
  10. request.TranscodeTemplateGroupId = "<TranscodeTemplateGroupId>"
  11. request.AcceptFormat = "JSON"
  12. return client.DeleteTranscodeTemplateGroup(request)
  13. }
  14. func main() {
  15. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  16. if err != nil {
  17. panic(err)
  18. }
  19. response, err := MyDeleteTranscodeTemplateGroup(client)
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Println(response.GetHttpContentString())
  24. fmt.Println(response.RequestId)
  25. }