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.

422 lines
15 KiB

4 years ago
4 years ago
  1. package main
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "github.com/tencentyun/cos-go-sdk-v5"
  10. "github.com/tencentyun/cos-go-sdk-v5/debug"
  11. )
  12. func log_status(err error) {
  13. if err == nil {
  14. return
  15. }
  16. if cos.IsNotFoundError(err) {
  17. // WARN
  18. fmt.Println("WARN: Resource is not existed")
  19. } else if e, ok := cos.IsCOSError(err); ok {
  20. fmt.Printf("ERROR: Code: %v\n", e.Code)
  21. fmt.Printf("ERROR: Message: %v\n", e.Message)
  22. fmt.Printf("ERROR: Resource: %v\n", e.Resource)
  23. fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
  24. // ERROR
  25. } else {
  26. fmt.Printf("ERROR: %v\n", err)
  27. // ERROR
  28. }
  29. }
  30. func InvokeSnapshotTask() {
  31. u, _ := url.Parse("https://wwj-bj-1253960454.cos.ap-beijing.myqcloud.com")
  32. cu, _ := url.Parse("https://wwj-bj-1253960454.ci.ap-beijing.myqcloud.com")
  33. b := &cos.BaseURL{BucketURL: u, CIURL: cu}
  34. c := cos.NewClient(b, &http.Client{
  35. Transport: &cos.AuthorizationTransport{
  36. SecretID: os.Getenv("COS_SECRETID"),
  37. SecretKey: os.Getenv("COS_SECRETKEY"),
  38. Transport: &debug.DebugRequestTransport{
  39. RequestHeader: true,
  40. // Notice when put a large file and set need the request body, might happend out of memory error.
  41. RequestBody: true,
  42. ResponseHeader: true,
  43. ResponseBody: true,
  44. },
  45. },
  46. })
  47. // DescribeMediaProcessQueues
  48. DescribeQueueOpt := &cos.DescribeMediaProcessQueuesOptions{
  49. QueueIds: "",
  50. PageNumber: 1,
  51. PageSize: 2,
  52. }
  53. DescribeQueueRes, _, err := c.CI.DescribeMediaProcessQueues(context.Background(), DescribeQueueOpt)
  54. log_status(err)
  55. fmt.Printf("%+v\n", DescribeQueueRes)
  56. // CreateMediaJobs
  57. createJobOpt := &cos.CreateMediaJobsOptions{
  58. Tag: "Snapshot",
  59. Input: &cos.JobInput{
  60. Object: "input/117374C.mp4",
  61. },
  62. Operation: &cos.MediaProcessJobOperation{
  63. Output: &cos.JobOutput{
  64. Region: "ap-beijing",
  65. Object: "output/abc-${Number}.jpg",
  66. Bucket: "wwj-bj-1253960454",
  67. },
  68. Snapshot: &cos.Snapshot{
  69. Mode: "Interval",
  70. Start: "0",
  71. Count: "1",
  72. },
  73. },
  74. QueueId: DescribeQueueRes.QueueList[0].QueueId,
  75. }
  76. createJobRes, _, err := c.CI.CreateMediaJobs(context.Background(), createJobOpt)
  77. log_status(err)
  78. fmt.Printf("%+v\n", createJobRes.JobsDetail)
  79. // DescribeMediaJobs
  80. DescribeJobRes, _, err := c.CI.DescribeMediaJob(context.Background(), createJobRes.JobsDetail.JobId)
  81. log_status(err)
  82. fmt.Printf("%+v\n", DescribeJobRes.JobsDetail)
  83. }
  84. func InvokeConcatTask() {
  85. u, _ := url.Parse("https://wwj-bj-1253960454.cos.ap-beijing.myqcloud.com")
  86. cu, _ := url.Parse("https://wwj-bj-1253960454.ci.ap-beijing.myqcloud.com")
  87. b := &cos.BaseURL{BucketURL: u, CIURL: cu}
  88. c := cos.NewClient(b, &http.Client{
  89. Transport: &cos.AuthorizationTransport{
  90. SecretID: os.Getenv("COS_SECRETID"),
  91. SecretKey: os.Getenv("COS_SECRETKEY"),
  92. Transport: &debug.DebugRequestTransport{
  93. RequestHeader: true,
  94. // Notice when put a large file and set need the request body, might happend out of memory error.
  95. RequestBody: true,
  96. ResponseHeader: true,
  97. ResponseBody: true,
  98. },
  99. },
  100. })
  101. // DescribeMediaProcessQueues
  102. DescribeQueueOpt := &cos.DescribeMediaProcessQueuesOptions{
  103. QueueIds: "",
  104. PageNumber: 1,
  105. PageSize: 2,
  106. }
  107. DescribeQueueRes, _, err := c.CI.DescribeMediaProcessQueues(context.Background(), DescribeQueueOpt)
  108. log_status(err)
  109. fmt.Printf("%+v\n", DescribeQueueRes)
  110. // CreateMediaJobs
  111. concatFragment := make([]cos.ConcatFragment, 0)
  112. concatFragment = append(concatFragment, cos.ConcatFragment{
  113. Url: "https://wwj-bj-1253960454.cos.ap-beijing.myqcloud.com/input/117374C.mp4",
  114. StartTime: "0",
  115. EndTime: "10",
  116. })
  117. concatFragment = append(concatFragment, cos.ConcatFragment{
  118. Url: "https://wwj-bj-1253960454.cos.ap-beijing.myqcloud.com/input/117374C.mp4",
  119. StartTime: "20",
  120. EndTime: "30",
  121. })
  122. createJobOpt := &cos.CreateMediaJobsOptions{
  123. Tag: "Concat",
  124. Operation: &cos.MediaProcessJobOperation{
  125. Output: &cos.JobOutput{
  126. Region: "ap-beijing",
  127. Object: "output/go_117374C.mp4",
  128. Bucket: "wwj-bj-1253960454",
  129. },
  130. ConcatTemplate: &cos.ConcatTemplate{
  131. Container: &cos.Container{
  132. Format: "mp4",
  133. },
  134. Video: &cos.Video{
  135. Codec: "H.265",
  136. },
  137. Audio: &cos.Audio{
  138. //Codec: "AAC",
  139. },
  140. ConcatFragment: concatFragment,
  141. },
  142. },
  143. QueueId: DescribeQueueRes.QueueList[0].QueueId,
  144. }
  145. createJobRes, _, err := c.CI.CreateMediaJobs(context.Background(), createJobOpt)
  146. log_status(err)
  147. fmt.Printf("%+v\n", createJobRes.JobsDetail)
  148. // DescribeMediaJobs
  149. DescribeJobRes, _, err := c.CI.DescribeMediaJob(context.Background(), createJobRes.JobsDetail.JobId)
  150. log_status(err)
  151. fmt.Printf("%+v\n", DescribeJobRes.JobsDetail)
  152. }
  153. func InvokeTranscodeTask() {
  154. u, _ := url.Parse("https://wwj-cq-1253960454.cos.ap-chongqing.myqcloud.com")
  155. cu, _ := url.Parse("https://wwj-cq-1253960454.ci.ap-chongqing.myqcloud.com")
  156. b := &cos.BaseURL{BucketURL: u, CIURL: cu}
  157. c := cos.NewClient(b, &http.Client{
  158. Transport: &cos.AuthorizationTransport{
  159. SecretID: os.Getenv("COS_SECRETID"),
  160. SecretKey: os.Getenv("COS_SECRETKEY"),
  161. Transport: &debug.DebugRequestTransport{
  162. RequestHeader: true,
  163. // Notice when put a large file and set need the request body, might happend out of memory error.
  164. RequestBody: true,
  165. ResponseHeader: true,
  166. ResponseBody: true,
  167. },
  168. },
  169. })
  170. // DescribeMediaProcessQueues
  171. DescribeQueueOpt := &cos.DescribeMediaProcessQueuesOptions{
  172. QueueIds: "",
  173. PageNumber: 1,
  174. PageSize: 2,
  175. }
  176. DescribeQueueRes, _, err := c.CI.DescribeMediaProcessQueues(context.Background(), DescribeQueueOpt)
  177. log_status(err)
  178. fmt.Printf("%+v\n", DescribeQueueRes)
  179. // CreateMediaJobs
  180. createJobOpt := &cos.CreateMediaJobsOptions{
  181. Tag: "Transcode",
  182. Input: &cos.JobInput{
  183. Object: "input/117374C.mp4",
  184. },
  185. Operation: &cos.MediaProcessJobOperation{
  186. Output: &cos.JobOutput{
  187. Region: "ap-chongqing",
  188. Object: "output/go_117374C.mp4",
  189. Bucket: "wwj-cq-1253960454",
  190. },
  191. Transcode: &cos.Transcode{
  192. Container: &cos.Container{
  193. Format: "mp4",
  194. },
  195. Video: &cos.Video{
  196. Codec: "H.264",
  197. },
  198. Audio: &cos.Audio{
  199. Codec: "AAC",
  200. },
  201. TimeInterval: &cos.TimeInterval{
  202. Start: "10",
  203. Duration: "",
  204. },
  205. },
  206. },
  207. QueueId: "paaf4fce5521a40888a3034a5de80f6ca",
  208. }
  209. createJobRes, _, err := c.CI.CreateMediaJobs(context.Background(), createJobOpt)
  210. log_status(err)
  211. fmt.Printf("%+v\n", createJobRes.JobsDetail)
  212. // DescribeMediaJobs
  213. DescribeJobRes, _, err := c.CI.DescribeMediaJob(context.Background(), createJobRes.JobsDetail.JobId)
  214. log_status(err)
  215. fmt.Printf("%+v\n", DescribeJobRes.JobsDetail)
  216. }
  217. func InvokeMultiTasks() {
  218. u, _ := url.Parse("https://wwj-cq-1253960454.cos.ap-chongqing.myqcloud.com")
  219. cu, _ := url.Parse("https://wwj-cq-1253960454.ci.ap-chongqing.myqcloud.com")
  220. b := &cos.BaseURL{BucketURL: u, CIURL: cu}
  221. c := cos.NewClient(b, &http.Client{
  222. Transport: &cos.AuthorizationTransport{
  223. SecretID: os.Getenv("COS_SECRETID"),
  224. SecretKey: os.Getenv("COS_SECRETKEY"),
  225. Transport: &debug.DebugRequestTransport{
  226. RequestHeader: true,
  227. // Notice when put a large file and set need the request body, might happend out of memory error.
  228. RequestBody: true,
  229. ResponseHeader: true,
  230. ResponseBody: true,
  231. },
  232. },
  233. })
  234. // DescribeMediaProcessQueues
  235. DescribeQueueOpt := &cos.DescribeMediaProcessQueuesOptions{
  236. QueueIds: "",
  237. PageNumber: 1,
  238. PageSize: 2,
  239. }
  240. DescribeQueueRes, _, err := c.CI.DescribeMediaProcessQueues(context.Background(), DescribeQueueOpt)
  241. log_status(err)
  242. fmt.Printf("%+v\n", DescribeQueueRes)
  243. // CreateMediaJobs
  244. createJobOpt := &cos.CreateMultiMediaJobsOptions{
  245. Input: &cos.JobInput{
  246. Object: "input/117374C.mp4",
  247. },
  248. Operation: []cos.MediaProcessJobOperation{
  249. cos.MediaProcessJobOperation{
  250. Tag: "Snapshot",
  251. Output: &cos.JobOutput{
  252. Region: "ap-chongqing",
  253. Object: "output/go_${Number}.mp4",
  254. Bucket: "wwj-cq-1253960454",
  255. },
  256. Snapshot: &cos.Snapshot{
  257. Mode: "Interval",
  258. Start: "0",
  259. Count: "1",
  260. },
  261. },
  262. cos.MediaProcessJobOperation{
  263. Tag: "Transcode",
  264. Output: &cos.JobOutput{
  265. Region: "ap-chongqing",
  266. Object: "output/go_117374C.mp4",
  267. Bucket: "wwj-cq-1253960454",
  268. },
  269. Transcode: &cos.Transcode{
  270. Container: &cos.Container{
  271. Format: "mp4",
  272. },
  273. Video: &cos.Video{
  274. Codec: "H.264",
  275. },
  276. Audio: &cos.Audio{
  277. Codec: "AAC",
  278. },
  279. TimeInterval: &cos.TimeInterval{
  280. Start: "10",
  281. Duration: "",
  282. },
  283. },
  284. },
  285. cos.MediaProcessJobOperation{
  286. Tag: "Animation",
  287. Output: &cos.JobOutput{
  288. Region: "ap-chongqing",
  289. Object: "output/go_117374C.gif",
  290. Bucket: "wwj-cq-1253960454",
  291. },
  292. Animation: &cos.Animation{
  293. Container: &cos.Container{
  294. Format: "gif",
  295. },
  296. Video: &cos.AnimationVideo{
  297. Codec: "gif",
  298. AnimateOnlyKeepKeyFrame: "true",
  299. },
  300. TimeInterval: &cos.TimeInterval{
  301. Start: "0",
  302. Duration: "",
  303. },
  304. },
  305. },
  306. },
  307. QueueId: "paaf4fce5521a40888a3034a5de80f6ca",
  308. }
  309. createJobRes, _, err := c.CI.CreateMultiMediaJobs(context.Background(), createJobOpt)
  310. log_status(err)
  311. for k, job := range createJobRes.JobsDetail {
  312. fmt.Printf("job:%d, %+v\n", k, job)
  313. }
  314. }
  315. func TaskNotifyCallback() {
  316. taskBody := "<Response><EventName>TaskFinish</EventName><JobsDetail><Code>Success</Code><CreationTime>2021-09-14T14:38:59+0800</CreationTime><EndTime>2021-09-14T14:39:59+0800</EndTime><Input><BucketId></BucketId><Object>e28ef88b61ed41b7a6ea76a8147f4f9e</Object><Region>ap-guangzhou</Region></Input><JobId>j7123e78c152611ec9e8c1ba6632b91a8</JobId><Message/><Operation><MediaInfo><Format><Bitrate>3775.738000</Bitrate><Duration>143.732000</Duration><FormatLongName>QuickTime / MOV</FormatLongName><FormatName>mov,mp4,m4a,3gp,3g2,mj2</FormatName><NumProgram>0</NumProgram><NumStream>2</NumStream><Size>67836813</Size><StartTime>0.000000</StartTime></Format><Stream><Audio><Bitrate>125.049000</Bitrate><Channel>2</Channel><ChannelLayout>stereo</ChannelLayout><CodecLongName>AAC (Advanced Audio Coding)</CodecLongName><CodecName>aac</CodecName><CodecTag>0x6134706d</CodecTag><CodecTagString>mp4a</CodecTagString><CodecTimeBase>1/44100</CodecTimeBase><Duration>143.730998</Duration><Index>1</Index><Language>und</Language><SampleFmt>fltp</SampleFmt><SampleRate>44100</SampleRate><StartTime>0.000000</StartTime><Timebase>1/44100</Timebase></Audio><Subtitle/><Video><AvgFps>25/1</AvgFps><Bitrate>3645.417000</Bitrate><CodecLongName>H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10</CodecLongName><CodecName>h264</CodecName><CodecTag>0x31637661</CodecTag><CodecTagString>avc1</CodecTagString><CodecTimeBase>1/12800</CodecTimeBase><Dar>9:16</Dar><Duration>143.680000</Duration><Fps>25.500000</Fps><HasBFrame>2</HasBFrame><Height>3412</Height><Index>0</Index><Language>und</Language><Level>51</Level><NumFrames>3592</NumFrames><PixFormat>yuv420p</PixFormat><Profile>High</Profile><RefFrames>1</RefFrames><Rotation>0.000000</Rotation><Sar>2559:2560</Sar><StartTime>0.000000</StartTime><Timebase>1/12800</Timebase><Width>1920</Width></Video></Stream></MediaInfo><MediaResult><OutputFile><Bucket></Bucket><ObjectName>f89f22f7a8be4f478434da58eb11d7da.mp4</ObjectName><ObjectPrefix/><Region>ap-guangzhou</Region></OutputFile></MediaResult><Output><Bucket></Bucket><Object>f89f22f7a8be4f478434da58eb11d7da.mp4</Object><Region>ap-guangzhou</Region></Output><TemplateId>t064fb9214850f49aaac44b5561a7b0b3b</TemplateId><TemplateName>MP4-FHD</TemplateName></Operation><QueueId>p6f358a37bf9442ad8f859db055cd0edb</QueueId><StartTime>2021-09-14T14:38:59+0800</StartTime><State>Success</State><Tag>Transcode</Tag></JobsDetail></Response>"
  317. var body cos.MediaProcessJobsNotifyBody
  318. err := xml.Unmarshal([]byte(taskBody), &body)
  319. if err != nil {
  320. fmt.Println(fmt.Sprintf("err:%v", err))
  321. } else {
  322. fmt.Println(fmt.Sprintf("body:%v", body))
  323. }
  324. }
  325. func WorkflowExecutionNotifyCallback() {
  326. workflowExecutionBody := "<Response><EventName>WorkflowFinish</EventName><WorkflowExecution><RunId>i70ae991a152911ecb184525400a8700f</RunId><BucketId></BucketId><Object>62ddbc1245.mp4</Object><CosHeaders><Key>x-cos-meta-id</Key><Value>62ddbc1245</Value></CosHeaders><CosHeaders><Key>Content-Type</Key><Value>video/mp4</Value></CosHeaders><WorkflowId>w29ba54d02b7340dd9fb44eb5beb786b9</WorkflowId><WorkflowName></WorkflowName><CreateTime>2021-09-14 15:00:26+0800</CreateTime><State>Success</State><Tasks><Type>Transcode</Type><CreateTime>2021-09-14 15:00:27+0800</CreateTime><EndTime>2021-09-14 15:00:42+0800</EndTime><State>Success</State><JobId>j70bab192152911ecab79bba409874f7f</JobId><Name>Transcode_1607323983818</Name><TemplateId>t088613dea8d564a9ba7e6b02cbd5de877</TemplateId><TemplateName>HLS-FHD</TemplateName></Tasks></WorkflowExecution></Response>"
  327. var body cos.WorkflowExecutionNotifyBody
  328. err := xml.Unmarshal([]byte(workflowExecutionBody), &body)
  329. if err != nil {
  330. fmt.Println(fmt.Sprintf("err:%v", err))
  331. } else {
  332. fmt.Println(fmt.Sprintf("body:%v", body))
  333. }
  334. }
  335. func InvokeSpriteSnapshotTask() {
  336. u, _ := url.Parse("https://wwj-cq-1253960454.cos.ap-chongqing.myqcloud.com")
  337. cu, _ := url.Parse("https://wwj-cq-1253960454.ci.ap-chongqing.myqcloud.com")
  338. b := &cos.BaseURL{BucketURL: u, CIURL: cu}
  339. c := cos.NewClient(b, &http.Client{
  340. Transport: &cos.AuthorizationTransport{
  341. SecretID: os.Getenv("COS_SECRETID"),
  342. SecretKey: os.Getenv("COS_SECRETKEY"),
  343. Transport: &debug.DebugRequestTransport{
  344. RequestHeader: true,
  345. // Notice when put a large file and set need the request body, might happend out of memory error.
  346. RequestBody: true,
  347. ResponseHeader: true,
  348. ResponseBody: true,
  349. },
  350. },
  351. })
  352. // DescribeMediaProcessQueues
  353. DescribeQueueOpt := &cos.DescribeMediaProcessQueuesOptions{
  354. QueueIds: "",
  355. PageNumber: 1,
  356. PageSize: 2,
  357. }
  358. DescribeQueueRes, _, err := c.CI.DescribeMediaProcessQueues(context.Background(), DescribeQueueOpt)
  359. log_status(err)
  360. fmt.Printf("%+v\n", DescribeQueueRes)
  361. // CreateMediaJobs
  362. createJobOpt := &cos.CreateMediaJobsOptions{
  363. Tag: "Snapshot",
  364. Input: &cos.JobInput{
  365. Object: "input/117374C.mp4",
  366. },
  367. Operation: &cos.MediaProcessJobOperation{
  368. Output: &cos.JobOutput{
  369. Region: "ap-chongqing",
  370. Object: "output/abc-${Number}.jpg",
  371. Bucket: "wwj-cq-1253960454",
  372. SpriteObject: "output/sprite-${Number}.jpg",
  373. },
  374. Snapshot: &cos.Snapshot{
  375. Mode: "Interval",
  376. Start: "0",
  377. Count: "100",
  378. SnapshotOutMode: "SnapshotAndSprite", // OnlySnapshot OnlySprite
  379. SpriteSnapshotConfig: &cos.SpriteSnapshotConfig{
  380. CellHeight: "128",
  381. CellWidth: "128",
  382. Color: "Black",
  383. Columns: "3",
  384. Lines: "10",
  385. Margin: "2",
  386. },
  387. },
  388. },
  389. QueueId: DescribeQueueRes.QueueList[0].QueueId,
  390. }
  391. createJobRes, _, err := c.CI.CreateMediaJobs(context.Background(), createJobOpt)
  392. log_status(err)
  393. fmt.Printf("%+v\n", createJobRes.JobsDetail)
  394. // DescribeMediaJobs
  395. DescribeJobRes, _, err := c.CI.DescribeMediaJob(context.Background(), createJobRes.JobsDetail.JobId)
  396. log_status(err)
  397. fmt.Printf("%+v\n", DescribeJobRes.JobsDetail)
  398. }
  399. func main() {
  400. // InvokeSnapshotTask()
  401. // InvokeConcatTask()
  402. // InvokeTranscodeTask()
  403. // InvokeMultiTasks()
  404. // TaskNotifyCallback()
  405. // WorkflowExecutionNotifyCallback()
  406. InvokeSpriteSnapshotTask()
  407. }