search.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package my_util
  2. import (
  3. "io/fs"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "github.com/allanpk716/ChineseSubFinder/pkg/sub_parser_hub"
  9. "github.com/allanpk716/ChineseSubFinder/pkg/decode"
  10. "github.com/allanpk716/ChineseSubFinder/pkg/filter"
  11. "github.com/allanpk716/ChineseSubFinder/pkg/sort_things"
  12. "github.com/emirpasic/gods/maps/treemap"
  13. "github.com/sirupsen/logrus"
  14. )
  15. // VideoNameSearchKeywordMaker 拼接视频搜索的 title 和 年份
  16. func VideoNameSearchKeywordMaker(l *logrus.Logger, title string, year string) string {
  17. iYear, err := strconv.Atoi(year)
  18. if err != nil {
  19. // 允许的错误
  20. l.Errorln("VideoNameSearchKeywordMaker", "year to int", err)
  21. iYear = 0
  22. }
  23. searchKeyword := title
  24. if iYear >= 2020 {
  25. searchKeyword = searchKeyword + " " + year
  26. }
  27. return searchKeyword
  28. }
  29. // SearchMatchedVideoFileFromDirs 搜索符合后缀名的视频文件
  30. func SearchMatchedVideoFileFromDirs(l *logrus.Logger, dirs []string) (*treemap.Map, error) {
  31. defer func() {
  32. l.Infoln("SearchMatchedVideoFileFromDirs End")
  33. l.Infoln(" --------------------------------------------------")
  34. }()
  35. l.Infoln(" --------------------------------------------------")
  36. l.Infoln("SearchMatchedVideoFileFromDirs Start...")
  37. var fileFullPathMap = treemap.NewWithStringComparator()
  38. for _, dir := range dirs {
  39. matchedVideoFile, err := SearchMatchedVideoFile(l, dir)
  40. if err != nil {
  41. return nil, err
  42. }
  43. value, found := fileFullPathMap.Get(dir)
  44. if found == false {
  45. fileFullPathMap.Put(dir, matchedVideoFile)
  46. } else {
  47. value = append(value.([]string), matchedVideoFile...)
  48. fileFullPathMap.Put(dir, value)
  49. }
  50. }
  51. fileFullPathMap.Each(func(seriesRootPathName interface{}, seriesNames interface{}) {
  52. oneSeriesRootPathName := seriesRootPathName.(string)
  53. fileFullPathList := seriesNames.([]string)
  54. // 排序,从最新的到最早的
  55. fileFullPathList = sort_things.SortByModTime(fileFullPathList)
  56. for _, s := range fileFullPathList {
  57. l.Debugln(s)
  58. }
  59. fileFullPathMap.Put(oneSeriesRootPathName, fileFullPathList)
  60. })
  61. return fileFullPathMap, nil
  62. }
  63. // SearchMatchedVideoFile 搜索符合后缀名的视频文件,现在也会把 BDMV 的文件搜索出来,但是这个并不是一个视频文件,需要在后续特殊处理
  64. func SearchMatchedVideoFile(l *logrus.Logger, dir string) ([]string, error) {
  65. var fileFullPathList = make([]string, 0)
  66. pathSep := string(os.PathSeparator)
  67. files, err := os.ReadDir(dir)
  68. if err != nil {
  69. return nil, err
  70. }
  71. for _, curFile := range files {
  72. fullPath := dir + pathSep + curFile.Name()
  73. if curFile.IsDir() {
  74. // 内层的错误就无视了
  75. oneList, _ := SearchMatchedVideoFile(l, fullPath)
  76. if oneList != nil {
  77. fileFullPathList = append(fileFullPathList, oneList...)
  78. }
  79. } else {
  80. // 这里就是文件了
  81. bok, fakeBDMVVideoFile := FileNameIsBDMV(fullPath)
  82. if bok == true {
  83. // 这类文件后续的扫描字幕操作需要额外的处理
  84. fileFullPathList = append(fileFullPathList, fakeBDMVVideoFile)
  85. continue
  86. }
  87. if IsWantedVideoExtDef(curFile.Name()) == false {
  88. // 不是期望的视频后缀名则跳过
  89. continue
  90. } else {
  91. // 这里还有一种情况,就是蓝光, BDMV 下面会有一个 STREAM 文件夹,里面很多 m2ts 的视频组成
  92. if filepath.Base(filepath.Dir(fullPath)) == "STREAM" {
  93. l.Debugln("SearchMatchedVideoFile, Skip BDMV.STREAM:", fullPath)
  94. continue
  95. }
  96. if filter.SkipFileInfo(l, curFile) == true {
  97. continue
  98. }
  99. fileFullPathList = append(fileFullPathList, fullPath)
  100. }
  101. }
  102. }
  103. return fileFullPathList, nil
  104. }
  105. func SearchTVNfo(l *logrus.Logger, dir string) ([]string, error) {
  106. var fileFullPathList = make([]string, 0)
  107. pathSep := string(os.PathSeparator)
  108. files, err := os.ReadDir(dir)
  109. if err != nil {
  110. return nil, err
  111. }
  112. for _, curFile := range files {
  113. fullPath := dir + pathSep + curFile.Name()
  114. if curFile.IsDir() {
  115. // 内层的错误就无视了
  116. oneList, _ := SearchTVNfo(l, fullPath)
  117. if oneList != nil {
  118. fileFullPathList = append(fileFullPathList, oneList...)
  119. }
  120. } else {
  121. // 这里就是文件了
  122. if strings.ToLower(curFile.Name()) != decode.MetadateTVNfo {
  123. continue
  124. } else {
  125. if filter.SkipFileInfo(l, curFile) == true {
  126. continue
  127. }
  128. fileFullPathList = append(fileFullPathList, fullPath)
  129. }
  130. }
  131. }
  132. return fileFullPathList, nil
  133. }
  134. // SearchSeriesAllEpsAndSubtitles 遍历这个连续剧目录下的所有视频文件,以及这个视频文件对应的字幕文件
  135. func SearchSeriesAllEpsAndSubtitles(l *logrus.Logger, dir string) {
  136. pathVideoMap := make(map[string][]string, 0)
  137. pathSubsMap := make(map[string][]string, 0)
  138. err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
  139. if err != nil {
  140. return err
  141. }
  142. if d.IsDir() == true {
  143. return nil
  144. }
  145. if IsWantedVideoExtDef(filepath.Ext(d.Name())) == true {
  146. // 如果是符合视频的后缀名,那么就缓存起来
  147. _, found := pathVideoMap[path]
  148. if found == false {
  149. pathVideoMap[path] = make([]string, 0)
  150. }
  151. pathVideoMap[path] = append(pathVideoMap[path], path)
  152. return nil
  153. }
  154. if sub_parser_hub.IsSubExtWanted(filepath.Ext(d.Name())) == true {
  155. // 如果是符合字幕的后缀名,那么就缓存起来
  156. _, found := pathSubsMap[path]
  157. if found == false {
  158. pathSubsMap[path] = make([]string, 0)
  159. }
  160. pathSubsMap[path] = append(pathSubsMap[path], path)
  161. return nil
  162. }
  163. return nil
  164. })
  165. if err != nil {
  166. return
  167. }
  168. }