119 lines
3.5 KiB
Go
119 lines
3.5 KiB
Go
package models
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"regexp"
|
|
"time"
|
|
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
vod20170321 "github.com/alibabacloud-go/vod-20170321/v2/client"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
imageRegexp = regexp.MustCompile(`image.gameuiux.cn`)
|
|
urlRegexp = regexp.MustCompile(`^https?://image.gameuiux.cn/`)
|
|
)
|
|
|
|
func GetVideoM3U8(content string) (playinfo *vod20170321.GetPlayInfoResponseBody, err error) {
|
|
if len(imageRegexp.FindStringSubmatch(content)) > 0 {
|
|
key := urlRegexp.ReplaceAllString(content, "")
|
|
// 连接点播服务的OSS
|
|
client, _err := vod20170321.NewClient(&openapi.Config{
|
|
AccessKeyId: tea.String(viper.GetString("video.accessID")),
|
|
AccessKeySecret: tea.String(viper.GetString("video.accessKey")),
|
|
RegionId: tea.String("cn-shanghai"),
|
|
})
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
// 通过标题查询视频ID
|
|
response, _err := client.SearchMedia(&vod20170321.SearchMediaRequest{
|
|
Match: tea.String(fmt.Sprintf("Title='%s'", key)),
|
|
})
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
if len(response.Body.MediaList) > 0 && *response.Body.MediaList[0].MediaType == "video" {
|
|
// 通过ID查询视频播放地址
|
|
VideoId := response.Body.MediaList[0].Video.VideoId
|
|
result, _err := client.GetPlayInfo(&vod20170321.GetPlayInfoRequest{VideoId: VideoId})
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
// 返回播放地址列表
|
|
return result.Body, nil
|
|
}
|
|
|
|
log.Println("无结果, 从OSS加载视频")
|
|
bucket := GetBucket("gameui-image2")
|
|
rest, _err := bucket.GetObject(key)
|
|
if _err != nil {
|
|
log.Println("读取视频失败", _err.Error())
|
|
return nil, _err
|
|
}
|
|
defer rest.Close()
|
|
|
|
// 1.获取视频上传地址和凭证,并生成视频信息
|
|
createUploadVideoResponse, _err := client.CreateUploadVideo(&vod20170321.CreateUploadVideoRequest{
|
|
Title: tea.String(key),
|
|
FileName: tea.String(key),
|
|
})
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
// 解码base64
|
|
upload := struct {
|
|
Endpoint string `json:"Endpoint"`
|
|
Bucket string `json:"Bucket"`
|
|
FileName string `json:"FileName"`
|
|
}{}
|
|
data, _ := base64.StdEncoding.DecodeString(*createUploadVideoResponse.Body.UploadAddress)
|
|
json.Unmarshal(data, &upload)
|
|
if bucket := GetBucket(upload.Bucket); bucket != nil {
|
|
if err = bucket.PutObject(upload.FileName, rest); err != nil {
|
|
log.Println("上传视频失败", err)
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
fmt.Println("上传视频结束")
|
|
|
|
count := 0
|
|
// 查询等待完成转码, 最多等待2分钟
|
|
for {
|
|
getMezzanineInfo, _err := client.GetMezzanineInfo(&vod20170321.GetMezzanineInfoRequest{
|
|
VideoId: createUploadVideoResponse.Body.VideoId,
|
|
})
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
if *getMezzanineInfo.Body.Mezzanine.Status == "Normal" || *getMezzanineInfo.Body.Mezzanine.Status == "Fail" {
|
|
break
|
|
}
|
|
time.Sleep(time.Second * 1)
|
|
count++
|
|
if count > 120 {
|
|
return nil, errors.New("转码超时")
|
|
}
|
|
}
|
|
// 4.媒体上传完成之后,可以获取媒体播放信息进行播放
|
|
// 4.1 通过播放凭证播放
|
|
getPlayInfoReponse, _err := client.GetPlayInfo(&vod20170321.GetPlayInfoRequest{
|
|
VideoId: createUploadVideoResponse.Body.VideoId,
|
|
PlayConfig: tea.String(`{"PlayDomain":"live.gameui.net","Format":"m3u8","Definition":"LD","AuthTimeout":3600,"StreamType":"video"}`),
|
|
})
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
|
|
return getPlayInfoReponse.Body, nil
|
|
}
|
|
return nil, errors.New("无效的视频地址")
|
|
}
|