downloader.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. package internal
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/common"
  4. "github.com/allanpk716/ChineseSubFinder/internal/ifaces"
  5. embyHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/emby_helper"
  6. "github.com/allanpk716/ChineseSubFinder/internal/logic/forced_scan_and_down_sub"
  7. markSystem "github.com/allanpk716/ChineseSubFinder/internal/logic/mark_system"
  8. "github.com/allanpk716/ChineseSubFinder/internal/logic/restore_fix_timeline_bk"
  9. seriesHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
  10. subSupplier "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier"
  11. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier/shooter"
  12. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier/subhd"
  13. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier/xunlei"
  14. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier/zimuku"
  15. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_timeline_fixer"
  16. "github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
  17. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  18. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  19. subcommon "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
  20. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  21. sub_timeline_fixer_pkg "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_timeline_fixer"
  22. "github.com/allanpk716/ChineseSubFinder/internal/types"
  23. "github.com/allanpk716/ChineseSubFinder/internal/types/emby"
  24. "github.com/allanpk716/ChineseSubFinder/internal/types/series"
  25. "github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
  26. "github.com/panjf2000/ants/v2"
  27. "github.com/sirupsen/logrus"
  28. "golang.org/x/net/context"
  29. "os"
  30. "path/filepath"
  31. "sync"
  32. )
  33. // Downloader 实例化一次用一次,不要反复的使用,很多临时标志位需要清理。
  34. type Downloader struct {
  35. reqParam types.ReqParam
  36. log *logrus.Logger
  37. topic int // 最多能够下载 Top 几的字幕,每一个网站
  38. mk *markSystem.MarkingSystem // MarkingSystem
  39. embyHelper *embyHelper.EmbyHelper
  40. movieFileFullPathList []string // 多个需要搜索字幕的电影文件全路径
  41. seriesSubNeedDlMap map[string][]emby.EmbyMixInfo // 多个需要搜索字幕的连续剧目录
  42. subFormatter ifaces.ISubFormatter // 字幕格式化命名的实现
  43. subNameFormatter subcommon.FormatterName // 从 inSubFormatter 推断出来
  44. needForcedScanAndDownSub bool // 将会强制扫描所有的视频,下载字幕,替换已经存在的字幕,不进行时间段和已存在则跳过的判断。且不会进过 Emby API 的逻辑,智能进行强制去以本程序的方式去扫描。
  45. NeedRestoreFixTimeLineBK bool // 从 csf-bk 文件还原时间轴修复前的字幕文件
  46. subTimelineFixerHelperEx *sub_timeline_fixer.SubTimelineFixerHelperEx // 字幕时间轴校正
  47. }
  48. func NewDownloader(inSubFormatter ifaces.ISubFormatter, _reqParam ...types.ReqParam) *Downloader {
  49. var downloader Downloader
  50. downloader.subFormatter = inSubFormatter
  51. downloader.log = log_helper.GetLogger()
  52. downloader.topic = common.DownloadSubsPerSite
  53. if len(_reqParam) > 0 {
  54. downloader.reqParam = _reqParam[0]
  55. if downloader.reqParam.Topic > 0 && downloader.reqParam.Topic != downloader.topic {
  56. downloader.topic = downloader.reqParam.Topic
  57. }
  58. // 如果 Debug 模式开启了,强制设置线程数为1,方便定位问题
  59. if downloader.reqParam.DebugMode == true {
  60. downloader.reqParam.Threads = 1
  61. } else {
  62. // 并发线程的范围控制
  63. if downloader.reqParam.Threads <= 0 {
  64. downloader.reqParam.Threads = 2
  65. } else if downloader.reqParam.Threads >= 10 {
  66. downloader.reqParam.Threads = 10
  67. }
  68. }
  69. // 初始化 Emby API 接口
  70. if downloader.reqParam.EmbyConfig.Url != "" && downloader.reqParam.EmbyConfig.ApiKey != "" {
  71. downloader.embyHelper = embyHelper.NewEmbyHelper(downloader.reqParam.EmbyConfig)
  72. }
  73. } else {
  74. downloader.reqParam = *types.NewReqParam()
  75. }
  76. // 强制下载线程为 1,太猛,不然都是错误
  77. downloader.reqParam.Threads = 1
  78. // 这里就不单独弄一个 reqParam.SubNameFormatter 字段来传递值了,因为 inSubFormatter 就已经知道是什么 formatter 了
  79. downloader.subNameFormatter = subcommon.FormatterName(downloader.subFormatter.GetFormatterFormatterName())
  80. var sitesSequence = make([]string, 0)
  81. // TODO 这里写固定了抉择字幕的顺序
  82. sitesSequence = append(sitesSequence, common.SubSiteZiMuKu)
  83. sitesSequence = append(sitesSequence, common.SubSiteSubHd)
  84. sitesSequence = append(sitesSequence, common.SubSiteShooter)
  85. sitesSequence = append(sitesSequence, common.SubSiteXunLei)
  86. downloader.mk = markSystem.NewMarkingSystem(sitesSequence, downloader.reqParam.SubTypePriority)
  87. downloader.movieFileFullPathList = make([]string, 0)
  88. downloader.seriesSubNeedDlMap = make(map[string][]emby.EmbyMixInfo)
  89. // 初始化,字幕校正的实例
  90. downloader.subTimelineFixerHelperEx = sub_timeline_fixer.NewSubTimelineFixerHelperEx(downloader.reqParam.SubTimelineFixerConfig)
  91. if downloader.reqParam.FixTimeLine == true {
  92. downloader.subTimelineFixerHelperEx.Check()
  93. }
  94. return &downloader
  95. }
  96. // ReadSpeFile 优先级最高。读取特殊文件,启用一些特殊的功能,比如 forced_scan_and_down_sub
  97. func (d *Downloader) ReadSpeFile() error {
  98. // 理论上是一次性的,用了这个文件就应该没了
  99. // 强制的字幕扫描
  100. needProcess_forced_scan_and_down_sub, err := forced_scan_and_down_sub.CheckSpeFile()
  101. if err != nil {
  102. return err
  103. }
  104. d.needForcedScanAndDownSub = needProcess_forced_scan_and_down_sub
  105. // 从 csf-bk 文件还原时间轴修复前的字幕文件
  106. needProcess_restore_fix_timeline_bk, err := restore_fix_timeline_bk.CheckSpeFile()
  107. if err != nil {
  108. return err
  109. }
  110. d.NeedRestoreFixTimeLineBK = needProcess_restore_fix_timeline_bk
  111. return nil
  112. }
  113. // GetUpdateVideoListFromEmby 这里首先会进行近期影片的获取,然后对这些影片进行刷新,然后在获取字幕列表,最终得到需要字幕获取的 video 列表
  114. func (d *Downloader) GetUpdateVideoListFromEmby(movieRootDir, seriesRootDir string) error {
  115. if d.embyHelper == nil {
  116. return nil
  117. }
  118. var err error
  119. var movieList []emby.EmbyMixInfo
  120. movieList, d.seriesSubNeedDlMap, err = d.embyHelper.GetRecentlyAddVideoList(movieRootDir, seriesRootDir)
  121. if err != nil {
  122. return err
  123. }
  124. // 获取全路径
  125. for _, info := range movieList {
  126. d.movieFileFullPathList = append(d.movieFileFullPathList, info.VideoFileFullPath)
  127. }
  128. // 输出调试信息
  129. log_helper.GetLogger().Debugln("GetUpdateVideoListFromEmby - DebugInfo - seriesSubNeedDlMap Start")
  130. for s, _ := range d.seriesSubNeedDlMap {
  131. log_helper.GetLogger().Debugln(s)
  132. }
  133. log_helper.GetLogger().Debugln("GetUpdateVideoListFromEmby - DebugInfo - seriesSubNeedDlMap End")
  134. log_helper.GetLogger().Debugln("GetUpdateVideoListFromEmby - DebugInfo - movieFileFullPathList Start")
  135. for s, value := range d.movieFileFullPathList {
  136. log_helper.GetLogger().Debugln(s, value)
  137. }
  138. log_helper.GetLogger().Debugln("GetUpdateVideoListFromEmby - DebugInfo - movieFileFullPathList End")
  139. return nil
  140. }
  141. func (d Downloader) RefreshEmbySubList() error {
  142. if d.embyHelper == nil {
  143. return nil
  144. }
  145. bRefresh := false
  146. defer func() {
  147. if bRefresh == true {
  148. d.log.Infoln("Refresh Emby Sub List Success")
  149. } else {
  150. d.log.Errorln("Refresh Emby Sub List Error")
  151. }
  152. }()
  153. bRefresh, err := d.embyHelper.RefreshEmbySubList()
  154. if err != nil {
  155. return err
  156. }
  157. return nil
  158. }
  159. // DownloadSub4Movie 这里对接 Emby 的时候比较方便,只要更新 d.movieFileFullPathList 就行了,不像连续剧那么麻烦
  160. func (d Downloader) DownloadSub4Movie(dir string) error {
  161. defer func() {
  162. // 所有的电影字幕下载完成,抉择完成,需要清理缓存目录
  163. err := my_util.ClearRootTmpFolder()
  164. if err != nil {
  165. d.log.Error("ClearRootTmpFolder", err)
  166. }
  167. d.log.Infoln("Download Movie Sub End...")
  168. }()
  169. var err error
  170. d.log.Infoln("Download Movie Sub Started...")
  171. // -----------------------------------------------------
  172. // 优先判断特殊的操作
  173. if d.needForcedScanAndDownSub == true {
  174. // 全扫描
  175. d.movieFileFullPathList, err = my_util.SearchMatchedVideoFile(dir)
  176. if err != nil {
  177. return err
  178. }
  179. } else {
  180. // 是否是通过 emby_helper api 获取的列表
  181. if d.embyHelper == nil {
  182. // 没有填写 emby_helper api 的信息,那么就走常规的全文件扫描流程
  183. d.movieFileFullPathList, err = my_util.SearchMatchedVideoFile(dir)
  184. if err != nil {
  185. return err
  186. }
  187. } else {
  188. // 进过 emby_helper api 的信息读取
  189. d.log.Infoln("Movie Sub Dl From Emby API...")
  190. if len(d.movieFileFullPathList) < 1 {
  191. d.log.Infoln("Movie Sub Dl From Emby API no movie need Dl sub")
  192. return nil
  193. }
  194. }
  195. }
  196. // -----------------------------------------------------
  197. // 并发控制
  198. movieDlFunc := func(i interface{}) error {
  199. inData := i.(InputData)
  200. // -----------------------------------------------------
  201. // 构建每个字幕站点下载者的实例
  202. var subSupplierHub = subSupplier.NewSubSupplierHub(
  203. //subhd.NewSupplier(d.reqParam),
  204. zimuku.NewSupplier(d.reqParam),
  205. xunlei.NewSupplier(d.reqParam),
  206. shooter.NewSupplier(d.reqParam),
  207. )
  208. if common.SubhdCode != "" {
  209. // 如果找到 code 了,那么就可以继续用这个实例
  210. subSupplierHub.AddSubSupplier(subhd.NewSupplier(d.reqParam))
  211. }
  212. // 字幕都下载缓存好了,需要抉择存哪一个,优先选择中文双语的,然后到中文
  213. organizeSubFiles, err := subSupplierHub.DownloadSub4Movie(inData.OneVideoFullPath, inData.Index, d.needForcedScanAndDownSub)
  214. if err != nil {
  215. d.log.Errorln("subSupplierHub.DownloadSub4Movie", inData.OneVideoFullPath, err)
  216. return err
  217. }
  218. // 返回的两个值都是 nil 的时候,就是无需下载字幕,那么同样不用输出额外的信息,因为之前会输出跳过的原因
  219. if organizeSubFiles == nil {
  220. return nil
  221. }
  222. // 去搜索了没有发现字幕
  223. if len(organizeSubFiles) < 1 {
  224. d.log.Infoln("no sub found", filepath.Base(inData.OneVideoFullPath))
  225. return nil
  226. }
  227. d.oneVideoSelectBestSub(inData.OneVideoFullPath, organizeSubFiles)
  228. // -----------------------------------------------------
  229. return nil
  230. }
  231. // -----------------------------------------------------
  232. antPool, err := ants.NewPoolWithFunc(d.reqParam.Threads, func(inData interface{}) {
  233. data := inData.(InputData)
  234. defer data.Wg.Done()
  235. ctx, cancel := context.WithTimeout(context.Background(), common.OneMovieProcessTimeOut)
  236. defer cancel()
  237. done := make(chan error, 1)
  238. panicChan := make(chan interface{}, 1)
  239. go func() {
  240. defer func() {
  241. if p := recover(); p != nil {
  242. panicChan <- p
  243. }
  244. }()
  245. done <- movieDlFunc(inData)
  246. }()
  247. select {
  248. case err := <-done:
  249. if err != nil {
  250. d.log.Errorln("DownloadSub4Movie.NewPoolWithFunc done with Error", err.Error())
  251. }
  252. return
  253. case p := <-panicChan:
  254. d.log.Errorln("DownloadSub4Movie.NewPoolWithFunc got panic", p)
  255. return
  256. case <-ctx.Done():
  257. d.log.Errorln("DownloadSub4Movie.NewPoolWithFunc got time out", ctx.Err())
  258. return
  259. }
  260. })
  261. if err != nil {
  262. return err
  263. }
  264. defer antPool.Release()
  265. wg := sync.WaitGroup{}
  266. // 一个视频文件同时多个站点查询,阻塞完毕后,在进行下一个
  267. for i, oneVideoFullPath := range d.movieFileFullPathList {
  268. wg.Add(1)
  269. err = antPool.Invoke(InputData{OneVideoFullPath: oneVideoFullPath, Index: i, Wg: &wg})
  270. if err != nil {
  271. d.log.Errorln("DownloadSub4Movie ants.Invoke", err)
  272. }
  273. }
  274. wg.Wait()
  275. return nil
  276. }
  277. func (d Downloader) DownloadSub4Series(dir string) error {
  278. var err error
  279. defer func() {
  280. // 所有的连续剧字幕下载完成,抉择完成,需要清理缓存目录
  281. err := my_util.ClearRootTmpFolder()
  282. if err != nil {
  283. d.log.Error("ClearRootTmpFolder", err)
  284. }
  285. d.log.Infoln("Download Series Sub End...")
  286. }()
  287. d.log.Infoln("Download Series Sub Started...")
  288. // 并发控制
  289. seriesDlFunc := func(i interface{}) error {
  290. inData := i.(InputData)
  291. // 构建每个字幕站点下载者的实例
  292. var subSupplierHub *subSupplier.SubSupplierHub
  293. subSupplierHub = subSupplier.NewSubSupplierHub(
  294. zimuku.NewSupplier(d.reqParam),
  295. //subhd.NewSupplier(d.reqParam),
  296. xunlei.NewSupplier(d.reqParam),
  297. shooter.NewSupplier(d.reqParam),
  298. )
  299. // 这里拿到了这一部连续剧的所有的剧集信息,以及所有下载到的字幕信息
  300. var seriesInfo *series.SeriesInfo
  301. var organizeSubFiles map[string][]string
  302. // 优先判断特殊的操作
  303. if d.needForcedScanAndDownSub == true {
  304. // 全盘扫描
  305. seriesInfo, organizeSubFiles, err = subSupplierHub.DownloadSub4Series(inData.OneVideoFullPath, inData.Index, d.needForcedScanAndDownSub)
  306. if err != nil {
  307. d.log.Errorln("subSupplierHub.DownloadSub4Series", inData.OneVideoFullPath, err)
  308. return err
  309. }
  310. } else {
  311. // 是否是通过 emby_helper api 获取的列表
  312. if d.embyHelper == nil {
  313. seriesInfo, organizeSubFiles, err = subSupplierHub.DownloadSub4Series(inData.OneVideoFullPath, inData.Index, d.needForcedScanAndDownSub)
  314. if err != nil {
  315. d.log.Errorln("subSupplierHub.DownloadSub4Series", inData.OneVideoFullPath, err)
  316. return err
  317. }
  318. } else {
  319. // 先进性 emby_helper api 的操作,读取需要更新字幕的项目
  320. seriesInfo, organizeSubFiles, err = subSupplierHub.DownloadSub4SeriesFromEmby(
  321. filepath.Join(dir, inData.OneVideoFullPath),
  322. d.seriesSubNeedDlMap[inData.OneVideoFullPath], inData.Index)
  323. if err != nil {
  324. d.log.Errorln("subSupplierHub.DownloadSub4Series", inData.OneVideoFullPath, err)
  325. return err
  326. }
  327. }
  328. }
  329. if organizeSubFiles == nil || len(organizeSubFiles) < 1 {
  330. d.log.Infoln("no sub found", filepath.Base(inData.OneVideoFullPath))
  331. return nil
  332. }
  333. // 只针对需要下载字幕的视频进行字幕的选择保存
  334. for epsKey, episodeInfo := range seriesInfo.NeedDlEpsKeyList {
  335. // 匹配对应的 Eps 去处理
  336. d.oneVideoSelectBestSub(episodeInfo.FileFullPath, organizeSubFiles[epsKey])
  337. }
  338. // 这里会拿到一份季度字幕的列表比如,Key 是 S1E0 S2E0 S3E0,value 是新的存储位置
  339. fullSeasonSubDict := d.saveFullSeasonSub(seriesInfo, organizeSubFiles)
  340. // TODO 季度的字幕包,应该优先于零散的字幕吧,暂定就这样了,注意是全部都替换
  341. // 需要与有下载需求的季交叉
  342. for _, episodeInfo := range seriesInfo.EpList {
  343. _, ok := seriesInfo.NeedDlSeasonDict[episodeInfo.Season]
  344. if ok == false {
  345. continue
  346. }
  347. // 匹配对应的 Eps 去处理
  348. seasonEpsKey := my_util.GetEpisodeKeyName(episodeInfo.Season, episodeInfo.Episode)
  349. d.oneVideoSelectBestSub(episodeInfo.FileFullPath, fullSeasonSubDict[seasonEpsKey])
  350. }
  351. // 是否清理全季的缓存字幕文件夹
  352. if d.reqParam.SaveOneSeasonSub == false {
  353. err = sub_helper.DeleteOneSeasonSubCacheFolder(seriesInfo.DirPath)
  354. if err != nil {
  355. return err
  356. }
  357. }
  358. return nil
  359. }
  360. antPool, err := ants.NewPoolWithFunc(d.reqParam.Threads, func(inData interface{}) {
  361. data := inData.(InputData)
  362. defer data.Wg.Done()
  363. ctx, cancel := context.WithTimeout(context.Background(), common.OneSeriesProcessTimeOut)
  364. defer cancel()
  365. done := make(chan error, 1)
  366. panicChan := make(chan interface{}, 1)
  367. go func() {
  368. defer func() {
  369. if p := recover(); p != nil {
  370. panicChan <- p
  371. }
  372. }()
  373. done <- seriesDlFunc(inData)
  374. }()
  375. select {
  376. case err := <-done:
  377. if err != nil {
  378. d.log.Errorln("DownloadSub4Series.NewPoolWithFunc done with Error", err.Error())
  379. }
  380. return
  381. case p := <-panicChan:
  382. d.log.Errorln("DownloadSub4Series.NewPoolWithFunc got panic", p)
  383. case <-ctx.Done():
  384. d.log.Errorln("DownloadSub4Series.NewPoolWithFunc got time out", ctx.Err())
  385. return
  386. }
  387. })
  388. if err != nil {
  389. return err
  390. }
  391. defer antPool.Release()
  392. // 是否是通过 emby_helper api 获取的列表
  393. var seriesDirList = make([]string, 0)
  394. if d.embyHelper == nil {
  395. // 遍历连续剧总目录下的第一层目录
  396. seriesDirList, err = seriesHelper.GetSeriesList(dir)
  397. if err != nil {
  398. return err
  399. }
  400. for index, seriesDir := range seriesDirList {
  401. d.log.Debugln("embyHelper == nil GetSeriesList", index, seriesDir)
  402. }
  403. } else {
  404. // 这里给出的是连续剧的文件夹名称
  405. d.log.Debugln("embyHelper seriesSubNeedDlMap Count:", len(d.seriesSubNeedDlMap))
  406. for s, _ := range d.seriesSubNeedDlMap {
  407. seriesDirList = append(seriesDirList, s)
  408. d.log.Debugln("embyHelper seriesSubNeedDlMap:", s)
  409. }
  410. }
  411. wg := sync.WaitGroup{}
  412. for i, oneSeriesPath := range seriesDirList {
  413. wg.Add(1)
  414. err = antPool.Invoke(InputData{OneVideoFullPath: oneSeriesPath, Index: i, Wg: &wg})
  415. if err != nil {
  416. d.log.Errorln("DownloadSub4Series ants.Invoke", err)
  417. }
  418. }
  419. wg.Wait()
  420. return nil
  421. }
  422. func (d Downloader) RestoreFixTimelineBK(moviesDir, seriesDir string) error {
  423. defer d.log.Infoln("End Restore Fix Timeline BK")
  424. d.log.Infoln("Start Restore Fix Timeline BK...")
  425. _, err := sub_timeline_fixer_pkg.Restore(moviesDir, seriesDir)
  426. if err != nil {
  427. return err
  428. }
  429. return nil
  430. }
  431. // oneVideoSelectBestSub 一个视频,选择最佳的一个字幕(也可以保存所有网站第一个最佳字幕)
  432. func (d Downloader) oneVideoSelectBestSub(oneVideoFullPath string, organizeSubFiles []string) {
  433. // 如果没有则直接跳过
  434. if organizeSubFiles == nil || len(organizeSubFiles) < 1 {
  435. return
  436. }
  437. var err error
  438. // 得到目标视频文件的文件名
  439. videoFileName := filepath.Base(oneVideoFullPath)
  440. // -------------------------------------------------
  441. // 调试缓存,把下载好的字幕写到对应的视频目录下,方便调试
  442. if d.reqParam.DebugMode == true {
  443. err = my_util.CopyFiles2DebugFolder([]string{videoFileName}, organizeSubFiles)
  444. if err != nil {
  445. d.log.Errorln("copySubFile2DesFolder", err)
  446. }
  447. }
  448. // -------------------------------------------------
  449. /*
  450. 这里需要额外考虑一点,有可能当前目录已经有一个 .Default .Forced 标记的字幕了
  451. 那么下载字幕丢进来的时候就需要提前把这个字幕找出来,去除整个 .Default .Forced 标记
  452. 然后进行正常的下载,存储和替换字幕,最后将本次操作的第一次标记为 .Default
  453. */
  454. // 不管是不是保存多个字幕,都要先扫描本地的字幕,进行 .Default .Forced 去除
  455. // 这个视频的所有字幕,去除 .default .Forced 标记
  456. err = sub_helper.SearchVideoMatchSubFileAndRemoveExtMark(oneVideoFullPath)
  457. if err != nil {
  458. // 找个错误可以忍
  459. d.log.Errorln("SearchVideoMatchSubFileAndRemoveExtMark,", oneVideoFullPath, err)
  460. }
  461. if d.reqParam.SaveMultiSub == false {
  462. // 选择最优的一个字幕
  463. var finalSubFile *subparser.FileInfo
  464. finalSubFile = d.mk.SelectOneSubFile(organizeSubFiles)
  465. if finalSubFile == nil {
  466. d.log.Warnln("Found", len(organizeSubFiles), " subtitles but not one fit:", oneVideoFullPath)
  467. return
  468. }
  469. /*
  470. 这里还有一个梗,Emby、jellyfin 支持 default 和 forced 扩展字段
  471. 但是,plex 只支持 forced
  472. 那么就比较麻烦,干脆,normal 的命名格式化实例,就不设置 default 了,forced 不想用,因为可能会跟你手动选择的字幕冲突(下次观看的时候,理论上也可能不会)
  473. */
  474. // 判断配置文件中的字幕命名格式化的选择
  475. bSetDefault := true
  476. if d.subNameFormatter == subcommon.Normal {
  477. bSetDefault = false
  478. }
  479. // 找到了,写入文件
  480. err = d.writeSubFile2VideoPath(oneVideoFullPath, *finalSubFile, "", bSetDefault, false)
  481. if err != nil {
  482. d.log.Errorln("SaveMultiSub:", d.reqParam.SaveMultiSub, "writeSubFile2VideoPath:", err)
  483. return
  484. }
  485. } else {
  486. // 每个网站 Top1 的字幕
  487. siteNames, finalSubFiles := d.mk.SelectEachSiteTop1SubFile(organizeSubFiles)
  488. if len(siteNames) < 0 {
  489. d.log.Warnln("SelectEachSiteTop1SubFile found none sub file")
  490. return
  491. }
  492. // 多网站 Top 1 字幕保存的时候,第一个设置为 Default 即可
  493. /*
  494. 由于新功能支持了字幕命名格式的选择,那么如果触发了多个字幕保存的逻辑,如果不调整
  495. 则会遇到,top1 先写入,然后 top2 覆盖 top1 ,以此类推的情况出现
  496. 所以如果开启了 Normal SubNameFormatter 的功能,则要反序写入文件
  497. 如果是 Emby 的字幕命名格式则无需考虑此问题,因为每个网站只会有一个字幕,且字幕命名格式决定了不会重复写入覆盖
  498. */
  499. if d.subNameFormatter == subcommon.Emby {
  500. for i, file := range finalSubFiles {
  501. setDefault := false
  502. if i == 0 {
  503. setDefault = true
  504. }
  505. err = d.writeSubFile2VideoPath(oneVideoFullPath, file, siteNames[i], setDefault, false)
  506. if err != nil {
  507. d.log.Errorln("SaveMultiSub:", d.reqParam.SaveMultiSub, "writeSubFile2VideoPath:", err)
  508. return
  509. }
  510. }
  511. } else {
  512. // 默认这里就是 normal 模式
  513. // 逆序写入
  514. /*
  515. 这里还有一个梗,Emby、jellyfin 支持 default 和 forced 扩展字段
  516. 但是,plex 只支持 forced
  517. 那么就比较麻烦,干脆,normal 的命名格式化实例,就不设置 default 了,forced 不想用,因为可能会跟你手动选择的字幕冲突(下次观看的时候,理论上也可能不会)
  518. */
  519. for i := len(finalSubFiles) - 1; i > -1; i-- {
  520. err = d.writeSubFile2VideoPath(oneVideoFullPath, finalSubFiles[i], siteNames[i], false, false)
  521. if err != nil {
  522. d.log.Errorln("SaveMultiSub:", d.reqParam.SaveMultiSub, "writeSubFile2VideoPath:", err)
  523. return
  524. }
  525. }
  526. }
  527. }
  528. // -------------------------------------------------
  529. }
  530. // saveFullSeasonSub 这里就需要单独存储到连续剧每一季的文件夹的特殊文件夹中。需要跟 DeleteOneSeasonSubCacheFolder 关联起来
  531. func (d Downloader) saveFullSeasonSub(seriesInfo *series.SeriesInfo, organizeSubFiles map[string][]string) map[string][]string {
  532. var fullSeasonSubDict = make(map[string][]string)
  533. for _, season := range seriesInfo.SeasonDict {
  534. seasonKey := my_util.GetEpisodeKeyName(season, 0)
  535. subs, ok := organizeSubFiles[seasonKey]
  536. if ok == false {
  537. continue
  538. }
  539. for _, sub := range subs {
  540. subFileName := filepath.Base(sub)
  541. newSeasonSubRootPath, err := my_util.GetDebugFolderByName([]string{
  542. filepath.Base(seriesInfo.DirPath),
  543. "Sub_" + seasonKey})
  544. if err != nil {
  545. d.log.Errorln("saveFullSeasonSub.GetDebugFolderByName", subFileName, err)
  546. continue
  547. }
  548. newSubFullPath := filepath.Join(newSeasonSubRootPath, subFileName)
  549. err = my_util.CopyFile(sub, newSubFullPath)
  550. if err != nil {
  551. d.log.Errorln("saveFullSeasonSub.CopyFile", subFileName, err)
  552. continue
  553. }
  554. // 从字幕的文件名推断是 哪一季 的 那一集
  555. _, gusSeason, gusEpisode, err := decode.GetSeasonAndEpisodeFromSubFileName(subFileName)
  556. if err != nil {
  557. return nil
  558. }
  559. // 把整季的字幕缓存位置也提供出去,如果之前没有下载到的,这里返回出来的可以补上
  560. seasonEpsKey := my_util.GetEpisodeKeyName(gusSeason, gusEpisode)
  561. _, ok := fullSeasonSubDict[seasonEpsKey]
  562. if ok == false {
  563. // 初始化
  564. fullSeasonSubDict[seasonEpsKey] = make([]string, 0)
  565. }
  566. fullSeasonSubDict[seasonEpsKey] = append(fullSeasonSubDict[seasonEpsKey], sub)
  567. }
  568. }
  569. return fullSeasonSubDict
  570. }
  571. // 在前面需要进行语言的筛选、排序,这里仅仅是存储, extraSubPreName 这里传递是字幕的网站,有就认为是多字幕的存储。空就是单字幕,单字幕就可以setDefault
  572. func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFile subparser.FileInfo, extraSubPreName string, setDefault bool, skipExistFile bool) error {
  573. defer d.log.Infoln("----------------------------------")
  574. videoRootPath := filepath.Dir(videoFileFullPath)
  575. subNewName, subNewNameWithDefault, _ := d.subFormatter.GenerateMixSubName(videoFileFullPath, finalSubFile.Ext, finalSubFile.Lang, extraSubPreName)
  576. desSubFullPath := filepath.Join(videoRootPath, subNewName)
  577. if setDefault == true {
  578. // 先判断没有 default 的字幕是否存在了,在的话,先删除,然后再写入
  579. if my_util.IsFile(desSubFullPath) == true {
  580. _ = os.Remove(desSubFullPath)
  581. }
  582. desSubFullPath = filepath.Join(videoRootPath, subNewNameWithDefault)
  583. }
  584. if skipExistFile == true {
  585. // 需要判断文件是否存在在,有则跳过
  586. if my_util.IsFile(desSubFullPath) == true {
  587. d.log.Infoln("OrgSubName:", finalSubFile.Name)
  588. d.log.Infoln("Sub Skip DownAt:", desSubFullPath)
  589. return nil
  590. }
  591. }
  592. // 最后写入字幕
  593. err := my_util.WriteFile(desSubFullPath, finalSubFile.Data)
  594. if err != nil {
  595. return err
  596. }
  597. d.log.Infoln("----------------------------------")
  598. d.log.Infoln("OrgSubName:", finalSubFile.Name)
  599. d.log.Infoln("SubDownAt:", desSubFullPath)
  600. // 然后还需要判断是否需要校正字幕的时间轴
  601. if d.reqParam.FixTimeLine == true {
  602. err = d.subTimelineFixerHelperEx.Process(videoFileFullPath, desSubFullPath)
  603. if err != nil {
  604. return err
  605. }
  606. }
  607. return nil
  608. }
  609. type InputData struct {
  610. OneVideoFullPath string
  611. Index int
  612. Wg *sync.WaitGroup
  613. }