diff --git a/Makefile b/Makefile index fe2d7fd..f532506 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ NAME=tencentKeTang BINDIR=bin -VERSION=v0.2.9 +VERSION=v0.2.10 BUILDTIME=$(shell date -u) GOBUILD=CGO_ENABLED=0 go build -ldflags '-w -s -X main.Version=${VERSION}' diff --git a/README.md b/README.md index 8dcb583..8176cbc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # 项目的由来 哎,腾讯课堂的app太难用了,此工具仅仅只是为了将视频下载到本地,使用第三方播放器使用 +# v0.2.10更新 +* 修复视频密钥文件下载失败的问题 + # v0.2.9更新 * 打脸来的太快,这两天实现了终端多行进度条…… * 最近尝试解决qq/微信扫码登录的问题,看来短时间内无法完成了,请采用cookie登录方式 diff --git a/ffmpeg/mergeAndDownload.go b/ffmpeg/mergeAndDownload.go index 2e8adcc..b8194c2 100644 --- a/ffmpeg/mergeAndDownload.go +++ b/ffmpeg/mergeAndDownload.go @@ -25,19 +25,21 @@ func (f *Ffmpeg) downloadTs(vodUrl, dk string, bitrate int, mp4Path string) erro if err != nil { return errors.Wrapf(err, "os.MkdirAll(%s)", m3u8Dir) } - key, err := base64.StdEncoding.DecodeString(dk) - if err != nil { - return errors.Wrap(err, "base64.StdEncoding.DecodeString") - } - keyFile, err := os.OpenFile(filepath.Join(m3u8Dir, "key"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) - if err != nil { - return errors.Wrapf(err, "os.OpenFile(%s)", filepath.Join(m3u8Dir, "index.m3u8")) - } - _, err = keyFile.Write(key) - if err != nil { - return errors.Wrap(err, "keyFile.Write") + if dk != "" { + key, err := base64.StdEncoding.DecodeString(dk) + if err != nil { + return errors.Wrap(err, "base64.StdEncoding.DecodeString") + } + keyFile, err := os.OpenFile(filepath.Join(m3u8Dir, "key"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) + if err != nil { + return errors.Wrapf(err, "os.OpenFile(%s)", filepath.Join(m3u8Dir, "index.m3u8")) + } + _, err = keyFile.Write(key) + if err != nil { + return errors.Wrap(err, "keyFile.Write") + } + _ = keyFile.Close() } - _ = keyFile.Close() m3u8, err := httplib.Get(vodUrl).String() if err != nil { @@ -59,6 +61,7 @@ func (f *Ffmpeg) downloadTs(vodUrl, dk string, bitrate int, mp4Path string) erro //先用lines设置max f.addDownloadBar(fileName, len(lines)) + keys := make(map[string]string) tsCount := 0 for _, line := range lines { if strings.HasPrefix(line, "#EXT-X-KEY") { @@ -66,7 +69,26 @@ func (f *Ffmpeg) downloadTs(vodUrl, dk string, bitrate int, mp4Path string) erro if reg1 == nil { return errors.Wrap(err, "regexp.MustCompile") } - line = reg1.ReplaceAllString(line, fmt.Sprintf(`URI="./%s"`, "key")) + //根据规则提取关键信息 + result1 := reg1.FindAllStringSubmatch(line, -1) + if len(result1) == 0 { + return errors.Wrap(err, "regexp.FindAllStringSubmatch") + } + keyUrl := result1[0][1] + keyFileName := "key" + if dk == "" { + if v, ok := keys[keyUrl]; ok { + keyFileName = v + } else { + keyFileName = fmt.Sprintf("key%d", len(keys)) + err := httplib.Get(keyUrl).ToFile(filepath.Join(m3u8Dir, keyFileName)) + if err != nil { + return errors.Wrap(err, "httplib.Get") + } + keys[keyUrl] = keyFileName + } + } + line = reg1.ReplaceAllString(line, fmt.Sprintf(`URI="./%s"`, keyFileName)) } else if strings.HasPrefix(line, "#") { } else { parm := strings.Split(line, "?")