From e5d266b40fdb62793cca939fc6ccda7eaa35fc8e Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Thu, 19 Jan 2023 20:27:06 +0800 Subject: [PATCH] fix --- CV/Var.go | 7 ++++++- F/api.go | 21 ++++++++++++--------- Reply/stream.go | 48 +++++++++++++++++++++++++++++++++++------------- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/CV/Var.go b/CV/Var.go index b1c5bda..d06fe37 100644 --- a/CV/Var.go +++ b/CV/Var.go @@ -20,7 +20,7 @@ import ( type Common struct { Uid int //client uid - Live []string //直播流链接 + Live []LiveQn //直播流链接 Live_qn int //当前直播流质量 Live_want_qn int //期望直播流质量 Roomid int //房间ID @@ -52,6 +52,11 @@ type Common struct { ReqPool *idpool.Idpool //请求池 } +type LiveQn struct { + Url string + ReUpTime time.Time +} + func (t *Common) Init() Common { t.Qn = map[int]string{ // no change 20000: "4K", diff --git a/F/api.go b/F/api.go index 3832110..f19f3bd 100644 --- a/F/api.go +++ b/F/api.go @@ -12,6 +12,7 @@ import ( "time" c "github.com/qydysky/bili_danmu/CV" + cv "github.com/qydysky/bili_danmu/CV" J "github.com/qydysky/bili_danmu/Json" "github.com/skratchdot/open-golang/open" @@ -293,7 +294,7 @@ func (c *GetFunc) Html() (missKey []string) { if !c.Liveing { c.Live_qn = 0 c.AcceptQn = c.Qn - c.Live = []string{} + c.Live = []cv.LiveQn{} return } @@ -359,9 +360,11 @@ func (c *GetFunc) Html() (missKey []string) { c.AcceptQn = tmp } //直播流链接 - c.Live = []string{} + c.Live = []cv.LiveQn{} for _, v1 := range v.URLInfo { - c.Live = append(c.Live, v1.Host+v.BaseURL+v1.Extra) + c.Live = append(c.Live, cv.LiveQn{ + Url: v1.Host + v.BaseURL + v1.Extra, + }) } } } @@ -547,7 +550,7 @@ func (c *GetFunc) getRoomPlayInfo() (missKey []string) { if !c.Liveing { c.Live_qn = 0 c.AcceptQn = c.Qn - c.Live = []string{} + c.Live = []cv.LiveQn{} return } @@ -626,9 +629,9 @@ func (c *GetFunc) getRoomPlayInfo() (missKey []string) { c.AcceptQn = tmp } //直播流链接 - c.Live = []string{} + c.Live = []cv.LiveQn{} for _, v1 := range v.URLInfo { - c.Live = append(c.Live, v1.Host+v.BaseURL+v1.Extra) + c.Live = append(c.Live, cv.LiveQn{Url: v1.Host + v.BaseURL + v1.Extra}) } //找到配置格式,跳出 @@ -738,7 +741,7 @@ func (c *GetFunc) getRoomPlayInfoByQn() (missKey []string) { if !c.Liveing { c.Live_qn = 0 c.AcceptQn = c.Qn - c.Live = []string{} + c.Live = []cv.LiveQn{} return } @@ -808,9 +811,9 @@ func (c *GetFunc) getRoomPlayInfoByQn() (missKey []string) { c.AcceptQn = tmp } //直播流链接 - c.Live = []string{} + c.Live = []cv.LiveQn{} for _, v1 := range v.URLInfo { - c.Live = append(c.Live, v1.Host+v.BaseURL+v1.Extra) + c.Live = append(c.Live, cv.LiveQn{Url: v1.Host + v.BaseURL + v1.Extra}) } } } diff --git a/Reply/stream.go b/Reply/stream.go index d578029..68b6402 100644 --- a/Reply/stream.go +++ b/Reply/stream.go @@ -191,13 +191,13 @@ func (t *M4SStream) fetchCheckStream() bool { } // 保存流类型 - if strings.Contains(t.common.Live[0], `m3u8`) { + if strings.Contains(t.common.Live[0].Url, `m3u8`) { if t.config.save_as_mp4 { t.stream_type = "mp4" } else { t.stream_type = "m3u8" } - } else if strings.Contains(t.common.Live[0], `flv`) { + } else if strings.Contains(t.common.Live[0].Url, `flv`) { t.stream_type = "flv" } @@ -221,7 +221,7 @@ func (t *M4SStream) fetchCheckStream() bool { req := t.reqPool.Get() r := req.Item.(*reqf.Req) if e := r.Reqf(reqf.Rval{ - Url: v, + Url: v.Url, Retry: 10, SleepTime: 1000, Proxy: t.common.Proxy, @@ -257,9 +257,19 @@ func (t *M4SStream) fetchCheckStream() bool { } func (t *M4SStream) fetchParseM3U8() (m4s_links []*m4s_link_item, m3u8_addon []byte, e error) { + // 开始请求 + req := t.reqPool.Get() + defer t.reqPool.Put(req) + r := req.Item.(*reqf.Req) + // 请求解析m3u8内容 - for _, v := range t.common.Live { - m3u8_url, err := url.Parse(v) + for k, v := range t.common.Live { + // 跳过尚未启用的live地址 + if time.Now().Before(v.ReUpTime) { + continue + } + + m3u8_url, err := url.Parse(v.Url) if err != nil { e = err return @@ -288,10 +298,6 @@ func (t *M4SStream) fetchParseM3U8() (m4s_links []*m4s_link_item, m3u8_addon []b rval.Header[`If-Modified-Since`] = t.stream_last_modified.Add(time.Second).Format("Mon, 02 Jan 2006 15:04:05 CST") } - // 开始请求 - req := t.reqPool.Get() - defer t.reqPool.Put(req) - r := req.Item.(*reqf.Req) if err := r.Reqf(rval); err != nil { e = err continue @@ -404,7 +410,12 @@ func (t *M4SStream) fetchParseM3U8() (m4s_links []*m4s_link_item, m3u8_addon []b nos, _ := tmp[len(tmp)-1].getNo() noe, _ := t.last_m4s.getNo() if timed > 3 && math.Abs(timed-float64(nos-noe)) > 2 { - e = fmt.Errorf("服务器 %s 发生故障 %d 秒产出了 %d 切片", m3u8_url.Host, int(timed), nos-noe) + // 1min后重新启用 + t.common.Live[k].ReUpTime = time.Now().Add(time.Minute) + t.log.L("W: ", fmt.Sprintf("服务器 %s 发生故障 %d 秒产出了 %d 切片", m3u8_url.Host, int(timed), nos-noe)) + if k == len(t.common.Live)-1 { + e = errors.New("全部切片服务器发生故障") + } continue } } @@ -541,7 +552,7 @@ func (t *M4SStream) saveStream() (e error) { func (t *M4SStream) saveStreamFlv() (e error) { //对每个直播流进行尝试 for _, v := range t.common.Live { - surl, err := url.Parse(v) + surl, err := url.Parse(v.Url) if err != nil { t.log.L(`E: `, err) e = err @@ -962,6 +973,17 @@ func (t *M4SStream) saveStreamM4s() (e error) { break } } + + // { + // if t.last_m4s != nil { + // l, _ := t.last_m4s.getNo() + // fmt.Println("last", l) + // } + // for i := 0; i < len(m4s_links); i++ { + // fmt.Println(m4s_links[i].getNo()) + // } + // } + if len(m4s_links) == 0 { time.Sleep(time.Second) continue @@ -970,7 +992,7 @@ func (t *M4SStream) saveStreamM4s() (e error) { if t.last_m4s == nil { t.last_m4s = &m4s_link_item{} } - for i := len(m4s_links) - 1; i > 0; i-- { + for i := len(m4s_links) - 1; i >= 0; i-- { // fmt.Println("set last m4s", m4s_links[i].Base) if !m4s_links[i].isInit() && len(m4s_links[i].Base) > 0 { m4s_links[i].copyTo(t.last_m4s) @@ -1067,7 +1089,7 @@ func (t *M4SStream) Start() bool { // 设置全部服务 for _, v := range t.common.Live { - if url_struct, e := url.Parse(v); e == nil { + if url_struct, e := url.Parse(v.Url); e == nil { t.stream_hosts.Store(url_struct.Hostname(), nil) } } -- 2.39.2