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