srt.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package srt
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/common"
  4. "github.com/allanpk716/ChineseSubFinder/internal/pkg/language"
  5. "github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
  6. "io/ioutil"
  7. "path/filepath"
  8. "regexp"
  9. "strings"
  10. )
  11. type Parser struct {
  12. }
  13. func NewParser() *Parser {
  14. return &Parser{}
  15. }
  16. func (p Parser) GetParserName() string {
  17. return "srt"
  18. }
  19. /*
  20. DetermineFileTypeFromFile 确定字幕文件的类型,是双语字幕或者某一种语言等等信息
  21. 当 error 是 common.DetermineFileTypeFromFileExtNotFitSRT
  22. 需要额外的处理逻辑,比如不用报错,而是跳过后续的逻辑
  23. */
  24. func (p Parser) DetermineFileTypeFromFile(filePath string) (bool, *subparser.FileInfo, error) {
  25. nowExt := filepath.Ext(filePath)
  26. if strings.ToLower(nowExt) != common.SubExtSRT {
  27. return false, nil, nil
  28. }
  29. fBytes, err := ioutil.ReadFile(filePath)
  30. if err != nil {
  31. return false, nil, err
  32. }
  33. inBytes, err := language.ChangeFileCoding2UTF8(fBytes)
  34. if err != nil {
  35. return false, nil, err
  36. }
  37. return p.DetermineFileTypeFromBytes(inBytes, nowExt)
  38. }
  39. // DetermineFileTypeFromBytes 确定字幕文件的类型,是双语字幕或者某一种语言等等信息
  40. func (p Parser) DetermineFileTypeFromBytes(inBytes []byte, nowExt string) (bool, *subparser.FileInfo, error) {
  41. allString := string(inBytes)
  42. // 注意,需要替换掉 \r 不然正则表达式会有问题
  43. allString = strings.ReplaceAll(allString, "\r", "")
  44. re := regexp.MustCompile(regString)
  45. // 找到 start end text
  46. matched := re.FindAllStringSubmatch(allString, -1)
  47. if len(matched) < 1 {
  48. return false, nil, nil
  49. }
  50. subFileInfo := subparser.FileInfo{}
  51. subFileInfo.Ext = nowExt
  52. subFileInfo.Dialogues = make([]subparser.OneDialogue, 0)
  53. // 这里需要统计一共有几个 \N,以及这个数量在整体行数中的比例,这样就知道是不是双语字幕了
  54. countLineFeed := 0
  55. for _, oneDial := range matched {
  56. startTime := oneDial[2]
  57. endTime := oneDial[3]
  58. nowText := oneDial[4]
  59. odl := subparser.OneDialogue{
  60. StartTime: startTime,
  61. EndTime: endTime,
  62. }
  63. odl.Lines = make([]string, 0)
  64. nowText = strings.TrimRight(nowText, "\n")
  65. texts := strings.Split(nowText, "\n")
  66. for i, text := range texts {
  67. if i == 1 {
  68. // 这样说明有两行字幕,也就是双语啦
  69. countLineFeed++
  70. }
  71. odl.Lines = append(odl.Lines, text)
  72. }
  73. subFileInfo.Dialogues = append(subFileInfo.Dialogues, odl)
  74. }
  75. // 再分析
  76. // 需要判断每一个 Line 是啥语言,[语言的code]次数
  77. var langDict map[int]int
  78. langDict = make(map[int]int)
  79. // 抽取出所有的中文对话
  80. var chLines = make([]string, 0)
  81. // 抽取出所有的第二语言对话
  82. var otherLines = make([]string, 0)
  83. for _, dialogue := range subFileInfo.Dialogues {
  84. language.DetectSubLangAndStatistics(dialogue.Lines, langDict, &chLines, &otherLines)
  85. }
  86. // 从统计出来的字典,找出 Top 1 或者 2 的出来,然后计算出是什么语言的字幕
  87. detectLang := language.SubLangStatistics2SubLangType(float32(countLineFeed), float32(len(matched)), langDict, chLines)
  88. subFileInfo.Lang = detectLang
  89. subFileInfo.Data = inBytes
  90. subFileInfo.CHLines = chLines
  91. subFileInfo.OtherLines = otherLines
  92. return true, &subFileInfo, nil
  93. }
  94. const regString = `(\d+)\n([\d:,]+)\s+-{2}\>\s+([\d:,]+)\n([\s\S]*?(\n{2}|$))`