Browse Source

新增一个功能,保存每个网站 Top1 的字幕

Signed-off-by: 716 <[email protected]>
716 4 years ago
parent
commit
ff56e486ab

+ 1 - 0
.gitignore

@@ -6,3 +6,4 @@
 /debugThings
 /tmpthings
 /model/result.png
+/sub_supplier/xunlei/Logs/*.log

+ 1 - 0
common/reqParam.go

@@ -3,6 +3,7 @@ package common
 // ReqParam 可选择传入的参数
 type ReqParam struct {
 	UserExtList []string	// 用户确认的视频后缀名支持列表
+	SaveMultiSub bool		// 存储每个网站 Top1 的字幕
 	DebugMode bool			// 调试标志位
 	HttpProxy string		// HttpClient 相关
 	UserAgent string		// HttpClient 相关

+ 35 - 15
downloader.go

@@ -99,7 +99,7 @@ func (d Downloader) DownloadSub(dir string) error {
 		// 字幕都下载缓存好了,需要抉择存哪一个,优先选择中文双语的,然后到中文
 		organizeSubFiles, err := subSupplierHub.DownloadSub(oneVideoFullPath, i)
 		if err != nil {
-			d.log.Errorln(oneVideoFullPath, "DownloadSub",err)
+			d.log.Errorln("subSupplierHub.DownloadSub", oneVideoFullPath ,err)
 			continue
 		}
 		// 得到目标视频文件的根目录
@@ -113,18 +113,35 @@ func (d Downloader) DownloadSub(dir string) error {
 			}
 		}
 		// -------------------------------------------------
-		// TODO 这里可以选择是保持一个字幕,还是把排名较高的几个都保存下来,这个得考虑字幕的命名问题
-		var finalSubFile *common.SubParserFileInfo
-		finalSubFile = d.mk.SelectOneSubFile(organizeSubFiles)
-		if finalSubFile == nil {
-			d.log.Warnln("Found", len(organizeSubFiles), " subtitles but not one fit:", oneVideoFullPath)
-			continue
-		}
-		// 找到了,写入文件
-		err = d.writeSubFile2VideoPath(oneVideoFullPath, *finalSubFile)
-		if err != nil {
-			d.log.Errorln("writeSubFile2VideoPath", err)
-			continue
+		if d.reqParam.SaveMultiSub == false {
+			// 选择最优的一个字幕
+			var finalSubFile *common.SubParserFileInfo
+			finalSubFile = d.mk.SelectOneSubFile(organizeSubFiles)
+			if finalSubFile == nil {
+				d.log.Warnln("Found", len(organizeSubFiles), " subtitles but not one fit:", oneVideoFullPath)
+				continue
+			}
+			// 找到了,写入文件
+			err = d.writeSubFile2VideoPath(oneVideoFullPath, *finalSubFile, "")
+			if err != nil {
+				d.log.Errorln("SaveMultiSub:", d.reqParam.SaveMultiSub ,"writeSubFile2VideoPath:", err)
+				continue
+			}
+		} else {
+			// 每个网站 Top1 的字幕
+			siteNames, finalSubFiles := d.mk.SelectEachSiteTop1SubFile(organizeSubFiles)
+			if len(siteNames) < 0 {
+				d.log.Warnln("SelectEachSiteTop1SubFile found none sub file")
+				continue
+			}
+
+			for i, file := range finalSubFiles {
+				err = d.writeSubFile2VideoPath(oneVideoFullPath, file, siteNames[i])
+				if err != nil {
+					d.log.Errorln("SaveMultiSub:", d.reqParam.SaveMultiSub ,"writeSubFile2VideoPath:", err)
+					continue
+				}
+			}
 		}
 		// -----------------------------------------------------
 	}
@@ -132,13 +149,16 @@ func (d Downloader) DownloadSub(dir string) error {
 }
 
 // 在前面需要进行语言的筛选、排序,这里仅仅是存储
-func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFile common.SubParserFileInfo) error {
+func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFile common.SubParserFileInfo, extraSubPreName string) error {
 	videoRootPath := filepath.Dir(videoFileFullPath)
 	embyLanExtName := model.Lang2EmbyName(finalSubFile.Lang)
 	// 构建视频文件加 emby 的字幕预研要求名称
 	videoFileNameWithOutExt := strings.ReplaceAll(filepath.Base(videoFileFullPath),
 		filepath.Ext(videoFileFullPath), "")
-	subNewName := videoFileNameWithOutExt + embyLanExtName + finalSubFile.Ext
+	if extraSubPreName != "" {
+		extraSubPreName = "[" + extraSubPreName +"]"
+	}
+	subNewName := videoFileNameWithOutExt + embyLanExtName + extraSubPreName + finalSubFile.Ext
 	desSubFullPath := path.Join(videoRootPath, subNewName)
 	// 最后写入字幕
 	err := utils.OutputFile(desSubFullPath, finalSubFile.Data)

+ 3 - 1
downloader_test.go

@@ -32,7 +32,9 @@ func TestDownloader_DownloadSub(t *testing.T) {
 	//dirRoot := "X:\\电影\\Oslo (2021)"
 	dirRoot := "X:\\电影\\The Devil All the Time (2020)"
 
-	dl := NewDownloader(common.ReqParam{DebugMode: true})
+	dl := NewDownloader(common.ReqParam{
+		SaveMultiSub: true,
+	})
 	err = dl.DownloadSub(dirRoot)
 	if err != nil {
 		t.Fatal(err)

+ 43 - 16
mark_system/marking_system.go

@@ -11,25 +11,63 @@ import (
 // MarkingSystem 评价系统,解决字幕排序优先级问题
 type MarkingSystem struct {
 	log *logrus.Logger
-	subSiteSequence []string	// 网站的优先级,从高到低
+	subSiteSequence []string			// 网站的优先级,从高到低
+	subParserHub *model.SubParserHub
 }
 
 func NewMarkingSystem(subSiteSequence []string) *MarkingSystem {
 	mk := MarkingSystem{subSiteSequence: subSiteSequence,
-		log: model.GetLogger()}
+		log: model.GetLogger(),
+		subParserHub: model.NewSubParserHub(ass.NewParser(), srt.NewParser())}
 	return &mk
 }
 
+// SelectOneSubFile 选择最优的一个字幕文件
 func (m MarkingSystem) SelectOneSubFile(organizeSubFiles []string) *common.SubParserFileInfo {
 	var finalSubFile common.SubParserFileInfo
 	// TODO 这里先处理 Top1 的字幕,后续再考虑怎么觉得 Top N 选择哪一个,很可能选择每个网站 Top 1就行了,具体的过滤逻辑在其内部实现
+	subInfoDict := m.parseSubFileInfo(organizeSubFiles)
+	// 优先级别暂定 subSiteSequence: zimuku -> subhd -> xunlei -> shooter
+	for _, subSite := range m.subSiteSequence {
+		value, ok := subInfoDict[subSite]
+		if ok == false {
+			continue
+		}
+		info := model.FindChineseBestSubtitle(value)
+		if info != nil {
+			finalSubFile = *info
+			return &finalSubFile
+		}
+	}
+	return nil
+}
+
+// SelectEachSiteTop1SubFile 每个网站最优的文件
+func (m MarkingSystem) SelectEachSiteTop1SubFile(organizeSubFiles []string) ([]string, []common.SubParserFileInfo) {
+	// 每个文件都带有出处 [subhd]
+	var outSiteName = make([]string, 0)
+	var outSubParserFileInfos = make([]common.SubParserFileInfo, 0)
+	subInfoDict := m.parseSubFileInfo(organizeSubFiles)
+	for siteName, infos := range subInfoDict {
+		// 每个网站保存一个
+		info := model.FindChineseBestSubtitle(infos)
+		if info != nil {
+			outSiteName = append(outSiteName, siteName)
+			outSubParserFileInfos = append(outSubParserFileInfos, *info)
+		}
+	}
+
+	return outSiteName, outSubParserFileInfos
+}
+
+// parseSubFileInfo 从文件解析字幕信息
+func (m MarkingSystem) parseSubFileInfo(organizeSubFiles []string) map[string][]common.SubParserFileInfo {
 	// 一个网站可能就算取了 Top1 字幕,也可能是返回一个压缩包,然后解压完就是多个字幕,所以
 	var subInfoDict = make(map[string][]common.SubParserFileInfo)
 	// 拿到现有的字幕列表,开始抉择
 	// 先判断当前字幕是什么语言(如果是简体,还需要考虑,判断这个字幕是简体还是繁体)
-	subParserHub := model.NewSubParserHub(ass.NewParser(), srt.NewParser())
 	for _, oneSubFileFullPath := range organizeSubFiles {
-		subFileInfo, err := subParserHub.DetermineFileTypeFromFile(oneSubFileFullPath)
+		subFileInfo, err := m.subParserHub.DetermineFileTypeFromFile(oneSubFileFullPath)
 		if err != nil {
 			m.log.Error(err)
 			continue
@@ -48,16 +86,5 @@ func (m MarkingSystem) SelectOneSubFile(organizeSubFiles []string) *common.SubPa
 		// 添加
 		subInfoDict[subFileInfo.FromWhereSite] = append(subInfoDict[subFileInfo.FromWhereSite], *subFileInfo)
 	}
-	// 优先级别暂定 subSiteSequence: zimuku -> subhd -> xunlei -> shooter
-	for _, subSite := range m.subSiteSequence {
-		value, ok := subInfoDict[subSite]
-		if ok == true {
-			info := model.FindChineseBestSubtitle(value)
-			if info != nil {
-				finalSubFile = *info
-				return &finalSubFile
-			}
-		}
-	}
-	return nil
+	return subInfoDict
 }

+ 0 - 1
model/subParserHub.go

@@ -60,7 +60,6 @@ func (p SubParserHub) getFromWhereSite(filePath string) string {
 	return matched[1]
 }
 
-
 // IsSubTypeWanted 这里匹配的字幕的格式,不包含 Ext 的 . 小数点,注意,仅仅是包含关系
 func IsSubTypeWanted(subName string) bool {
 	nowLowerName := strings.ToLower(subName)

+ 3 - 2
sub_supplier/shooter/shooter_test.go

@@ -1,12 +1,13 @@
 package shooter
 
 import (
-	"github.com/allanpk716/ChineseSubFinder/model"
+	"github.com/allanpk716/ChineseSubFinder/common"
 	"testing"
 )
 
 func TestNewSupplier(t *testing.T) {
-	movie1 := "X:\\电影\\龙猫 (1988)\\龙猫 (1988) 1080p DTS.mkv"
+	movie1 := "X:\\电影\\The Devil All the Time (2020)\\The Devil All the Time (2020) WEBDL-1080p.mkv"
+	//movie1 := "X:\\电影\\龙猫 (1988)\\龙猫 (1988) 1080p DTS.mkv"
 	//movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
 	//movie1 := "X:\\电影\\机动战士Z高达:星之继承者 (2005)\\机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"
 	//movie1 := "X:\\连续剧\\The Bad Batch\\Season 1\\The Bad Batch - S01E01 - Aftermath WEBDL-1080p.mkv"

+ 5 - 0
sub_supplier/subSupplierHub.go

@@ -34,6 +34,11 @@ func NewSubSupplierHub(one _interface.ISupplier,_inSupplier ..._interface.ISuppl
 
 // DownloadSub 某一个视频的字幕下载,下载完毕后,返回下载缓存每个字幕的位置
 func (d SubSupplierHub) DownloadSub(videoFullPath string, index int) ([]string, error) {
+	// 先清理缓存文件夹
+	err := model.ClearTmpFolder()
+	if err != nil {
+		d.log.Error(err)
+	}
 	subInfos := d.downloadSub4OneVideo(videoFullPath, index)
 	organizeSubFiles, err := d.organizeDlSubFiles(subInfos)
 	if err != nil {

+ 2 - 1
sub_supplier/subhd/subhd_test.go

@@ -6,7 +6,8 @@ import (
 
 func TestSupplier_GetSubListFromFile(t *testing.T) {
 
-	movie1 := "X:\\电影\\Oslo (2021)\\Oslo (2021) WEBDL-1080p.mkv"
+	movie1 := "X:\\电影\\The Devil All the Time (2020)\\The Devil All the Time (2020) WEBDL-1080p.mkv"
+	//movie1 := "X:\\电影\\Oslo (2021)\\Oslo (2021) WEBDL-1080p.mkv"
 	//movie1 := "X:\\电影\\Spiral From the Book of Saw (2021)\\Spiral From the Book of Saw (2021) WEBDL-1080p.mkv"
 	//movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
 	//movie1 := "X:\\电影\\机动战士Z高达:星之继承者 (2005)\\机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"

+ 3 - 2
sub_supplier/xunlei/xunlei_test.go

@@ -1,12 +1,13 @@
 package xunlei
 
 import (
-	"github.com/allanpk716/ChineseSubFinder/model"
+	"github.com/allanpk716/ChineseSubFinder/common"
 	"testing"
 )
 
 func TestGetList(t *testing.T) {
-	movie1 := "X:\\电影\\龙猫 (1988)\\龙猫 (1988) 1080p DTS.mkv"
+	movie1 := "X:\\电影\\The Devil All the Time (2020)\\The Devil All the Time (2020) WEBDL-1080p.mkv"
+	//movie1 := "X:\\电影\\龙猫 (1988)\\龙猫 (1988) 1080p DTS.mkv"
 	//movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
 	//movie1:= "X:\\电影\\Spiral From the Book of Saw (2021)\\Spiral From the Book of Saw (2021) WEBDL-1080p.mkv"
 	//movie1 := "X:\\电影\\机动战士Z高达:星之继承者 (2005)\\机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"

+ 2 - 1
sub_supplier/zimuku/zimuku_test.go

@@ -20,8 +20,9 @@ func TestSupplier_GetSubListFromKeyword(t *testing.T) {
 }
 
 func TestSupplier_GetSubListFromFile(t *testing.T) {
+	movie1 := "X:\\电影\\The Devil All the Time (2020)\\The Devil All the Time (2020) WEBDL-1080p.mkv"
 	//movie1 := "X:\\电影\\龙猫 (1988)\\龙猫 (1988) 1080p DTS.mkv"
-	movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
+	//movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
 	//movie1:= "X:\\电影\\Spiral From the Book of Saw (2021)\\Spiral From the Book of Saw (2021) WEBDL-1080p.mkv"
 	//movie1 := "X:\\电影\\机动战士Z高达:星之继承者 (2005)\\机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"
 	//movie1 := "X:\\连续剧\\The Bad Batch\\Season 1\\The Bad Batch - S01E01 - Aftermath WEBDL-1080p.mkv"