normal.go 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/language"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
  8. languageConst "github.com/allanpk716/ChineseSubFinder/internal/types/language"
  9. "github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
  10. "path/filepath"
  11. "regexp"
  12. "strings"
  13. )
  14. type Formatter struct {
  15. subParser *sub_parser_hub.SubParserHub
  16. }
  17. func NewFormatter() *Formatter {
  18. return &Formatter{subParser: sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())}
  19. }
  20. // GetFormatterName 当前的 Formatter 是那个
  21. func (f Formatter) GetFormatterName() string {
  22. return common.FormatterNameString_Normal
  23. }
  24. func (f Formatter) GetFormatterFormatterName() int {
  25. return int(common.Normal)
  26. }
  27. // IsMatchThisFormat 是否满足当前实现接口的字幕命名格式 - 是否符合规则、fileNameWithOutExt string, subExt string, subLang types.MyLanguage, extraSubPreName string
  28. func (f Formatter) IsMatchThisFormat(subName string) (bool, string, string, languageConst.MyLanguage, string) {
  29. /*
  30. Emby 的命名规则比较特殊,而且本程序就是做中文字幕下载的,所以,下面的正则表达式比较特殊
  31. 见本程序内 internal/types/language/ISOLanguage.go 这里的支持 ISO 规范和中文编码变种
  32. 见文档、讨论:
  33. https://emby.media/community/index.php?/topic/94504-current-chinese-subtitle-filter-not-so-accurate-and-hope-improve-like-this/
  34. https://en.wikipedia.org/wiki/Chinese_Wikipedia#Automatic_conversion_between_traditional_and_simplified_Chinese_characters
  35. https://stackoverflow.com/questions/18902072/what-standard-do-language-codes-of-the-form-zh-hans-belong-to
  36. */
  37. //subName = strings.ToLower(subName)
  38. var re = regexp.MustCompile(language.ISOSupportRegexRule())
  39. matched := re.FindAllStringSubmatch(subName, -1)
  40. /*
  41. 详细看测试用例
  42. The Boss Baby Family Business (2021) WEBDL-1080p.zh.ass
  43. The Boss Baby Family Business (2021) WEBDL-1080p.zh.default.ass
  44. The Boss Baby Family Business (2021) WEBDL-1080p.zh.forced.ass
  45. The Boss Baby Family Business (2021) WEBDL-1080p.chi.ass
  46. The Boss Baby Family Business (2021) WEBDL-1080p.chi.default.ass
  47. The Boss Baby Family Business (2021) WEBDL-1080p.chi.forced.ass
  48. The Boss Baby Family Business (2021) WEBDL-1080p.zho.ass
  49. The Boss Baby Family Business (2021) WEBDL-1080p.zho.default.ass
  50. The Boss Baby Family Business (2021) WEBDL-1080p.zho.forced.ass
  51. [0][0] .zh.ass
  52. [0][1] zh
  53. [0][2] .ass
  54. */
  55. if matched == nil || len(matched) < 1 || len(matched[0]) < 3 {
  56. return false, "", "", languageConst.Unknown, ""
  57. }
  58. var subLang languageConst.MyLanguage
  59. var extraSubPreName string
  60. fileNameWithOutExt := strings.ReplaceAll(subName, matched[0][0], "")
  61. subExt := matched[0][2]
  62. var subLangStr = matched[0][1]
  63. extraSubPreName = ""
  64. // 这里有一个点,是直接从 zh zho ch 去转换成中文语言就行了,还是要做字幕的语言识别
  65. // 目前倾向于这里用后面的逻辑
  66. subLang = language.ISOString2SupportLang(subLangStr)
  67. return true, fileNameWithOutExt, subExt, subLang, extraSubPreName
  68. }
  69. // GenerateMixSubName 通过视频和字幕信息,生成当前实现接口的字幕命名格式。extraSubPreName 一般是填写字幕网站,不填写则留空 - 新名称、新名称带有 default 标记,新名称带有 forced 标记
  70. func (f Formatter) GenerateMixSubName(videoFileName, subExt string, subLang languageConst.MyLanguage, extraSubPreName string) (string, string, string) {
  71. /*
  72. 这里会生成类似的文件名 xxxx.zh
  73. */
  74. videoFileNameWithOutExt := strings.ReplaceAll(filepath.Base(videoFileName),
  75. filepath.Ext(videoFileName), "")
  76. return f.GenerateMixSubNameBase(videoFileNameWithOutExt, subExt, subLang, extraSubPreName)
  77. }
  78. func (f Formatter) GenerateMixSubNameBase(fileNameWithOutExt, subExt string, subLang languageConst.MyLanguage, extraSubPreName string) (string, string, string) {
  79. // 这里传入字幕后缀名的时候,可能会带有 default 或者 forced 字段,需要剔除
  80. nowSubExt := strings.ReplaceAll(subExt, subparser.Sub_Ext_Mark_Default, "")
  81. nowSubExt = strings.ReplaceAll(nowSubExt, subparser.Sub_Ext_Mark_Forced, "")
  82. subNewName := fileNameWithOutExt + "." + languageConst.ISO_639_1_Chinese + nowSubExt
  83. subNewNameWithDefault := fileNameWithOutExt + "." + languageConst.ISO_639_1_Chinese + subparser.Sub_Ext_Mark_Default + nowSubExt
  84. subNewNameWithForced := fileNameWithOutExt + "." + languageConst.ISO_639_1_Chinese + subparser.Sub_Ext_Mark_Forced + nowSubExt
  85. return subNewName, subNewNameWithDefault, subNewNameWithForced
  86. }