You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.8 KiB

4 years ago
4 years ago
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "os"
  8. "github.com/tencentyun/cos-go-sdk-v5"
  9. "github.com/tencentyun/cos-go-sdk-v5/debug"
  10. )
  11. func log_status(err error) {
  12. if err == nil {
  13. return
  14. }
  15. if cos.IsNotFoundError(err) {
  16. // WARN
  17. fmt.Println("WARN: Resource is not existed")
  18. } else if e, ok := cos.IsCOSError(err); ok {
  19. fmt.Printf("ERROR: Code: %v\n", e.Code)
  20. fmt.Printf("ERROR: Message: %v\n", e.Message)
  21. fmt.Printf("ERROR: Resource: %v\n", e.Resource)
  22. fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
  23. // ERROR
  24. } else {
  25. fmt.Printf("ERROR: %v\n", err)
  26. // ERROR
  27. }
  28. }
  29. func InvokeConcatTask() {
  30. u, _ := url.Parse("https://wwj-bj-1253960454.cos.ap-beijing.myqcloud.com")
  31. cu, _ := url.Parse("https://wwj-bj-1253960454.ci.ap-beijing.myqcloud.com")
  32. b := &cos.BaseURL{BucketURL: u, CIURL: cu}
  33. c := cos.NewClient(b, &http.Client{
  34. Transport: &cos.AuthorizationTransport{
  35. SecretID: os.Getenv("COS_SECRETID"),
  36. SecretKey: os.Getenv("COS_SECRETKEY"),
  37. Transport: &debug.DebugRequestTransport{
  38. RequestHeader: true,
  39. // Notice when put a large file and set need the request body, might happend out of memory error.
  40. RequestBody: true,
  41. ResponseHeader: true,
  42. ResponseBody: true,
  43. },
  44. },
  45. })
  46. // DescribeMediaProcessQueues
  47. DescribeQueueOpt := &cos.DescribeMediaProcessQueuesOptions{
  48. QueueIds: "",
  49. PageNumber: 1,
  50. PageSize: 2,
  51. }
  52. DescribeQueueRes, _, err := c.CI.DescribeMediaProcessQueues(context.Background(), DescribeQueueOpt)
  53. log_status(err)
  54. fmt.Printf("%+v\n", DescribeQueueRes)
  55. // CreateMediaJobs
  56. concatFragment := make([]cos.ConcatFragment, 0)
  57. concatFragment = append(concatFragment, cos.ConcatFragment{
  58. Url: "https://wwj-bj-1253960454.cos.ap-beijing.myqcloud.com/input/117374C.mp4",
  59. StartTime: "0",
  60. EndTime: "10",
  61. })
  62. concatFragment = append(concatFragment, cos.ConcatFragment{
  63. Url: "https://wwj-bj-1253960454.cos.ap-beijing.myqcloud.com/input/117374C.mp4",
  64. StartTime: "20",
  65. EndTime: "30",
  66. })
  67. createJobOpt := &cos.CreateMediaJobsOptions{
  68. Tag: "Concat",
  69. Operation: &cos.MediaProcessJobOperation{
  70. Output: &cos.JobOutput{
  71. Region: "ap-beijing",
  72. Object: "output/go_117374C.mp4",
  73. Bucket: "wwj-bj-1253960454",
  74. },
  75. ConcatTemplate: &cos.ConcatTemplate{
  76. Container: &cos.Container{
  77. Format: "mp4",
  78. },
  79. Video: &cos.Video{
  80. Codec: "H.264",
  81. },
  82. Audio: &cos.Audio{
  83. Codec: "AAC",
  84. },
  85. ConcatFragment:concatFragment,
  86. },
  87. },
  88. QueueId: DescribeQueueRes.QueueList[0].QueueId,
  89. }
  90. createJobRes, _, err := c.CI.CreateMediaJobs(context.Background(), createJobOpt)
  91. log_status(err)
  92. fmt.Printf("%+v\n", createJobRes.JobsDetail)
  93. // DescribeMediaJobs
  94. DescribeJobRes, _, err := c.CI.DescribeMediaJob(context.Background(), createJobRes.JobsDetail.JobId)
  95. log_status(err)
  96. fmt.Printf("%+v\n", DescribeJobRes.JobsDetail)
  97. }
  98. func InvokeTranscodeTask() {
  99. u, _ := url.Parse("https://wwj-cq-1253960454.cos.ap-chongqing.myqcloud.com")
  100. cu, _ := url.Parse("https://wwj-cq-1253960454.ci.ap-chongqing.myqcloud.com")
  101. b := &cos.BaseURL{BucketURL: u, CIURL: cu}
  102. c := cos.NewClient(b, &http.Client{
  103. Transport: &cos.AuthorizationTransport{
  104. SecretID: os.Getenv("COS_SECRETID"),
  105. SecretKey: os.Getenv("COS_SECRETKEY"),
  106. Transport: &debug.DebugRequestTransport{
  107. RequestHeader: true,
  108. // Notice when put a large file and set need the request body, might happend out of memory error.
  109. RequestBody: true,
  110. ResponseHeader: true,
  111. ResponseBody: true,
  112. },
  113. },
  114. })
  115. // DescribeMediaProcessQueues
  116. DescribeQueueOpt := &cos.DescribeMediaProcessQueuesOptions{
  117. QueueIds: "",
  118. PageNumber: 1,
  119. PageSize: 2,
  120. }
  121. DescribeQueueRes, _, err := c.CI.DescribeMediaProcessQueues(context.Background(), DescribeQueueOpt)
  122. log_status(err)
  123. fmt.Printf("%+v\n", DescribeQueueRes)
  124. // CreateMediaJobs
  125. createJobOpt := &cos.CreateMediaJobsOptions{
  126. Tag: "Transcode",
  127. Input: &cos.JobInput{
  128. Object: "input/117374C.mp4",
  129. },
  130. Operation: &cos.MediaProcessJobOperation{
  131. Output: &cos.JobOutput{
  132. Region: "ap-chongqing",
  133. Object: "output/go_117374C.mp4",
  134. Bucket: "wwj-cq-1253960454",
  135. },
  136. Transcode: &cos.Transcode{
  137. Container: &cos.Container{
  138. Format: "mp4",
  139. },
  140. Video: &cos.Video{
  141. Codec: "H.264",
  142. },
  143. Audio: &cos.Audio{
  144. Codec: "AAC",
  145. },
  146. TimeInterval: &cos.TimeInterval{
  147. Start: "10",
  148. Duration: "",
  149. },
  150. },
  151. },
  152. QueueId: "paaf4fce5521a40888a3034a5de80f6ca",
  153. }
  154. createJobRes, _, err := c.CI.CreateMediaJobs(context.Background(), createJobOpt)
  155. log_status(err)
  156. fmt.Printf("%+v\n", createJobRes.JobsDetail)
  157. // DescribeMediaJobs
  158. DescribeJobRes, _, err := c.CI.DescribeMediaJob(context.Background(), createJobRes.JobsDetail.JobId)
  159. log_status(err)
  160. fmt.Printf("%+v\n", DescribeJobRes.JobsDetail)
  161. }
  162. func main() {
  163. InvokeConcatTask()
  164. //InvokeTranscodeTask()
  165. }