downloader_things.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package downloader
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
  4. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  5. subcommon "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  7. "github.com/allanpk716/ChineseSubFinder/internal/types/series"
  8. "github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
  9. "os"
  10. "path/filepath"
  11. )
  12. // oneVideoSelectBestSub 一个视频,选择最佳的一个字幕(也可以保存所有网站第一个最佳字幕)
  13. func (d Downloader) oneVideoSelectBestSub(oneVideoFullPath string, organizeSubFiles []string) {
  14. // 如果没有则直接跳过
  15. if organizeSubFiles == nil || len(organizeSubFiles) < 1 {
  16. return
  17. }
  18. var err error
  19. // 得到目标视频文件的文件名
  20. videoFileName := filepath.Base(oneVideoFullPath)
  21. // -------------------------------------------------
  22. // 调试缓存,把下载好的字幕写到对应的视频目录下,方便调试
  23. if d.reqParam.DebugMode == true {
  24. err = my_util.CopyFiles2DebugFolder([]string{videoFileName}, organizeSubFiles)
  25. if err != nil {
  26. d.log.Errorln("copySubFile2DesFolder", err)
  27. }
  28. }
  29. // -------------------------------------------------
  30. /*
  31. 这里需要额外考虑一点,有可能当前目录已经有一个 .Default .Forced 标记的字幕了
  32. 那么下载字幕丢进来的时候就需要提前把这个字幕找出来,去除整个 .Default .Forced 标记
  33. 然后进行正常的下载,存储和替换字幕,最后将本次操作的第一次标记为 .Default
  34. */
  35. // 不管是不是保存多个字幕,都要先扫描本地的字幕,进行 .Default .Forced 去除
  36. // 这个视频的所有字幕,去除 .default .Forced 标记
  37. err = sub_helper.SearchVideoMatchSubFileAndRemoveExtMark(oneVideoFullPath)
  38. if err != nil {
  39. // 找个错误可以忍
  40. d.log.Errorln("SearchVideoMatchSubFileAndRemoveExtMark,", oneVideoFullPath, err)
  41. }
  42. if d.reqParam.SaveMultiSub == false {
  43. // 选择最优的一个字幕
  44. var finalSubFile *subparser.FileInfo
  45. finalSubFile = d.mk.SelectOneSubFile(organizeSubFiles)
  46. if finalSubFile == nil {
  47. d.log.Warnln("Found", len(organizeSubFiles), " subtitles but not one fit:", oneVideoFullPath)
  48. return
  49. }
  50. /*
  51. 这里还有一个梗,Emby、jellyfin 支持 default 和 forced 扩展字段
  52. 但是,plex 只支持 forced
  53. 那么就比较麻烦,干脆,normal 的命名格式化实例,就不设置 default 了,forced 不想用,因为可能会跟你手动选择的字幕冲突(下次观看的时候,理论上也可能不会)
  54. */
  55. // 判断配置文件中的字幕命名格式化的选择
  56. bSetDefault := true
  57. if d.subNameFormatter == subcommon.Normal {
  58. bSetDefault = false
  59. }
  60. // 找到了,写入文件
  61. err = d.writeSubFile2VideoPath(oneVideoFullPath, *finalSubFile, "", bSetDefault, false)
  62. if err != nil {
  63. d.log.Errorln("SaveMultiSub:", d.reqParam.SaveMultiSub, "writeSubFile2VideoPath:", err)
  64. return
  65. }
  66. } else {
  67. // 每个网站 Top1 的字幕
  68. siteNames, finalSubFiles := d.mk.SelectEachSiteTop1SubFile(organizeSubFiles)
  69. if len(siteNames) < 0 {
  70. d.log.Warnln("SelectEachSiteTop1SubFile found none sub file")
  71. return
  72. }
  73. // 多网站 Top 1 字幕保存的时候,第一个设置为 Default 即可
  74. /*
  75. 由于新功能支持了字幕命名格式的选择,那么如果触发了多个字幕保存的逻辑,如果不调整
  76. 则会遇到,top1 先写入,然后 top2 覆盖 top1 ,以此类推的情况出现
  77. 所以如果开启了 Normal SubNameFormatter 的功能,则要反序写入文件
  78. 如果是 Emby 的字幕命名格式则无需考虑此问题,因为每个网站只会有一个字幕,且字幕命名格式决定了不会重复写入覆盖
  79. */
  80. if d.subNameFormatter == subcommon.Emby {
  81. for i, file := range finalSubFiles {
  82. setDefault := false
  83. if i == 0 {
  84. setDefault = true
  85. }
  86. err = d.writeSubFile2VideoPath(oneVideoFullPath, file, siteNames[i], setDefault, false)
  87. if err != nil {
  88. d.log.Errorln("SaveMultiSub:", d.reqParam.SaveMultiSub, "writeSubFile2VideoPath:", err)
  89. return
  90. }
  91. }
  92. } else {
  93. // 默认这里就是 normal 模式
  94. // 逆序写入
  95. /*
  96. 这里还有一个梗,Emby、jellyfin 支持 default 和 forced 扩展字段
  97. 但是,plex 只支持 forced
  98. 那么就比较麻烦,干脆,normal 的命名格式化实例,就不设置 default 了,forced 不想用,因为可能会跟你手动选择的字幕冲突(下次观看的时候,理论上也可能不会)
  99. */
  100. for i := len(finalSubFiles) - 1; i > -1; i-- {
  101. err = d.writeSubFile2VideoPath(oneVideoFullPath, finalSubFiles[i], siteNames[i], false, false)
  102. if err != nil {
  103. d.log.Errorln("SaveMultiSub:", d.reqParam.SaveMultiSub, "writeSubFile2VideoPath:", err)
  104. return
  105. }
  106. }
  107. }
  108. }
  109. // -------------------------------------------------
  110. }
  111. // saveFullSeasonSub 这里就需要单独存储到连续剧每一季的文件夹的特殊文件夹中。需要跟 DeleteOneSeasonSubCacheFolder 关联起来
  112. func (d Downloader) saveFullSeasonSub(seriesInfo *series.SeriesInfo, organizeSubFiles map[string][]string) map[string][]string {
  113. var fullSeasonSubDict = make(map[string][]string)
  114. for _, season := range seriesInfo.SeasonDict {
  115. seasonKey := my_util.GetEpisodeKeyName(season, 0)
  116. subs, ok := organizeSubFiles[seasonKey]
  117. if ok == false {
  118. continue
  119. }
  120. for _, sub := range subs {
  121. subFileName := filepath.Base(sub)
  122. newSeasonSubRootPath, err := my_util.GetDebugFolderByName([]string{
  123. filepath.Base(seriesInfo.DirPath),
  124. "Sub_" + seasonKey})
  125. if err != nil {
  126. d.log.Errorln("saveFullSeasonSub.GetDebugFolderByName", subFileName, err)
  127. continue
  128. }
  129. newSubFullPath := filepath.Join(newSeasonSubRootPath, subFileName)
  130. err = my_util.CopyFile(sub, newSubFullPath)
  131. if err != nil {
  132. d.log.Errorln("saveFullSeasonSub.CopyFile", subFileName, err)
  133. continue
  134. }
  135. // 从字幕的文件名推断是 哪一季 的 那一集
  136. _, gusSeason, gusEpisode, err := decode.GetSeasonAndEpisodeFromSubFileName(subFileName)
  137. if err != nil {
  138. return nil
  139. }
  140. // 把整季的字幕缓存位置也提供出去,如果之前没有下载到的,这里返回出来的可以补上
  141. seasonEpsKey := my_util.GetEpisodeKeyName(gusSeason, gusEpisode)
  142. _, ok := fullSeasonSubDict[seasonEpsKey]
  143. if ok == false {
  144. // 初始化
  145. fullSeasonSubDict[seasonEpsKey] = make([]string, 0)
  146. }
  147. fullSeasonSubDict[seasonEpsKey] = append(fullSeasonSubDict[seasonEpsKey], sub)
  148. }
  149. }
  150. return fullSeasonSubDict
  151. }
  152. // 在前面需要进行语言的筛选、排序,这里仅仅是存储, extraSubPreName 这里传递是字幕的网站,有就认为是多字幕的存储。空就是单字幕,单字幕就可以setDefault
  153. func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFile subparser.FileInfo, extraSubPreName string, setDefault bool, skipExistFile bool) error {
  154. defer d.log.Infoln("----------------------------------")
  155. videoRootPath := filepath.Dir(videoFileFullPath)
  156. subNewName, subNewNameWithDefault, _ := d.subFormatter.GenerateMixSubName(videoFileFullPath, finalSubFile.Ext, finalSubFile.Lang, extraSubPreName)
  157. desSubFullPath := filepath.Join(videoRootPath, subNewName)
  158. if setDefault == true {
  159. // 先判断没有 default 的字幕是否存在了,在的话,先删除,然后再写入
  160. if my_util.IsFile(desSubFullPath) == true {
  161. _ = os.Remove(desSubFullPath)
  162. }
  163. desSubFullPath = filepath.Join(videoRootPath, subNewNameWithDefault)
  164. }
  165. if skipExistFile == true {
  166. // 需要判断文件是否存在在,有则跳过
  167. if my_util.IsFile(desSubFullPath) == true {
  168. d.log.Infoln("OrgSubName:", finalSubFile.Name)
  169. d.log.Infoln("Sub Skip DownAt:", desSubFullPath)
  170. return nil
  171. }
  172. }
  173. // 最后写入字幕
  174. err := my_util.WriteFile(desSubFullPath, finalSubFile.Data)
  175. if err != nil {
  176. return err
  177. }
  178. d.log.Infoln("----------------------------------")
  179. d.log.Infoln("OrgSubName:", finalSubFile.Name)
  180. d.log.Infoln("SubDownAt:", desSubFullPath)
  181. // 然后还需要判断是否需要校正字幕的时间轴
  182. if d.reqParam.FixTimeLine == true {
  183. err = d.subTimelineFixerHelperEx.Process(videoFileFullPath, desSubFullPath)
  184. if err != nil {
  185. return err
  186. }
  187. }
  188. return nil
  189. }
  190. type DownloadInputData struct {
  191. OneVideoFullPath string
  192. OneSeriesPath string
  193. RootDirPath string
  194. }