sub_format_changer.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package sub_formatter
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/allanpk716/ChineseSubFinder/internal/dao"
  6. "github.com/allanpk716/ChineseSubFinder/internal/ifaces"
  7. movieHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/movie_helper"
  8. seriesHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
  9. "github.com/allanpk716/ChineseSubFinder/internal/models"
  10. "github.com/allanpk716/ChineseSubFinder/internal/pkg"
  11. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  12. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
  13. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/emby"
  14. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/normal"
  15. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  16. "github.com/allanpk716/ChineseSubFinder/internal/types"
  17. "os"
  18. "strings"
  19. )
  20. type SubFormatChanger struct {
  21. movieRootDir string
  22. seriesRootDir string
  23. formatter map[string]ifaces.ISubFormatter
  24. }
  25. func NewSubFormatChanger(movieRootDir string, seriesRootDir string) *SubFormatChanger {
  26. formatter := SubFormatChanger{movieRootDir: movieRootDir, seriesRootDir: seriesRootDir}
  27. // 初始化支持的 formatter
  28. // normal
  29. formatter.formatter = make(map[string]ifaces.ISubFormatter)
  30. normalM := normal.NewFormatter()
  31. formatter.formatter[normalM.GetFormatterName()] = normalM
  32. // emby
  33. embyM := emby.NewFormatter()
  34. formatter.formatter[embyM.GetFormatterName()] = embyM
  35. return &formatter
  36. }
  37. // AutoDetectThenChangeTo 自动检测字幕的命名格式,然后转换到目标的 formatter 上
  38. func (s SubFormatChanger) AutoDetectThenChangeTo(desFormatter common.FormatterName) (RenameResults, error) {
  39. var err error
  40. outStruct := RenameResults{}
  41. outStruct.RenamedFiles = make(map[string]int)
  42. outStruct.ErrFiles = make(map[string]int)
  43. if pkg.IsDir(s.movieRootDir) == false {
  44. return outStruct, errors.New("movieRootDir path not exist: " + s.movieRootDir)
  45. }
  46. if pkg.IsDir(s.seriesRootDir) == false {
  47. return outStruct, errors.New("seriesRootDir path not exist: " + s.seriesRootDir)
  48. }
  49. // 先找出有那些电影文件夹和连续剧文件夹
  50. var movieFullPathList = make([]string, 0)
  51. movieFullPathList, err = pkg.SearchMatchedVideoFile(s.movieRootDir)
  52. if err != nil {
  53. return outStruct, err
  54. }
  55. seriesDirList, err := seriesHelper.GetSeriesList(s.seriesRootDir)
  56. if err != nil {
  57. return outStruct, err
  58. }
  59. // 搜索所有的字幕,找到相关的字幕进行修改
  60. for _, one := range movieFullPathList {
  61. found := false
  62. var fitMovieNameSubList = make([]string, 0)
  63. found, _, fitMovieNameSubList, err = movieHelper.MovieHasChineseSub(one)
  64. if err != nil || found == false {
  65. continue
  66. }
  67. // 判断是否是符合要求
  68. for _, fitSubName := range fitMovieNameSubList {
  69. s.autoDetectAndChange(&outStruct, fitSubName, desFormatter)
  70. }
  71. }
  72. // 连续剧
  73. var seriesSubFiles = make([]string, 0)
  74. for _, oneSeriesDir := range seriesDirList {
  75. seriesSubFiles, err = sub_helper.SearchMatchedSubFile(oneSeriesDir)
  76. if err != nil {
  77. return outStruct, err
  78. }
  79. // 判断是否是符合要求
  80. for _, fitSubName := range seriesSubFiles {
  81. s.autoDetectAndChange(&outStruct, fitSubName, desFormatter)
  82. }
  83. }
  84. return outStruct, nil
  85. }
  86. // autoDetectAndChange 自动检测命名格式,然后修改至目标的命名格式
  87. func (s SubFormatChanger) autoDetectAndChange(outStruct *RenameResults, fitSubName string, desFormatter common.FormatterName) {
  88. for _, formatter := range s.formatter {
  89. bok, fileNameWithOutExt, subExt, subLang, extraSubPreName := formatter.IsMatchThisFormat(fitSubName)
  90. if bok == false {
  91. continue
  92. }
  93. // 如果检测到的格式和目标要转换到的格式是一个,那么就跳过
  94. if common.FormatterName(formatter.GetFormatterFormatterName()) == desFormatter {
  95. return
  96. }
  97. // 这里得到的 subExt 可能是 .ass or .default.ass or .forced.ass
  98. // 需要进行剔除,因为后续的 GenerateMixSubName 会自动生成对应的附加后缀名
  99. // 转换格式后,需要保留之前的 default 或者 forced
  100. findDefault := false
  101. findForce := false
  102. if strings.Contains(subExt, types.Sub_Ext_Mark_Default) == true {
  103. subExt = strings.Replace(subExt, types.Sub_Ext_Mark_Default, "", -1)
  104. findDefault = true
  105. }
  106. if strings.Contains(subExt, types.Sub_Ext_Mark_Forced) == true {
  107. subExt = strings.Replace(subExt, types.Sub_Ext_Mark_Forced, "", -1)
  108. findForce = true
  109. }
  110. // 通过传入的目标格式化 formatter 的名称去调用
  111. newSubFileName := ""
  112. newName, newDefaultName, newForcedName := s.formatter[fmt.Sprintf("%s", desFormatter)].
  113. GenerateMixSubNameBase(fileNameWithOutExt, subExt, subLang, extraSubPreName)
  114. if findDefault == false && findForce == false {
  115. // 使用没得额外 Default 或者 Forced 的名称即可
  116. newSubFileName = newName
  117. } else if findDefault == true {
  118. newSubFileName = newDefaultName
  119. } else if findForce == true {
  120. newSubFileName = newForcedName
  121. }
  122. if newSubFileName == "" {
  123. continue
  124. }
  125. // 确认改格式
  126. err := os.Rename(fitSubName, newSubFileName)
  127. if err != nil {
  128. tmpName := pkg.FixWindowPathBackSlash(fitSubName)
  129. outStruct.ErrFiles[tmpName] += 1
  130. continue
  131. } else {
  132. tmpName := pkg.FixWindowPathBackSlash(newSubFileName)
  133. outStruct.RenamedFiles[tmpName] += 1
  134. }
  135. }
  136. }
  137. type RenameResults struct {
  138. RenamedFiles map[string]int
  139. ErrFiles map[string]int
  140. }
  141. // GetSubFormatter 选择字幕命名格式化的实例
  142. func GetSubFormatter(subNameFormatter int) ifaces.ISubFormatter {
  143. var subFormatter ifaces.ISubFormatter
  144. switch subNameFormatter {
  145. case int(common.Emby):
  146. {
  147. subFormatter = emby.NewFormatter()
  148. break
  149. }
  150. case int(common.Normal):
  151. {
  152. subFormatter = normal.NewFormatter()
  153. break
  154. }
  155. default:
  156. {
  157. subFormatter = emby.NewFormatter()
  158. break
  159. }
  160. }
  161. return subFormatter
  162. }
  163. // SubFormatChangerProcess 执行 SubFormatChanger 逻辑,并且更新数据库缓存
  164. func SubFormatChangerProcess(movieRootDir string, seriesRootDir string, nowDesFormatter common.FormatterName) (RenameResults, error) {
  165. var subFormatRec models.SubFormatRec
  166. dao.GetDb().First(&subFormatRec)
  167. subFormatChanger := NewSubFormatChanger(movieRootDir, seriesRootDir)
  168. // 理论上有且仅有一条记录
  169. if subFormatRec.Done == false {
  170. // 没有找到,认为是第一次执行
  171. renameResults, err := subFormatChanger.AutoDetectThenChangeTo(nowDesFormatter)
  172. if err != nil {
  173. return renameResults, err
  174. }
  175. // 需要记录到数据库中
  176. oneSubFormatter := models.SubFormatRec{FormatName: int(nowDesFormatter), Done: true}
  177. dao.GetDb().Create(&oneSubFormatter)
  178. return renameResults, nil
  179. } else {
  180. // 找到了,需要判断上一次执行的目标 formatter 是啥,如果这次的目标 formatter 不一样则执行
  181. // 如果是一样的则跳过
  182. if common.FormatterName(subFormatRec.FormatName) == nowDesFormatter {
  183. log_helper.GetLogger().Infoln("DesSubFormatter == LateTimeSubFormatter then skip process")
  184. return RenameResults{}, nil
  185. }
  186. // 执行更改
  187. renameResults, err := subFormatChanger.AutoDetectThenChangeTo(nowDesFormatter)
  188. if err != nil {
  189. return renameResults, err
  190. }
  191. // 更新数据库
  192. subFormatRec.FormatName = int(nowDesFormatter)
  193. subFormatRec.Done = true
  194. dao.GetDb().Save(subFormatRec)
  195. return renameResults, nil
  196. }
  197. }