可用
This commit is contained in:
		
							
								
								
									
										17
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -4,8 +4,8 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"runtime"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -603,7 +603,7 @@ func main() {
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	// 获取转换后的m3u8视频链接
 | 
			
		||||
	http.HandleFunc("/video/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	http.HandleFunc("/video", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
			
		||||
 | 
			
		||||
		queryParam := r.URL.Query().Get("url")
 | 
			
		||||
@@ -615,18 +615,15 @@ func main() {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		fmt.Println("safeParam", safeParam)
 | 
			
		||||
		urls := models.GetVideoM3U8(safeParam)
 | 
			
		||||
 | 
			
		||||
		// 将对象转换为有缩进的JSON输出
 | 
			
		||||
		json, err := json.MarshalIndent(&struct{Urls string `json:"urls"`}{
 | 
			
		||||
			Urls: urls,
 | 
			
		||||
		}, "", "    ")
 | 
			
		||||
		urls, err := models.GetVideoM3U8(safeParam)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println(err)
 | 
			
		||||
			log.Println("获取视频链接失败", err)
 | 
			
		||||
			w.WriteHeader(http.StatusBadRequest)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 输出JSON
 | 
			
		||||
		// 将对象转换为有缩进的JSON输出
 | 
			
		||||
		json, _ := json.MarshalIndent(urls, "", "    ")
 | 
			
		||||
		w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
		w.Write(json)
 | 
			
		||||
	})
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package models
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/base64"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"regexp"
 | 
			
		||||
@@ -13,7 +14,7 @@ import (
 | 
			
		||||
	"github.com/spf13/viper"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func GetVideoM3U8(content string) (blob string) {
 | 
			
		||||
func GetVideoM3U8(content string) (playinfo *vod20170321.GetPlayInfoResponseBodyPlayInfoList, err error) {
 | 
			
		||||
	if len(regexp.MustCompile(`image.gameuiux.cn`).FindStringSubmatch(content)) > 0 {
 | 
			
		||||
		key := regexp.MustCompile(`^https?://image.gameuiux.cn/`).ReplaceAllString(content, "")
 | 
			
		||||
		log.Println("GetVideoM3U8", key)
 | 
			
		||||
@@ -25,7 +26,7 @@ func GetVideoM3U8(content string) (blob string) {
 | 
			
		||||
		})
 | 
			
		||||
		if _err != nil {
 | 
			
		||||
			log.Println("Ea", _err.Error())
 | 
			
		||||
			return
 | 
			
		||||
			return nil, _err
 | 
			
		||||
		}
 | 
			
		||||
		// 通过标题查询视频ID
 | 
			
		||||
		response, _err := client.SearchMedia(&vod20170321.SearchMediaRequest{
 | 
			
		||||
@@ -33,20 +34,20 @@ func GetVideoM3U8(content string) (blob string) {
 | 
			
		||||
		})
 | 
			
		||||
		if _err != nil {
 | 
			
		||||
			log.Println("Eb", _err.Error())
 | 
			
		||||
			return
 | 
			
		||||
			return nil, _err
 | 
			
		||||
		}
 | 
			
		||||
		if len(response.Body.MediaList) > 0 && *response.Body.MediaList[0].MediaType == "video" {
 | 
			
		||||
			// 通过ID查询视频播放地址
 | 
			
		||||
			VideoId := response.Body.MediaList[0].Video.VideoId
 | 
			
		||||
			log.Println("GetPlayInfo", response.Body.MediaList)
 | 
			
		||||
			result, err := client.GetPlayInfo(&vod20170321.GetPlayInfoRequest{VideoId: VideoId})
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Println("Ex", err.Error())
 | 
			
		||||
				return
 | 
			
		||||
			result, _err := client.GetPlayInfo(&vod20170321.GetPlayInfoRequest{VideoId: VideoId})
 | 
			
		||||
			if _err != nil {
 | 
			
		||||
				log.Println("Ex", _err.Error())
 | 
			
		||||
				return nil, _err
 | 
			
		||||
			}
 | 
			
		||||
			log.Println("GetPlayInfo", result)
 | 
			
		||||
			log.Println("GetPlayInfo", result.Body.PlayInfoList)
 | 
			
		||||
			// TODO: 返回播放地址列表
 | 
			
		||||
			return
 | 
			
		||||
			return result.Body.PlayInfoList, nil
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// TODO: 保存到点播服务
 | 
			
		||||
@@ -54,10 +55,10 @@ func GetVideoM3U8(content string) (blob string) {
 | 
			
		||||
 | 
			
		||||
		log.Println("无结果, 从OSS加载视频")
 | 
			
		||||
		bucket := GetBucket("gameui-image2")
 | 
			
		||||
		rest, err := bucket.GetObject(key)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("读取视频失败", err)
 | 
			
		||||
			return
 | 
			
		||||
		rest, _err := bucket.GetObject(key)
 | 
			
		||||
		if _err != nil {
 | 
			
		||||
			log.Println("读取视频失败", _err.Error())
 | 
			
		||||
			return nil, _err
 | 
			
		||||
		}
 | 
			
		||||
		defer rest.Close()
 | 
			
		||||
 | 
			
		||||
@@ -68,7 +69,7 @@ func GetVideoM3U8(content string) (blob string) {
 | 
			
		||||
		})
 | 
			
		||||
		if _err != nil {
 | 
			
		||||
			log.Println("Ee", _err.Error())
 | 
			
		||||
			return
 | 
			
		||||
			return nil, _err
 | 
			
		||||
		}
 | 
			
		||||
		// 解码base64
 | 
			
		||||
		upload := struct {
 | 
			
		||||
@@ -79,9 +80,9 @@ func GetVideoM3U8(content string) (blob string) {
 | 
			
		||||
		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 {
 | 
			
		||||
			if err = bucket.PutObject(upload.FileName, rest); err != nil {
 | 
			
		||||
				log.Println("上传视频失败", err)
 | 
			
		||||
				return
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -95,10 +96,11 @@ func GetVideoM3U8(content string) (blob string) {
 | 
			
		||||
		})
 | 
			
		||||
		if _err != nil {
 | 
			
		||||
			log.Println("Ef", _err.Error())
 | 
			
		||||
			return
 | 
			
		||||
			return nil, _err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		fmt.Println("GetPlayInfo", getPlayInfoReponse.Body.PlayInfoList)
 | 
			
		||||
		return getPlayInfoReponse.Body.PlayInfoList, nil
 | 
			
		||||
 | 
			
		||||
		//// 4.2 通过播放地址播放
 | 
			
		||||
		//getVideoPlayAuthRequest := &vod20170321.GetVideoPlayAuthRequest{
 | 
			
		||||
@@ -110,5 +112,5 @@ func GetVideoM3U8(content string) (blob string) {
 | 
			
		||||
		//}
 | 
			
		||||
		//console.Log(util.ToJSONString(tea.ToMap(getVideoPlayAuthReponse)))
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
	return nil, errors.New("无效的视频地址")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user