normal.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package normal
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/ass"
  4. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
  7. "github.com/allanpk716/ChineseSubFinder/internal/types"
  8. "path/filepath"
  9. "regexp"
  10. "strings"
  11. )
  12. type Formatter struct {
  13. subParser *sub_parser_hub.SubParserHub
  14. }
  15. func NewFormatter() *Formatter {
  16. return &Formatter{subParser: sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())}
  17. }
  18. // GetFormatterName 当前的 Formatter 是那个
  19. func (f Formatter) GetFormatterName() string {
  20. return common.FormatterNameString_Normal
  21. }
  22. func (f Formatter) GetFormatterFormatterName() int {
  23. return int(common.Normal)
  24. }
  25. // IsMatchThisFormat 是否满足当前实现接口的字幕命名格式 - 是否符合规则、fileNameWithOutExt string, subExt string, subLang types.Language, extraSubPreName string
  26. func (f Formatter) IsMatchThisFormat(subName string) (bool, string, string, types.Language, string) {
  27. /*
  28. Emby 的命名规则比较特殊,而且本程序就是做中文字幕下载的,所以,下面的正则表达式比较特殊
  29. */
  30. var re = regexp.MustCompile(`(?m)\.(\bzh\b|\bzho\b|\bchi\b)(\.\S+)`)
  31. matched := re.FindAllStringSubmatch(subName, -1)
  32. /*
  33. The Boss Baby Family Business (2021) WEBDL-1080p.zh.ass
  34. The Boss Baby Family Business (2021) WEBDL-1080p.zh.default.ass
  35. The Boss Baby Family Business (2021) WEBDL-1080p.zh.forced.ass
  36. The Boss Baby Family Business (2021) WEBDL-1080p.chi.ass
  37. The Boss Baby Family Business (2021) WEBDL-1080p.chi.default.ass
  38. The Boss Baby Family Business (2021) WEBDL-1080p.chi.forced.ass
  39. The Boss Baby Family Business (2021) WEBDL-1080p.zho.ass
  40. The Boss Baby Family Business (2021) WEBDL-1080p.zho.default.ass
  41. The Boss Baby Family Business (2021) WEBDL-1080p.zho.forced.ass
  42. [0][0] .zh.ass
  43. [0][1] zh
  44. [0][2] .ass
  45. */
  46. if matched == nil || len(matched) < 1 || len(matched[0]) < 3 {
  47. return false, "", "", types.Unknow, ""
  48. }
  49. var subLang types.Language
  50. var extraSubPreName string
  51. fileNameWithOutExt := strings.ReplaceAll(subName, matched[0][0], "")
  52. subExt := matched[0][2]
  53. //var subLangStr = matched[0][1]
  54. extraSubPreName = ""
  55. // 这里有一个点,是直接从 zh zho ch 去转换成中文语言就行了,还是要做字幕的语言识别
  56. // 目前倾向于这里用后面的逻辑
  57. //subLang = language.ChineseISOString2Lang(subLangStr)
  58. file, err := f.subParser.DetermineFileTypeFromFile(subName)
  59. if err != nil {
  60. return false, "", "", 0, ""
  61. }
  62. subLang = file.Lang
  63. return true, fileNameWithOutExt, subExt, subLang, extraSubPreName
  64. }
  65. // GenerateMixSubName 通过视频和字幕信息,生成当前实现接口的字幕命名格式。extraSubPreName 一般是填写字幕网站,不填写则留空 - 新名称、新名称带有 default 标记,新名称带有 forced 标记
  66. func (f Formatter) GenerateMixSubName(videoFileName, subExt string, subLang types.Language, extraSubPreName string) (string, string, string) {
  67. /*
  68. 这里会生成类似的文件名 xxxx.zh
  69. */
  70. videoFileNameWithOutExt := strings.ReplaceAll(filepath.Base(videoFileName),
  71. filepath.Ext(videoFileName), "")
  72. return f.GenerateMixSubNameBase(videoFileNameWithOutExt, subExt, subLang, extraSubPreName)
  73. }
  74. func (f Formatter) GenerateMixSubNameBase(fileNameWithOutExt, subExt string, subLang types.Language, extraSubPreName string) (string, string, string) {
  75. subNewName := fileNameWithOutExt + "." + types.ChineseAbbr_639_1 + subExt
  76. subNewNameWithDefault := fileNameWithOutExt + "." + types.ChineseAbbr_639_1 + types.Sub_Ext_Mark_Default + subExt
  77. subNewNameWithForced := fileNameWithOutExt + "." + types.ChineseAbbr_639_1 + types.Sub_Ext_Mark_Forced + subExt
  78. return subNewName, subNewNameWithDefault, subNewNameWithForced
  79. }