ass.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package ass
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/pkg/language"
  4. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/regex_things"
  6. "github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
  7. "github.com/emirpasic/gods/maps/treemap"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. )
  12. type Parser struct {
  13. }
  14. func NewParser() *Parser {
  15. return &Parser{}
  16. }
  17. func (p Parser) GetParserName() string {
  18. return "ass"
  19. }
  20. /*
  21. DetermineFileTypeFromFile 确定字幕文件的类型,是双语字幕或者某一种语言等等信息
  22. 当 error 是 common.DetermineFileTypeFromFileExtNotFitASSorSSA
  23. 需要额外的处理逻辑,比如不用报错,而是跳过后续的逻辑
  24. */
  25. func (p Parser) DetermineFileTypeFromFile(filePath string) (bool, *subparser.FileInfo, error) {
  26. nowExt := filepath.Ext(filePath)
  27. log_helper.GetLogger().Debugln("DetermineFileTypeFromFile", p.GetParserName(), filePath)
  28. fBytes, err := os.ReadFile(filePath)
  29. if err != nil {
  30. return false, nil, err
  31. }
  32. inBytes, err := language.ChangeFileCoding2UTF8(fBytes)
  33. if err != nil {
  34. return false, nil, err
  35. }
  36. return p.DetermineFileTypeFromBytes(inBytes, nowExt)
  37. }
  38. // DetermineFileTypeFromBytes 确定字幕文件的类型,是双语字幕或者某一种语言等等信息
  39. func (p Parser) DetermineFileTypeFromBytes(inBytes []byte, nowExt string) (bool, *subparser.FileInfo, error) {
  40. allString := string(inBytes)
  41. // 注意,需要替换掉 \r 不然正则表达式会有问题
  42. allString = strings.ReplaceAll(allString, "\r", "")
  43. // 找到 start end text
  44. matched := regex_things.ReMatchDialogueASS.FindAllStringSubmatch(allString, -1)
  45. if matched == nil || len(matched) < 1 {
  46. log_helper.GetLogger().Debugln("DetermineFileTypeFromBytes can't found DialoguesFilter, Skip")
  47. return false, nil, nil
  48. }
  49. subFileInfo := subparser.FileInfo{}
  50. subFileInfo.Content = string(inBytes)
  51. subFileInfo.Ext = nowExt
  52. subFileInfo.Dialogues = make([]subparser.OneDialogue, 0)
  53. subFileInfo.DialoguesFilter = make([]subparser.OneDialogue, 0)
  54. // 这里需要统计一共有几个 \N,以及这个数量在整体行数中的比例,这样就知道是不是双语字幕了
  55. countLineFeed := 0
  56. // 有意义的对话统计数,排除 Style 类型
  57. usefullyDialogueCount := 0
  58. // 先进行字幕 StyleName 的出现次数排序,找到最多的,就是常规字幕的,不是特效的
  59. var nameMap = make(map[string]int)
  60. for _, oneLine := range matched {
  61. nowStyleName := oneLine[3]
  62. _, ok := nameMap[nowStyleName]
  63. if ok == false {
  64. nameMap[nowStyleName] = 1
  65. } else {
  66. nameMap[nowStyleName]++
  67. }
  68. }
  69. /*
  70. 现在可能会遇到两种可能出现的双语字幕:
  71. 1.
  72. 一个 Dialogue 中,直接描述两个语言
  73. 2.
  74. 排序的目标是找出 Name 有几种,一般来说都是 Default 一种
  75. 但是目前也会有用这个 Name 来做双语标记的
  76. 比如相同的时间点:一个 Name 是 Chs Subtitle
  77. 一个 Name 是 Eng Subtitle
  78. 那么排序来说,就应该是 Top1、2 两个
  79. 但是之前是为了剔除某一些特效动画,进行排序后只找 Top 1,但是遇到上面 2 的情况
  80. 解析就只读取到一个语言的字幕了
  81. 那么现在的解决方案就是,一开始先进行 Name 的统计。
  82. 然后统计是否有一个相同的时间段,出现了两个 Dialogue,比如:
  83. 0:01:01.00-0:01:11.00 这个时间段,一共有两个 Dialogue 使用了,然后需要统计这种情况占比所有的 Dialogue 的比例
  84. 如果比例很高,那么就认为是情况 2 的双语字幕
  85. 如果没有那么多,或者就没得。就任务是情况 1 的双语字幕,这个也不能说就是双语字幕,只不过走之前的逻辑就够了。
  86. */
  87. mapByValue := sortMapByValue(nameMap)
  88. // 把所有的对白缓存下来,其实优先是把时间信息缓存,其他信息无所谓
  89. p.oneLineSubDialogueParser0(matched, &subFileInfo)
  90. if p.detectOneOrTwoLineDialogue(matched) == true {
  91. // 情况1
  92. usefullyDialogueCount, countLineFeed = p.oneLineSubDialogueParser1(matched, mapByValue, &subFileInfo)
  93. } else {
  94. // 情况2
  95. usefullyDialogueCount, countLineFeed = p.oneLineSubDialogueParser2(matched, mapByValue, &subFileInfo)
  96. }
  97. // 再分析
  98. // 需要判断每一个 Line 是啥语言,[语言的code]次数
  99. var langDict map[int]int
  100. langDict = make(map[int]int)
  101. // 抽取出所有的中文对话
  102. var chLines = make([]string, 0)
  103. // 抽取出所有的第二语言对话
  104. var otherLines = make([]string, 0)
  105. // 抽取出来的对话数组,为了后续用来匹配和修改时间轴
  106. var usefulDialogueExs = make([]subparser.OneDialogueEx, 0)
  107. // 在这之前需要把 subFileInfo.DialoguesFilter 的内容填好,Lines 这里如果是单种语言应该就是一个元素,如果是双语就需要拆分成两个元素
  108. // 这样向后传递就简单了,也统一了
  109. emptyLines := 0
  110. for _, dialogue := range subFileInfo.DialoguesFilter {
  111. emptyLines += language.DetectSubLangAndStatistics(dialogue, langDict, &usefulDialogueExs, &chLines, &otherLines)
  112. }
  113. // 从统计出来的字典,找出 Top 1 或者 2 的出来,然后计算出是什么语言的字幕
  114. detectLang := language.SubLangStatistics2SubLangType(float32(countLineFeed), float32(usefullyDialogueCount-emptyLines), langDict, chLines)
  115. subFileInfo.Lang = detectLang
  116. subFileInfo.Data = inBytes
  117. subFileInfo.DialoguesFilterEx = usefulDialogueExs
  118. subFileInfo.CHLines = chLines
  119. subFileInfo.OtherLines = otherLines
  120. return true, &subFileInfo, nil
  121. }
  122. // oneLineSubDialogueParser0 情况 0 时候的解析器,不过滤,只要是对白都加进去
  123. func (p Parser) oneLineSubDialogueParser0(matched [][]string, subFileInfo *subparser.FileInfo) {
  124. for _, oneLine := range matched {
  125. startTime := oneLine[1]
  126. endTime := oneLine[2]
  127. nowStyleName := oneLine[3]
  128. nowText := oneLine[4]
  129. odl := subparser.OneDialogue{
  130. StyleName: nowStyleName,
  131. StartTime: startTime,
  132. EndTime: endTime,
  133. Lines: []string{nowText},
  134. }
  135. subFileInfo.Dialogues = append(subFileInfo.Dialogues, odl)
  136. }
  137. }
  138. // oneLineSubDialogueParser1 情况 1 时候的解析器
  139. func (p Parser) oneLineSubDialogueParser1(matched [][]string, mapByValue StyleNameInfos, subFileInfo *subparser.FileInfo) (int, int) {
  140. var countLineFeed = 0
  141. var usefullyDialogueCount = 0
  142. // 先读取一次字幕文件
  143. for _, oneLine := range matched {
  144. // 排除特效内容,只统计有意义的对话部分
  145. if strings.Contains(oneLine[0], mapByValue[0].Name) == false {
  146. continue
  147. }
  148. usefullyDialogueCount++
  149. startTime := oneLine[1]
  150. endTime := oneLine[2]
  151. nowStyleName := oneLine[3]
  152. nowText := oneLine[4]
  153. odl := subparser.OneDialogue{
  154. StyleName: nowStyleName,
  155. StartTime: startTime,
  156. EndTime: endTime,
  157. }
  158. odl.Lines = make([]string, 0)
  159. countLineFeed = p.parseOneDialogueText(nowText, &odl, countLineFeed)
  160. subFileInfo.DialoguesFilter = append(subFileInfo.DialoguesFilter, odl)
  161. }
  162. return usefullyDialogueCount, countLineFeed
  163. }
  164. // oneLineSubDialogueParser2 情况 2 时候的解析器
  165. func (p Parser) oneLineSubDialogueParser2(matched [][]string, mapByValue StyleNameInfos, subFileInfo *subparser.FileInfo) (int, int) {
  166. var countLineFeed = 0
  167. var usefullyDialogueCount = 0
  168. //var timeMap = make(map[string]subparser.OneDialogue, 0)
  169. // 更换数据结构的原因是为了能够使用顺序,go 内置的 map 不是顺序的,是随机的,会导致后续的逻辑出问题
  170. var timeMap = treemap.NewWithStringComparator()
  171. // 先读取一次字幕文件
  172. for _, oneLine := range matched {
  173. usefullyDialogueCount++
  174. // 这里可能会统计到特效的部分,但是这里忽略这个问题,因为目标不是这个
  175. // 统计 Dialogue 的开始和结束时间
  176. startTime := oneLine[1]
  177. endTime := oneLine[2]
  178. nowStyleName := oneLine[3]
  179. nowText := oneLine[4]
  180. mergeTime := startTime + "_" + endTime
  181. value, ok := timeMap.Get(mergeTime)
  182. if ok == false {
  183. // 首次新增
  184. odl := subparser.OneDialogue{
  185. StyleName: nowStyleName,
  186. StartTime: startTime,
  187. EndTime: endTime,
  188. }
  189. odl.Lines = make([]string, 0)
  190. countLineFeed = p.parseOneDialogueText(nowText, &odl, countLineFeed)
  191. timeMap.Put(mergeTime, odl)
  192. } else {
  193. // 双语
  194. odl := value.(subparser.OneDialogue)
  195. countLineFeed = p.parseOneDialogueText(nowText, &odl, countLineFeed)
  196. timeMap.Put(mergeTime, odl)
  197. }
  198. }
  199. for _, value := range timeMap.Values() {
  200. odl := value.(subparser.OneDialogue)
  201. subFileInfo.DialoguesFilter = append(subFileInfo.DialoguesFilter, odl)
  202. }
  203. return usefullyDialogueCount, countLineFeed
  204. }
  205. // parseOneDialogueText 对话的对白内容解析
  206. func (p Parser) parseOneDialogueText(nowText string, odl *subparser.OneDialogue, countLineFeed int) int {
  207. // nowText 优先移除 \h 这个是替换空格, \h 是让两个词在一行,不换行显示
  208. nowText = strings.ReplaceAll(nowText, `\h`, " ")
  209. // nowText 这个需要先把 {} 花括号内的内容给移除
  210. nowText1 := regex_things.ReMatchBrace.ReplaceAllString(nowText, "")
  211. nowText1 = regex_things.ReMatchBracket.ReplaceAllString(nowText1, "")
  212. nowText1 = strings.TrimRight(nowText1, "\r")
  213. // 然后判断是否有 \N 或者 \n
  214. // 直接把 \n 替换为 \N 来解析
  215. nowText1 = strings.ReplaceAll(nowText1, `\n`, `\N`)
  216. if strings.Contains(nowText1, `\N`) {
  217. // 有,那么就需要再次切割,一般是双语字幕
  218. for _, matched2 := range regex_things.ReCutDoubleLanguage.FindAllStringSubmatch(nowText1, -1) {
  219. if matched2 == nil {
  220. continue
  221. }
  222. for i, s := range matched2 {
  223. if i == 0 {
  224. continue
  225. }
  226. s = strings.ReplaceAll(s, `\N`, "")
  227. odl.Lines = append(odl.Lines, s)
  228. }
  229. }
  230. countLineFeed++
  231. } else {
  232. // 无,则可以直接添加
  233. nowText1 = strings.ReplaceAll(nowText1, `\N`, "")
  234. odl.Lines = append(odl.Lines, nowText1)
  235. }
  236. return countLineFeed
  237. }
  238. // detectOneOrTwoLineDialogue 优先检测一次字幕文件,可能存在的双语字幕的情况,是 1 还是 2 ,详细解释看调用此函数前的解释
  239. func (p Parser) detectOneOrTwoLineDialogue(matched [][]string) bool {
  240. /*
  241. 这里判断的方法粗暴一点,直接判断两个 Dialogue 都是一个时间段的比例是多少,达到了就是情况2,不是就是情况1
  242. */
  243. allDialogue := len(matched)
  244. twoLine := 0
  245. var timeMap = make(map[string]int, 0)
  246. // 先读取一次字幕文件
  247. for _, oneLine := range matched {
  248. // 这里可能会统计到特效的部分,但是这里忽略这个问题,因为目标不是这个
  249. // 统计 Dialogue 的开始和结束时间
  250. startTime := oneLine[1]
  251. endTime := oneLine[2]
  252. mergeTime := startTime + "_" + endTime
  253. _, ok := timeMap[mergeTime]
  254. if ok == false {
  255. timeMap[mergeTime] = 1
  256. } else {
  257. timeMap[mergeTime]++
  258. if timeMap[mergeTime] == 2 {
  259. twoLine++
  260. }
  261. }
  262. }
  263. // 目前看到的文件大概再 47% 以上,考虑到更多的“注释”、“特效”,至少有 38% 就够了
  264. per := float64(twoLine) / float64(allDialogue)
  265. if per > 0.38 {
  266. // 使用情况2的字幕分析方式
  267. return false
  268. }
  269. // 使用情况1的字幕分析方式
  270. return true
  271. }