Skip to content

Commit

Permalink
* 先更新ts的url,再应用过滤,方便记录日志文件
Browse files Browse the repository at this point in the history
  • Loading branch information
orestonce committed Oct 15, 2024
1 parent 4ba0f3f commit 247bc90
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
27 changes: 19 additions & 8 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"crypto/tls"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/orestonce/m3u8d/mformat"
"net/http"
Expand Down Expand Up @@ -51,6 +52,7 @@ func (this *DownloadEnv) StartDownload(req StartDownload_Req) (errMsg string) {
this.status.IsRunning = true
go func() {
this.runDownload(req, skipInfo)
this.logToFile_TsNotWriteReason()
this.status.SetProgressBarTitle("下载进度")
this.status.DrawProgressBar(1, 0)

Expand Down Expand Up @@ -157,6 +159,9 @@ func (this *DownloadEnv) runDownload(req StartDownload_Req, skipInfo SkipTsInfo)
}
this.logToFile("version: " + GetVersion())
this.logToFile("origin m3u8 url: " + req.M3u8Url)

data, _ := json.Marshal(req)
this.logToFile("run args: " + string(data))
}

this.status.SetProgressBarTitle("[1/5]嗅探m3u8")
Expand Down Expand Up @@ -196,28 +201,34 @@ func (this *DownloadEnv) runDownload(req StartDownload_Req, skipInfo SkipTsInfo)

this.status.SetProgressBarTitle("[2/5]获取ts列表")
tsList := info.GetTsList()

//先更新ts的url信息, 方便后续记录url
errMsg = updateTsUrl(req.M3u8Url, tsList)
if errMsg != "" {
this.setErrMsg("updateTsUrl: " + errMsg)
return
}

tsList, skipTsRecordList := skipApplyFilter(tsList, skipInfo)
for _, record := range skipTsRecordList {
this.status.setTsNotWriteReason(&record.ts, "触发跳过表达式,"+record.reason)
}
if len(tsList) <= 0 {
this.setErrMsg("需要下载的文件为空")
return
}
// 获取m3u8地址的内容体
err = this.updateMedia(req.M3u8Url, tsList)
if err != nil {
this.setErrMsg("updateMedia: " + err.Error())
errMsg = this.updateMediaKeyContent(req.M3u8Url, tsList)
if errMsg != "" {
this.setErrMsg("updateMediaKeyContent: " + errMsg)
return
}

for _, record := range skipTsRecordList {
this.status.setTsNotWriteReason(&record.ts, "触发跳过表达式,"+record.reason)
}

// 下载ts
this.status.SetProgressBarTitle("[3/5]下载ts")
this.status.SpeedResetBytes()
err = this.downloader(tsList, skipInfo, tsSaveDir, req)
this.status.SpeedResetBytes()
this.logToFile_TsNotWriteReason()
if err != nil {
this.setErrMsg("下载ts文件错误: " + err.Error())
return
Expand Down
29 changes: 18 additions & 11 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,42 +80,49 @@ func getHost(Url string) (host string, err error) {
return u.Scheme + "://" + u.Host, nil
}

// updateMedia 更新ts媒体的URL、key的URL、key的内容
func (this *DownloadEnv) updateMedia(m3u8Url string, tsList []mformat.TsInfo) (err error) {
uriToContentMap := map[string][]byte{}

func updateTsUrl(m3u8Url string, tsList []mformat.TsInfo) (errMsg string) {
for idx := range tsList {
ts := &tsList[idx]
var errMsg string
ts.Url, errMsg = ResolveRefUrl(m3u8Url, ts.URI)
if errMsg != "" {
return errors.New(errMsg)
return "ts.URI = " + ts.URI + ", error " + errMsg
}
}
return ""
}

// updateMediaKeyContent 下载ts媒体的加密key的内容
func (this *DownloadEnv) updateMediaKeyContent(m3u8Url string, tsList []mformat.TsInfo) (errMsg string) {
uriToContentMap := map[string][]byte{}

for idx := range tsList {
ts := &tsList[idx]
if ts.Key.Method != `` {
keyContent, ok := uriToContentMap[ts.Key.KeyURI]
if ok == false {
var keyUrl string
keyUrl, errMsg = ResolveRefUrl(m3u8Url, ts.Key.KeyURI)
if errMsg != "" {
return errors.New(errMsg)
return "ts.Key.KeyURI = " + ts.Key.KeyURI + ", error " + errMsg
}
var httpResp *http.Response
var err error
keyContent, httpResp, err = this.doGetRequest(keyUrl, true)
if err != nil {
return err
return "ts.Key.KeyURI = " + ts.Key.KeyURI + ", http error " + err.Error()
}
if httpResp.StatusCode != 200 {
return errors.New("http code error " + strconv.Itoa(httpResp.StatusCode))
return "ts.Key.KeyURI = " + ts.Key.KeyURI + ", http code error " + strconv.Itoa(httpResp.StatusCode)
}
if ts.Key.Method == mformat.EncryptMethod_AES128 && len(keyContent) != 16 { // Aes 128
return errors.New("invalid key " + strconv.Quote(string(keyContent)))
return "ts.Key.KeyURI = " + ts.Key.KeyURI + ", invalid key " + strconv.Quote(string(keyContent))
}
uriToContentMap[ts.Key.KeyURI] = keyContent
}
ts.Key.KeyContent = keyContent
}
}
return nil
return ""
}

// 下载ts文件
Expand Down

0 comments on commit 247bc90

Please sign in to comment.