Skip to content

Commit 50c32c4

Browse files
authored
Merge pull request #159 from agin719/cos-v4-dev
add CI 媒体处理接口
2 parents 7593b9e + 08b65a1 commit 50c32c4

File tree

4 files changed

+298
-0
lines changed

4 files changed

+298
-0
lines changed

ci_media.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cos
33
import (
44
"context"
55
"encoding/xml"
6+
"fmt"
67
"net/http"
78
)
89

@@ -506,6 +507,7 @@ type MediaProcessBucket struct {
506507
CreateTime string `xml:"CreateTime,omitempty"`
507508
}
508509

510+
// 媒体bucket接口 https://cloud.tencent.com/document/product/436/48988
509511
func (s *CIService) DescribeMediaProcessBuckets(ctx context.Context, opt *DescribeMediaProcessBucketsOptions) (*DescribeMediaProcessBucketsResult, *Response, error) {
510512
var res DescribeMediaProcessBucketsResult
511513
sendOpt := sendOptions{
@@ -518,3 +520,123 @@ func (s *CIService) DescribeMediaProcessBuckets(ctx context.Context, opt *Descri
518520
resp, err := s.client.send(ctx, &sendOpt)
519521
return &res, resp, err
520522
}
523+
524+
type GetMediaInfoResult struct {
525+
XMLName xml.Name `xml:"Response"`
526+
MediaInfo struct {
527+
Format struct {
528+
Bitrate float32 `xml:"Bitrate"`
529+
Duration float32 `xml:"Duration"`
530+
FormatLongName string `xml:"FormatLongName"`
531+
FormatName string `xml:"FormatName"`
532+
NumProgram int `xml:"NumProgram"`
533+
NumStream int `xml:"NumStream"`
534+
Size int `xml:"Size"`
535+
StartTime float32 `xml:"StartTime"`
536+
} `xml:"Format"`
537+
Stream struct {
538+
Audio []struct {
539+
Index int `xml:"Index"`
540+
CodecName string `xml:"CodecName"`
541+
CodecLongName string `xml:"CodecLongName"`
542+
CodecTimeBase string `xml:"CodecTimeBase"`
543+
CodecTagString string `xml:"CodecTagString"`
544+
CodecTag string `xml:"CodecTag"`
545+
SampleFmt string `xml:"SampleFmt"`
546+
SampleRate int `xml:"SampleRate"`
547+
Channel int `xml:"Channel"`
548+
ChannelLayout string `xml:"ChannelLayout"`
549+
Timebase string `xml:"Timebase"`
550+
StartTime float32 `xml:"StartTime"`
551+
Duration float32 `xml:"Duration"`
552+
Bitrate float32 `xml:"Bitrate"`
553+
Language string `xml:"Language"`
554+
} `xml:"Audio"`
555+
Subtitle struct {
556+
Index int `xml:"Index"`
557+
Language string `xml:"Language"`
558+
} `xml:"Subtitle"`
559+
Video struct {
560+
Index int `xml:"Index"`
561+
CodecName string `xml:"CodecName"`
562+
CodecLongName string `xml:"CodecLongName"`
563+
CodecTimeBase string `xml:"CodecTimeBase"`
564+
CodecTagString string `xml:"CodecTagString"`
565+
CodecTag string `xml:"CodecTag"`
566+
Profile string `xml:"Profile"`
567+
Height int `xml:"Height"`
568+
Width int `xml:"Width"`
569+
HasBFrame int `xml:"HasBFrame"`
570+
RefFrames int `xml:"RefFrames"`
571+
Sar string `xml:"Sar"`
572+
Dar string `xml:"Dar"`
573+
PixFormat string `xml:"PixFormat"`
574+
FieldOrder string `xml:"FieldOrder"`
575+
Level int `xml:"Level"`
576+
Fps float32 `xml:"Fps"`
577+
AvgFps string `xml:"AvgFps"`
578+
Timebase string `xml:"Timebase"`
579+
StartTime float32 `xml:"StartTime"`
580+
Duration float32 `xml:"Duration"`
581+
Bitrate float32 `xml:"Bitrate"`
582+
NumFrames int `xml:"NumFrames"`
583+
Language string `xml:"Language"`
584+
} `xml:"Video"`
585+
} `xml:"Stream"`
586+
} `xml:"MediaInfo"`
587+
}
588+
589+
// 媒体信息接口 https://cloud.tencent.com/document/product/436/55672
590+
func (s *CIService) GetMediaInfo(ctx context.Context, name string, opt *ObjectGetOptions, id ...string) (*GetMediaInfoResult, *Response, error) {
591+
var u string
592+
if len(id) == 1 {
593+
u = fmt.Sprintf("/%s?versionId=%s&ci-process=videoinfo", encodeURIComponent(name), id[0])
594+
} else if len(id) == 0 {
595+
u = fmt.Sprintf("/%s?ci-process=videoinfo", encodeURIComponent(name))
596+
} else {
597+
return nil, nil, fmt.Errorf("wrong params")
598+
}
599+
600+
var res GetMediaInfoResult
601+
sendOpt := sendOptions{
602+
baseURL: s.client.BaseURL.BucketURL,
603+
uri: u,
604+
method: http.MethodGet,
605+
optQuery: opt,
606+
optHeader: opt,
607+
result: &res,
608+
}
609+
resp, err := s.client.send(ctx, &sendOpt)
610+
return &res, resp, err
611+
}
612+
613+
type GetSnapshotOptions struct {
614+
Time float32 `url:"time,omitempty"`
615+
Height int `url:"height,omitempty"`
616+
Width int `url:"width,omitempty"`
617+
Format string `url:"format,omitempty"`
618+
Rotate string `url:"rotate,omitempty"`
619+
Mode string `url:"mode,omitempty"`
620+
}
621+
622+
// 媒体截图接口 https://cloud.tencent.com/document/product/436/55671
623+
func (s *CIService) GetSnapshot(ctx context.Context, name string, opt *GetSnapshotOptions, id ...string) (*Response, error) {
624+
var u string
625+
if len(id) == 1 {
626+
u = fmt.Sprintf("/%s?versionId=%s&ci-process=snapshot", encodeURIComponent(name), id[0])
627+
} else if len(id) == 0 {
628+
u = fmt.Sprintf("/%s?ci-process=snapshot", encodeURIComponent(name))
629+
} else {
630+
return nil, fmt.Errorf("wrong params")
631+
}
632+
633+
sendOpt := sendOptions{
634+
baseURL: s.client.BaseURL.BucketURL,
635+
uri: u,
636+
method: http.MethodGet,
637+
optQuery: opt,
638+
disableCloseBody: true,
639+
}
640+
resp, err := s.client.send(ctx, &sendOpt)
641+
return resp, err
642+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"os"
9+
10+
"github.com/tencentyun/cos-go-sdk-v5"
11+
"github.com/tencentyun/cos-go-sdk-v5/debug"
12+
)
13+
14+
func log_status(err error) {
15+
if err == nil {
16+
return
17+
}
18+
if cos.IsNotFoundError(err) {
19+
// WARN
20+
fmt.Println("WARN: Resource is not existed")
21+
} else if e, ok := cos.IsCOSError(err); ok {
22+
fmt.Printf("ERROR: Code: %v\n", e.Code)
23+
fmt.Printf("ERROR: Message: %v\n", e.Message)
24+
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25+
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26+
// ERROR
27+
} else {
28+
fmt.Printf("ERROR: %v\n", err)
29+
// ERROR
30+
}
31+
}
32+
33+
func main() {
34+
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35+
// DescirbeMediaBuckets 需要设置 CIURL 为 ci.<Region>.myqcloud.com
36+
cu, _ := url.Parse("https://ci.ap-guangzhou.myqcloud.com")
37+
b := &cos.BaseURL{BucketURL: u, CIURL: cu}
38+
c := cos.NewClient(b, &http.Client{
39+
Transport: &cos.AuthorizationTransport{
40+
SecretID: os.Getenv("COS_SECRETID"),
41+
SecretKey: os.Getenv("COS_SECRETKEY"),
42+
Transport: &debug.DebugRequestTransport{
43+
RequestHeader: true,
44+
// Notice when put a large file and set need the request body, might happend out of memory error.
45+
RequestBody: true,
46+
ResponseHeader: true,
47+
ResponseBody: true,
48+
},
49+
},
50+
})
51+
52+
opt := &cos.DescribeMediaProcessBucketsOptions{
53+
Regions: "ap-guangzhou",
54+
}
55+
res, _, err := c.CI.DescribeMediaProcessBuckets(context.Background(), opt)
56+
log_status(err)
57+
fmt.Printf("res: %+v\n", res)
58+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"os"
9+
10+
"github.com/tencentyun/cos-go-sdk-v5"
11+
"github.com/tencentyun/cos-go-sdk-v5/debug"
12+
)
13+
14+
func log_status(err error) {
15+
if err == nil {
16+
return
17+
}
18+
if cos.IsNotFoundError(err) {
19+
// WARN
20+
fmt.Println("WARN: Resource is not existed")
21+
} else if e, ok := cos.IsCOSError(err); ok {
22+
fmt.Printf("ERROR: Code: %v\n", e.Code)
23+
fmt.Printf("ERROR: Message: %v\n", e.Message)
24+
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25+
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26+
// ERROR
27+
} else {
28+
fmt.Printf("ERROR: %v\n", err)
29+
// ERROR
30+
}
31+
}
32+
33+
func main() {
34+
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35+
b := &cos.BaseURL{BucketURL: u}
36+
c := cos.NewClient(b, &http.Client{
37+
Transport: &cos.AuthorizationTransport{
38+
SecretID: os.Getenv("COS_SECRETID"),
39+
SecretKey: os.Getenv("COS_SECRETKEY"),
40+
Transport: &debug.DebugRequestTransport{
41+
RequestHeader: true,
42+
// Notice when put a large file and set need the request body, might happend out of memory error.
43+
RequestBody: true,
44+
ResponseHeader: true,
45+
ResponseBody: true,
46+
},
47+
},
48+
})
49+
50+
res, _, err := c.CI.GetMediaInfo(context.Background(), "test.mp4", nil)
51+
log_status(err)
52+
fmt.Printf("res: %+v\n", res)
53+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"net/url"
9+
"os"
10+
11+
"github.com/tencentyun/cos-go-sdk-v5"
12+
"github.com/tencentyun/cos-go-sdk-v5/debug"
13+
)
14+
15+
func log_status(err error) {
16+
if err == nil {
17+
return
18+
}
19+
if cos.IsNotFoundError(err) {
20+
// WARN
21+
fmt.Println("WARN: Resource is not existed")
22+
} else if e, ok := cos.IsCOSError(err); ok {
23+
fmt.Printf("ERROR: Code: %v\n", e.Code)
24+
fmt.Printf("ERROR: Message: %v\n", e.Message)
25+
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26+
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27+
// ERROR
28+
} else {
29+
fmt.Printf("ERROR: %v\n", err)
30+
// ERROR
31+
}
32+
}
33+
34+
func main() {
35+
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
36+
b := &cos.BaseURL{BucketURL: u}
37+
c := cos.NewClient(b, &http.Client{
38+
Transport: &cos.AuthorizationTransport{
39+
SecretID: os.Getenv("COS_SECRETID"),
40+
SecretKey: os.Getenv("COS_SECRETKEY"),
41+
Transport: &debug.DebugRequestTransport{
42+
RequestHeader: true,
43+
// Notice when put a large file and set need the request body, might happend out of memory error.
44+
RequestBody: true,
45+
ResponseHeader: true,
46+
ResponseBody: false,
47+
},
48+
},
49+
})
50+
51+
opt := &cos.GetSnapshotOptions{
52+
Time: 1,
53+
}
54+
resp, err := c.CI.GetSnapshot(context.Background(), "test.mp4", opt)
55+
log_status(err)
56+
defer resp.Body.Close()
57+
58+
fd, err := os.OpenFile("test.jpg", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
59+
if err != nil {
60+
log_status(err)
61+
}
62+
_, err = io.Copy(fd, resp.Body)
63+
fd.Close()
64+
log_status(err)
65+
}

0 commit comments

Comments
 (0)