vod.go
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package aliyun
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
"github.com/astaxie/beego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time"
"opp/internal/utils"
"path"
"time"
)
//客户端
func DefaultVodClient() (client *vod.Client, err error) {
return InitVodClient(AccessKeyID, AccessKeySecret)
}
//初始化客户端
func InitVodClient(accessKeyId string, accessKeySecret string) (client *vod.Client, err error) {
// 点播服务接入区域
regionId := RegionID
// 创建授权对象
credential := &credentials.AccessKeyCredential{
accessKeyId,
accessKeySecret,
}
// 自定义config
config := sdk.NewConfig()
config.AutoRetry = true // 失败是否自动重试
config.MaxRetryTime = 3 // 最大重试次数
config.Timeout = 3000000000 // 连接超时,单位:纳秒;默认为3秒
// 创建vodClient实例
return vod.NewClientWithOptions(regionId, config, credential)
}
//获取视频上传地址和凭证,并创建视频信息
func CreateUploadVideo(client *vod.Client, r *CreateUploadVideoRequest) (response *CreateUploadVideoResponse, err error) {
request := vod.CreateCreateUploadVideoRequest()
request.Title = getFileName(FileVideo, "video_file.mp4")
request.FileName = getFileName(FileVideo, "video_file.mp4")
//request.CoverURL = "http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png"
//request.Tags = "tag1,tag2"
request.AcceptFormat = "JSON"
rsp, err := client.CreateUploadVideo(request)
if err != nil {
return
}
response = &CreateUploadVideoResponse{
RequestId: rsp.RequestId,
VideoId: rsp.VideoId,
UploadAddress: rsp.UploadAddress,
UploadAuth: rsp.UploadAuth,
}
return
}
//获取图片上传地址和凭证,并创建视频信息
func CreateUploadImage(client *vod.Client, r *CreateUploadImageRequest) (response *CreateUploadImageResponse, err error) {
request := vod.CreateCreateUploadImageRequest()
request.ImageType = "default"
request.AcceptFormat = "JSON"
rsp, err := client.CreateUploadImage(request)
if err != nil {
return
}
err = utils.JsonDeepCopy(&response, &rsp)
return
}
//获取播放信息
func GetPlayInfo(client *vod.Client, videoId string) (response *vod.GetPlayInfoResponse, err error) {
request := vod.CreateGetPlayInfoRequest()
request.VideoId = videoId
request.AcceptFormat = "JSON"
return client.GetPlayInfo(request)
}
//fileType: video voice image
func getFileName(fileType string, filename string) string {
date := comm_time.GetTimeByYyyymmdd()
subfix := path.Ext(filename)
prefix := fmt.Sprintf("%v_%v", time.Now().Unix(), common.RandomString(32))
filename = fmt.Sprintf("%v%v", prefix, subfix)
sourcePath := fmt.Sprintf("%v/%v/%v/%v/%v", beego.BConfig.AppName, beego.BConfig.RunMode, fileType, date, filename)
return sourcePath
}