]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Add Feature fmp4自动刷新流地址
authorqydysky <32743305+qydysky@users.noreply.github.com>
Thu, 19 Jan 2023 13:59:10 +0000 (21:59 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Thu, 19 Jan 2023 13:59:10 +0000 (21:59 +0800)
CV/Var.go
F/api.go
Reply/stream.go

index d06fe374c95e048f6353f34b99b426859540d08f..0036b21aa30e89695ef2ac2fab30e147a83bda57 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -55,6 +55,7 @@ type Common struct {
 type LiveQn struct {
        Url      string
        ReUpTime time.Time
+       Expires  int //流到期时间
 }
 
 func (t *Common) Init() Common {
index f19f3bd05f13f89f9f03ced26e5cf7613693c85f..48157927848f214a48a3f3e00b00139400d2fe2e 100644 (file)
--- a/F/api.go
+++ b/F/api.go
@@ -362,9 +362,17 @@ func (c *GetFunc) Html() (missKey []string) {
                                                                //直播流链接
                                                                c.Live = []cv.LiveQn{}
                                                                for _, v1 := range v.URLInfo {
-                                                                       c.Live = append(c.Live, cv.LiveQn{
+                                                                       item := cv.LiveQn{
                                                                                Url: v1.Host + v.BaseURL + v1.Extra,
-                                                                       })
+                                                                       }
+
+                                                                       if query, e := url.ParseQuery(v1.Extra); e == nil {
+                                                                               if expires, e := strconv.Atoi(query.Get("expires")); e == nil {
+                                                                                       item.Expires = expires
+                                                                               }
+                                                                       }
+
+                                                                       c.Live = append(c.Live, item)
                                                                }
                                                        }
                                                }
@@ -631,7 +639,17 @@ func (c *GetFunc) getRoomPlayInfo() (missKey []string) {
                                                        //直播流链接
                                                        c.Live = []cv.LiveQn{}
                                                        for _, v1 := range v.URLInfo {
-                                                               c.Live = append(c.Live, cv.LiveQn{Url: v1.Host + v.BaseURL + v1.Extra})
+                                                               item := cv.LiveQn{
+                                                                       Url: v1.Host + v.BaseURL + v1.Extra,
+                                                               }
+
+                                                               if query, e := url.ParseQuery(v1.Extra); e == nil {
+                                                                       if expires, e := strconv.Atoi(query.Get("expires")); e == nil {
+                                                                               item.Expires = expires
+                                                                       }
+                                                               }
+
+                                                               c.Live = append(c.Live, item)
                                                        }
 
                                                        //找到配置格式,跳出
@@ -813,7 +831,17 @@ func (c *GetFunc) getRoomPlayInfoByQn() (missKey []string) {
                                                        //直播流链接
                                                        c.Live = []cv.LiveQn{}
                                                        for _, v1 := range v.URLInfo {
-                                                               c.Live = append(c.Live, cv.LiveQn{Url: v1.Host + v.BaseURL + v1.Extra})
+                                                               item := cv.LiveQn{
+                                                                       Url: v1.Host + v.BaseURL + v1.Extra,
+                                                               }
+
+                                                               if query, e := url.ParseQuery(v1.Extra); e == nil {
+                                                                       if expires, e := strconv.Atoi(query.Get("expires")); e == nil {
+                                                                               item.Expires = expires
+                                                                       }
+                                                               }
+
+                                                               c.Live = append(c.Live, item)
                                                        }
                                                }
                                        }
index 6d79d287fc4236f136b927413da20f9716bada53..51b3f758830cbea83b93df315f0d127139bc6f3c 100644 (file)
@@ -201,15 +201,6 @@ func (t *M4SStream) fetchCheckStream() bool {
                t.stream_type = "flv"
        }
 
-       // // 保存流地址过期时间
-       // if m3u8_url, err := url.Parse(t.common.Live[0]); err != nil {
-       //      t.log.L(`E: `, err.Error())
-       //      return false
-       // } else {
-       //      expires, _ := strconv.Atoi(m3u8_url.Query().Get("expires"))
-       //      t.stream_expires = int64(expires)
-       // }
-
        // 检查是否可以获取
        CookieM := make(map[string]string)
        t.common.Cookie.Range(func(k, v interface{}) bool {
@@ -746,11 +737,22 @@ func (t *M4SStream) saveStreamM4s() (e error) {
                fmp4KeyFrames    = slice.New[byte]()
                fmp4KeyFramesBuf []byte
                fmp4Decoder      = &Fmp4Decoder{}
+               flashingSer      bool
        )
 
        // 下载循环
        for download_seq := []*m4s_link_item{}; ; {
 
+               // 刷新流地址
+               if !flashingSer && int64(t.common.Live[0].Expires)-time.Now().Unix() < 60 {
+                       flashingSer = true
+                       t.log.L(`T: `, `刷新流地址...`)
+                       go func() {
+                               t.fetchCheckStream()
+                               flashingSer = false
+                       }()
+               }
+
                // 存在待下载切片
                if len(download_seq) != 0 {
 
@@ -955,17 +957,6 @@ func (t *M4SStream) saveStreamM4s() (e error) {
                        }
                }
 
-               // 刷新流地址
-               // 偶尔刷新后的切片编号与原来不连续,故不再提前检查,直到流获取失败再刷新
-               // if time.Now().Unix()+60 > t.stream_expires {
-               //      t.stream_expires = time.Now().Add(time.Minute * 2).Unix() // 临时的流链接过期时间
-               //      go func() {
-               //              if t.fetchCheckStream() {
-               //                      t.last_m4s = nil
-               //              }
-               //      }()
-               // }
-
                // 获取解析m3u8
                var m4s_links, m3u8_addon, err = t.fetchParseM3U8()
                if err != nil {