From 4cf3a1ddc61310592d874c7a1c3e892a08030773 Mon Sep 17 00:00:00 2001 From: qydysky Date: Sat, 12 Jun 2021 17:16:51 +0800 Subject: [PATCH] =?utf8?q?hls=E6=95=85=E9=9A=9C=E8=BD=AC=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Reply/F.go | 161 +++++++++++++++++++++++++++++------------------------ 1 file changed, 87 insertions(+), 74 deletions(-) diff --git a/Reply/F.go b/Reply/F.go index b661bd4..ea1af9e 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -325,30 +325,42 @@ func Savestreamf(){ if savestream.cancel.Islive() {return} + //random host load balance + Host_list := []string{} + if savestream.hls_banlance_host { + for _,v := range c.Live { + url_struct,e := url.Parse(v) + if e != nil {continue} + Host_list = append(Host_list, url_struct.Hostname()) + } + } + var ( no_found_link = errors.New("no_found_link") no_Modified = errors.New("no_Modified") last_hls_Modified time.Time - hls_get_link = func(m3u8_url string,last_download *m4s_link_item) (need_download []*m4s_link_item,m3u8_file_addition []byte,expires int,err error) { - url_struct,e := url.Parse(m3u8_url) - if e != nil { - err = e - return - } - - query := url_struct.Query() - + hls_get_link = func(m3u8_urls []string,last_download *m4s_link_item) (need_download []*m4s_link_item,m3u8_file_addition []byte,expires int,err error) { var ( + r *reqf.Req + m3u8_url *url.URL + ) + for index:=0;index 3000 { l.L(`I: `, `hls列表下载慢`, usedt, `ms`) } @@ -385,6 +400,7 @@ func Savestreamf(){ } } + query := m3u8_url.Query() trid := query.Get("trid") expires,_ = strconv.Atoi(query.Get("expires")) buf := r.Respon @@ -393,9 +409,9 @@ func Savestreamf(){ if len(buf) != 0 && !bytes.Contains(buf, []byte("#")) { buf,err = base64.StdEncoding.DecodeString(string(buf)) if err != nil { + l.L(`W: `, err, string(buf)) return } - // fmt.Println(`base64`) } var m4s_links []*m4s_link_item @@ -420,7 +436,7 @@ func Savestreamf(){ return } m4s_links = append(m4s_links, &m4s_link_item{ - Url:url_struct.ResolveReference(u).String(), + Url:m3u8_url.ResolveReference(u).String(), Base:m4s_link, Offset_line:i, }) @@ -1130,7 +1146,7 @@ func Savestreamf(){ break } - links,file_add,exp,e := hls_get_link(c.Live[0],last_download) + links,file_add,exp,e := hls_get_link(c.Live,last_download) if e != nil { if e == no_Modified { time.Sleep(time.Duration(2)*time.Second) @@ -1163,23 +1179,6 @@ func Savestreamf(){ continue } - //random host load balance - if savestream.hls_banlance_host { - Host_list := []string{} - for _,v := range c.Live { - url_struct,e := url.Parse(v) - if e != nil {continue} - Host_list = append(Host_list, url_struct.Hostname()) - } - random := int(time.Now().Unix()) - for i:=0;i= c.Live_qn { expires = int64(exp) @@ -1268,40 +1267,54 @@ func Savestreamf(){ download_limit.Block() defer download_limit.UnBlock() + //use url struct + var link_url *url.URL + { + if tmp,e := url.Parse(link.Url);e != nil { + l.L(`E: `, e) + return + } else {link_url = tmp} + } + link.status = s_loading - r := reqf.New() - if e := r.Reqf(reqf.Rval{ - Url: link.Url, - SaveToPath: path + link.Base, - Retry: 1, - ConnectTimeout: 3000, - ReadTimeout: 1000, - Timeout: 3000, - Proxy: c.Proxy, - }); e != nil{ - if reqf.IsTimeout(e) || strings.Contains(e.Error(), "x509") { - l.L(`T: `, link.Base, `将重试!`) - //避免影响后续猜测 - link.Offset_line = 0 - miss_download.Lock() - miss_download.List = append(miss_download.List, link) - miss_download.Unlock() + for index:=0;index 700 { - l.L(`I: `, `hls切片下载慢`, usedt, `ms`) - } - link.status = s_fin - //存入cache - if _,ok := m4s_cache.Load(path + link.Base);!ok{ - m4s_cache.Store(path + link.Base, r.Respon) - go func(){//移除 - time.Sleep(time.Second*time.Duration(savestream.hlsbuffersize+savestream.m4s_hls+1)) - m4s_cache.Delete(path + link.Base) - }() + if usedt := r.UsedTime.Seconds();usedt > 700 { + l.L(`I: `, `hls切片下载慢`, usedt, `ms`) + } + link.status = s_fin + //存入cache + if _,ok := m4s_cache.Load(path + link.Base);!ok{ + m4s_cache.Store(path + link.Base, r.Respon) + go func(){//移除 + time.Sleep(time.Second*time.Duration(savestream.hlsbuffersize+savestream.m4s_hls+1)) + m4s_cache.Delete(path + link.Base) + }() + } + break } } }(links[i],savestream.path) -- 2.39.2