fixer_test.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. package sub_timeline_fixer
  2. import (
  3. "fmt"
  4. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/ass"
  5. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/debug_view"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  8. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
  9. "github.com/allanpk716/ChineseSubFinder/internal/pkg/vad"
  10. "github.com/allanpk716/ChineseSubFinder/internal/types/sub_timeline_fiexer"
  11. "github.com/james-bowman/nlp"
  12. "github.com/james-bowman/nlp/measures/pairwise"
  13. "gonum.org/v1/gonum/mat"
  14. "path/filepath"
  15. "strings"
  16. "testing"
  17. )
  18. func TestStopWordCounter(t *testing.T) {
  19. testRootDir := filepath.FromSlash("../../../TestData/FixTimeline")
  20. subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
  21. bFind, info, err := subParserHub.DetermineFileTypeFromFile(filepath.Join(testRootDir, "yes", "R&M S05E01 - English.srt"))
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. if bFind == false {
  26. t.Fatal("not match sub types")
  27. }
  28. allString := strings.Join(info.OtherLines, " ")
  29. s := SubTimelineFixer{}
  30. stopWords := s.StopWordCounter(strings.ToLower(allString), 5)
  31. t.Logf("\n\nsub name: %s \t lem(stopWords): %d", info.Name, len(stopWords))
  32. }
  33. func TestTFIDF(t *testing.T) {
  34. testCorpus := []string{
  35. "The quick brown fox jumped over the lazy dog",
  36. "hey diddle diddle, the cat and the fiddle",
  37. "the cow jumped over the moon",
  38. "the little dog laughed to see such fun",
  39. "and the dish ran away with the spoon",
  40. }
  41. query := "the brown fox ran around the dog"
  42. vectoriser := nlp.NewCountVectoriser(StopWords...)
  43. transformer := nlp.NewTfidfTransformer()
  44. // set k (the number of dimensions following truncation) to 4
  45. reducer := nlp.NewTruncatedSVD(4)
  46. lsiPipeline := nlp.NewPipeline(vectoriser, transformer, reducer)
  47. // Transform the corpus into an LSI fitting the model to the documents in the process
  48. lsi, err := lsiPipeline.FitTransform(testCorpus...)
  49. if err != nil {
  50. t.Errorf("Failed to process documents because %v", err)
  51. return
  52. }
  53. // run the query through the same pipeline that was fitted to the corpus and
  54. // to project it into the same dimensional space
  55. queryVector, err := lsiPipeline.Transform(query)
  56. if err != nil {
  57. t.Errorf("Failed to process documents because %v", err)
  58. return
  59. }
  60. // iterate over document feature vectors (columns) in the LSI matrix and compare
  61. // with the query vector for similarity. Similarity is determined by the difference
  62. // between the angles of the vectors known as the cosine similarity
  63. highestSimilarity := -1.0
  64. var matched int
  65. _, docs := lsi.Dims()
  66. for i := 0; i < docs; i++ {
  67. similarity := pairwise.CosineSimilarity(queryVector.(mat.ColViewer).ColView(0), lsi.(mat.ColViewer).ColView(i))
  68. if similarity > highestSimilarity {
  69. matched = i
  70. highestSimilarity = similarity
  71. }
  72. }
  73. t.Logf("\n\nMatched '%s'", testCorpus[matched])
  74. // Output: Matched 'The quick brown fox jumped over the lazy dog'
  75. }
  76. func TestGetOffsetTimeV1(t *testing.T) {
  77. testRootDir := filepath.FromSlash("../../../TestData/FixTimeline")
  78. testRootDirYes := filepath.Join(testRootDir, "yes")
  79. testRootDirNo := filepath.Join(testRootDir, "no")
  80. subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
  81. type args struct {
  82. enSubFile string
  83. ch_enSubFile string
  84. staticLineFileSavePath string
  85. }
  86. tests := []struct {
  87. name string
  88. args args
  89. want float64
  90. wantErr bool
  91. }{
  92. /*
  93. 这里有几个比较理想的字幕时间轴校正的示例
  94. */
  95. {name: "R&M S05E01", args: args{enSubFile: filepath.Join(testRootDirYes, "R&M S05E01 - English.srt"),
  96. ch_enSubFile: filepath.Join(testRootDirYes, "R&M S05E01 - 简英.srt"),
  97. staticLineFileSavePath: "bar.html"}, want: -6.42981818181818, wantErr: false},
  98. {name: "R&M S05E10", args: args{enSubFile: filepath.Join(testRootDirYes, "R&M S05E10 - English.ass"),
  99. ch_enSubFile: filepath.Join(testRootDirYes, "R&M S05E10 - 简英.ass"),
  100. staticLineFileSavePath: "bar.html"}, want: -6.335985401459854, wantErr: false},
  101. {name: "基地 S01E03", args: args{enSubFile: filepath.Join(testRootDirYes, "基地 S01E03 - English.ass"),
  102. ch_enSubFile: filepath.Join(testRootDirYes, "基地 S01E03 - 简英.ass"),
  103. staticLineFileSavePath: "bar.html"}, want: -32.09061538461539, wantErr: false},
  104. /*
  105. WTF,这部剧集
  106. Dan Brown'timelineFixer The Lost Symbol
  107. 内置的英文字幕时间轴是歪的,所以修正完了就错了
  108. */
  109. {name: "Dan Brown'timelineFixer The Lost Symbol - S01E01", args: args{
  110. enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E01.chinese(inside).ass"),
  111. ch_enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E01.chinese(简英,shooter).ass"),
  112. staticLineFileSavePath: "bar.html"},
  113. want: 1.3217821782178225, wantErr: false},
  114. {name: "Dan Brown'timelineFixer The Lost Symbol - S01E02", args: args{
  115. enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E02.chinese(inside).ass"),
  116. ch_enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E02.chinese(简英,subhd).ass"),
  117. staticLineFileSavePath: "bar.html"},
  118. want: -0.5253383458646617, wantErr: false},
  119. {name: "Dan Brown'timelineFixer The Lost Symbol - S01E03", args: args{
  120. enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E03.chinese(inside).ass"),
  121. ch_enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E03.chinese(繁英,xunlei).ass"),
  122. staticLineFileSavePath: "bar.html"},
  123. want: -0.505656, wantErr: false},
  124. {name: "Dan Brown'timelineFixer The Lost Symbol - S01E04", args: args{
  125. enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E04.chinese(inside).ass"),
  126. ch_enSubFile: filepath.Join(testRootDirNo, "Dan Brown's The Lost Symbol - S01E04.chinese(简英,zimuku).ass"),
  127. staticLineFileSavePath: "bar.html"},
  128. want: -0.633415, wantErr: false},
  129. /*
  130. 只有一个是字幕下载了一个错误的,其他的无需修正
  131. */
  132. {name: "Don't Breathe 2 (2021) - shooter-srt", args: args{
  133. enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).srt"),
  134. ch_enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,shooter).srt"),
  135. staticLineFileSavePath: "bar.html"},
  136. want: 0, wantErr: false},
  137. {name: "Don't Breathe 2 (2021) - subhd-srt error matched sub", args: args{
  138. enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).srt"),
  139. ch_enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,subhd).srt"),
  140. staticLineFileSavePath: "bar.html"},
  141. want: 0, wantErr: false},
  142. {name: "Don't Breathe 2 (2021) - xunlei-ass", args: args{
  143. enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).ass"),
  144. ch_enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,xunlei).ass"),
  145. staticLineFileSavePath: "bar.html"},
  146. want: 0, wantErr: false},
  147. {name: "Don't Breathe 2 (2021) - zimuku-ass", args: args{
  148. enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).ass"),
  149. ch_enSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,zimuku).ass"),
  150. staticLineFileSavePath: "bar.html"},
  151. want: 0, wantErr: false},
  152. /*
  153. 基地
  154. */
  155. {name: "Foundation (2021) - S01E01", args: args{
  156. enSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E01.chinese(inside).ass"),
  157. ch_enSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E01.chinese(简英,zimuku).ass"),
  158. staticLineFileSavePath: "bar.html"},
  159. want: 0, wantErr: false},
  160. {name: "Foundation (2021) - S01E02", args: args{
  161. enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E02.chinese(inside).ass"),
  162. ch_enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E02.chinese(简英,subhd).ass"),
  163. staticLineFileSavePath: "bar.html"},
  164. want: -30.624840, wantErr: false},
  165. {name: "Foundation (2021) - S01E03", args: args{
  166. enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E03.chinese(inside).ass"),
  167. ch_enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E03.chinese(简英,subhd).ass"),
  168. staticLineFileSavePath: "bar.html"},
  169. want: -32.085037037037054, wantErr: false},
  170. {name: "Foundation (2021) - S01E04", args: args{
  171. enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E04.chinese(inside).ass"),
  172. ch_enSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E04.chinese(简英,subhd).ass"),
  173. staticLineFileSavePath: "bar.html"},
  174. want: -36.885074, wantErr: false},
  175. {name: "Foundation (2021) - S01E04", args: args{
  176. enSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E04.chinese(inside).srt"),
  177. ch_enSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E04.chinese(繁英,shooter).srt"),
  178. staticLineFileSavePath: "bar.html"},
  179. want: 0, wantErr: false},
  180. /*
  181. The Card Counter
  182. */
  183. {name: "The Card Counter", args: args{
  184. enSubFile: filepath.Join(testRootDirNo, "The Card Counter (2021).chinese(inside).ass"),
  185. ch_enSubFile: filepath.Join(testRootDirNo, "The Card Counter (2021).chinese(简英,xunlei).ass"),
  186. staticLineFileSavePath: "bar.html"},
  187. want: 0, wantErr: false},
  188. {name: "The Card Counter", args: args{
  189. enSubFile: filepath.Join(testRootDirNo, "The Card Counter (2021).chinese(inside).ass"),
  190. ch_enSubFile: filepath.Join(testRootDirNo, "The Card Counter (2021).chinese(简英,shooter).ass"),
  191. staticLineFileSavePath: "bar.html"},
  192. want: 0.224844, wantErr: false},
  193. /*
  194. Kingdom Ashin of the North
  195. */
  196. {name: "Kingdom Ashin of the North - error matched sub", args: args{
  197. enSubFile: filepath.Join(testRootDirNo, "Kingdom Ashin of the North (2021).chinese(inside).ass"),
  198. ch_enSubFile: filepath.Join(testRootDirNo, "Kingdom Ashin of the North (2021).chinese(简英,subhd).ass"),
  199. staticLineFileSavePath: "bar.html"},
  200. want: 0, wantErr: false},
  201. /*
  202. Only Murders in the Building
  203. */
  204. {name: "Only Murders in the Building - S01E06", args: args{
  205. enSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E06.chinese(inside).ass"),
  206. ch_enSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E06.chinese(简英,subhd).ass"),
  207. staticLineFileSavePath: "bar.html"},
  208. want: 0, wantErr: false},
  209. {name: "Only Murders in the Building - S01E08", args: args{
  210. enSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E08.chinese(inside).ass"),
  211. ch_enSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E08.chinese(简英,subhd).ass"),
  212. staticLineFileSavePath: "bar.html"},
  213. want: 0, wantErr: false},
  214. /*
  215. Ted Lasso
  216. */
  217. {name: "Ted Lasso - S02E09", args: args{
  218. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(inside).ass"),
  219. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(简英,subhd).ass"),
  220. staticLineFileSavePath: "bar.html"},
  221. want: 0, wantErr: false},
  222. {name: "Ted Lasso - S02E09", args: args{
  223. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(inside).ass"),
  224. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(简英,zimuku).ass"),
  225. staticLineFileSavePath: "bar.html"},
  226. want: 0, wantErr: false},
  227. {name: "Ted Lasso - S02E10", args: args{
  228. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(inside).ass"),
  229. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(简英,subhd).ass"),
  230. staticLineFileSavePath: "bar.html"},
  231. want: 0, wantErr: false},
  232. {name: "Ted Lasso - S02E10", args: args{
  233. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(inside).ass"),
  234. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(简英,zimuku).ass"),
  235. staticLineFileSavePath: "bar.html"},
  236. want: 0, wantErr: false},
  237. {name: "Ted Lasso - S02E10", args: args{
  238. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(inside).ass"),
  239. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(简英,shooter).ass"),
  240. staticLineFileSavePath: "bar.html"},
  241. want: 0, wantErr: false},
  242. {name: "Ted Lasso - S02E11", args: args{
  243. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(inside).ass"),
  244. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(简英,subhd).ass"),
  245. staticLineFileSavePath: "bar.html"},
  246. want: 0, wantErr: false},
  247. {name: "Ted Lasso - S02E11", args: args{
  248. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(inside).ass"),
  249. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(简英,zimuku).ass"),
  250. staticLineFileSavePath: "bar.html"},
  251. want: 0, wantErr: false},
  252. {name: "Ted Lasso - S02E12", args: args{
  253. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(inside).ass"),
  254. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(简英,subhd).ass"),
  255. staticLineFileSavePath: "bar.html"},
  256. want: 0, wantErr: false},
  257. {name: "Ted Lasso - S02E12", args: args{
  258. enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(inside).ass"),
  259. ch_enSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(简英,shooter).ass"),
  260. staticLineFileSavePath: "bar.html"},
  261. want: 0, wantErr: false},
  262. /*
  263. The Protégé
  264. */
  265. {name: "The Protégé", args: args{
  266. enSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(inside).ass"),
  267. ch_enSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(简英,zimuku).ass"),
  268. staticLineFileSavePath: "bar.html"},
  269. want: 0, wantErr: false},
  270. {name: "The Protégé", args: args{
  271. enSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(inside).srt"),
  272. ch_enSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(简英,shooter).srt"),
  273. staticLineFileSavePath: "bar.html"},
  274. want: 0, wantErr: false},
  275. /*
  276. The Witcher Nightmare of the Wolf
  277. */
  278. {name: "The Witcher Nightmare of the Wolf", args: args{
  279. enSubFile: filepath.Join(testRootDirNo, "The Witcher Nightmare of the Wolf.chinese(inside).ass"),
  280. ch_enSubFile: filepath.Join(testRootDirNo, "The Witcher Nightmare of the Wolf.chinese(简英,zimuku).ass"),
  281. staticLineFileSavePath: "bar.html"},
  282. want: 0, wantErr: false},
  283. /*
  284. What If…!
  285. */
  286. {name: "What If…! - S01E07", args: args{
  287. enSubFile: filepath.Join(testRootDirNo, "What If…! - S01E07.chinese(inside).ass"),
  288. ch_enSubFile: filepath.Join(testRootDirNo, "What If…! - S01E07.chinese(简英,subhd).ass"),
  289. staticLineFileSavePath: "bar.html"},
  290. want: 0, wantErr: false},
  291. {name: "What If…! - S01E09", args: args{
  292. enSubFile: filepath.Join(testRootDirNo, "What If…! - S01E09.chinese(inside).srt"),
  293. ch_enSubFile: filepath.Join(testRootDirNo, "What If…! - S01E09.chinese(简英,shooter).srt"),
  294. staticLineFileSavePath: "bar.html"},
  295. want: 0, wantErr: false},
  296. }
  297. for _, tt := range tests {
  298. t.Run(tt.name, func(t *testing.T) {
  299. bFind, infoBase, err := subParserHub.DetermineFileTypeFromFile(tt.args.enSubFile)
  300. if err != nil {
  301. t.Fatal(err)
  302. }
  303. if bFind == false {
  304. t.Fatal("sub not match")
  305. }
  306. /*
  307. 这里发现一个梗,内置的英文字幕导出的时候,有可能需要合并多个 Dialogue,见
  308. internal/pkg/sub_helper/sub_helper.go 中 MergeMultiDialogue4EngSubtitle 的实现
  309. */
  310. sub_helper.MergeMultiDialogue4EngSubtitle(infoBase)
  311. bFind, infoSrc, err := subParserHub.DetermineFileTypeFromFile(tt.args.ch_enSubFile)
  312. if err != nil {
  313. t.Fatal(err)
  314. }
  315. if bFind == false {
  316. t.Fatal("sub not match")
  317. }
  318. /*
  319. 这里发现一个梗,内置的英文字幕导出的时候,有可能需要合并多个 Dialogue,见
  320. internal/pkg/sub_helper/sub_helper.go 中 MergeMultiDialogue4EngSubtitle 的实现
  321. */
  322. sub_helper.MergeMultiDialogue4EngSubtitle(infoSrc)
  323. bok, got, sd, err := timelineFixer.GetOffsetTimeV1(infoBase, infoSrc, tt.args.ch_enSubFile+"-bar.html", tt.args.ch_enSubFile+".log")
  324. if (err != nil) != tt.wantErr {
  325. t.Errorf("GetOffsetTimeV1() error = %v, wantErr %v", err, tt.wantErr)
  326. return
  327. }
  328. // 在一个正负范围内都可以接受
  329. if got > tt.want-0.1 && got < tt.want+0.1 {
  330. } else {
  331. t.Errorf("GetOffsetTimeV1() got = %v, want %v", got, tt.want)
  332. }
  333. //if got != tt.want {
  334. // t.Errorf("GetOffsetTimeV1() got = %v, want %v", got, tt.want)
  335. //}
  336. if bok == true && got != 0 {
  337. _, err = timelineFixer.FixSubTimelineOneOffsetTime(infoSrc, got, tt.args.ch_enSubFile+FixMask+infoBase.Ext)
  338. if err != nil {
  339. t.Fatal(err)
  340. }
  341. }
  342. println(fmt.Sprintf("GetOffsetTimeV1: %fs SD:%f", got, sd))
  343. })
  344. }
  345. }
  346. func TestGetOffsetTimeV2_BaseSub(t *testing.T) {
  347. testRootDir := filepath.FromSlash("../../../TestData/FixTimeline")
  348. testRootDirYes := filepath.Join(testRootDir, "yes")
  349. testRootDirNo := filepath.Join(testRootDir, "no")
  350. subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
  351. type args struct {
  352. baseSubFile string
  353. srcSubFile string
  354. staticLineFileSavePath string
  355. }
  356. tests := []struct {
  357. name string
  358. args args
  359. want float64
  360. wantErr bool
  361. }{
  362. /*
  363. 这里有几个比较理想的字幕时间轴校正的示例
  364. */
  365. {name: "R&M S05E01", args: args{baseSubFile: filepath.Join(testRootDirYes, "R&M S05E01 - English.srt"),
  366. srcSubFile: filepath.Join(testRootDirYes, "R&M S05E01 - 简英.srt"),
  367. staticLineFileSavePath: "bar.html"}, want: -6.4, wantErr: false},
  368. {name: "R&M S05E01-1", args: args{baseSubFile: filepath.Join(testRootDirYes, "R&M S05E01 - English.srt"),
  369. srcSubFile: filepath.Join(testRootDirYes, "R&M S05E01 - English.srt"),
  370. staticLineFileSavePath: "bar.html"}, want: 0, wantErr: false},
  371. {name: "R&M S05E10-0", args: args{baseSubFile: filepath.Join(testRootDirYes, "R&M S05E10 - English.ass"),
  372. srcSubFile: filepath.Join(testRootDirYes, "R&M S05E10 - 简英.ass"),
  373. staticLineFileSavePath: "bar.html"}, want: -6.405985401459854, wantErr: false},
  374. {name: "R&M S05E10-1", args: args{baseSubFile: filepath.Join(testRootDirYes, "R&M S05E10 - 简英.ass"),
  375. srcSubFile: filepath.Join(testRootDirYes, "R&M S05E10 - English.ass"),
  376. staticLineFileSavePath: "bar.html"}, want: 6.405985401459854, wantErr: false},
  377. {name: "R&M S05E10-2", args: args{baseSubFile: filepath.Join(testRootDirYes, "R&M S05E10 - 简英.ass"),
  378. srcSubFile: filepath.Join(testRootDirYes, "R&M S05E10 - 简英.ass"),
  379. staticLineFileSavePath: "bar.html"}, want: 0, wantErr: false},
  380. /*
  381. 基地
  382. */
  383. {name: "Foundation (2021) - S01E01", args: args{
  384. baseSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E01.chinese(inside).ass"),
  385. srcSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E01.chinese(简英,zimuku).ass"),
  386. staticLineFileSavePath: "bar.html"},
  387. want: 0, wantErr: false},
  388. {name: "Foundation (2021) - S01E02", args: args{
  389. baseSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E02.chinese(inside).ass"),
  390. srcSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E02.chinese(简英,subhd).ass"),
  391. staticLineFileSavePath: "bar.html"},
  392. want: -30.624840, wantErr: false},
  393. {name: "Foundation (2021) - S01E03", args: args{
  394. baseSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E03.chinese(inside).ass"),
  395. srcSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E03.chinese(简英,subhd).ass"),
  396. staticLineFileSavePath: "bar.html"},
  397. want: -32.085037037037054, wantErr: false},
  398. {name: "Foundation (2021) - S01E04", args: args{
  399. baseSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E04.chinese(inside).ass"),
  400. srcSubFile: filepath.Join(testRootDirYes, "Foundation (2021) - S01E04.chinese(简英,subhd).ass"),
  401. staticLineFileSavePath: "bar.html"},
  402. want: -36.885074, wantErr: false},
  403. {name: "Foundation (2021) - S01E04", args: args{
  404. baseSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E04.chinese(inside).srt"),
  405. srcSubFile: filepath.Join(testRootDirNo, "Foundation (2021) - S01E04.chinese(繁英,shooter).srt"),
  406. staticLineFileSavePath: "bar.html"},
  407. want: 0, wantErr: false},
  408. /*
  409. Don't Breathe 2 (2021)
  410. */
  411. {name: "Don't Breathe 2 (2021) - zimuku-ass", args: args{
  412. baseSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).ass"),
  413. srcSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,zimuku).ass"),
  414. staticLineFileSavePath: "bar.html"},
  415. want: 0, wantErr: false},
  416. {name: "Don't Breathe 2 (2021) - shooter-srt", args: args{
  417. baseSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(inside).srt"),
  418. srcSubFile: filepath.Join(testRootDirNo, "Don't Breathe 2 (2021).chinese(简英,shooter).srt"),
  419. staticLineFileSavePath: "bar.html"},
  420. want: 0, wantErr: false},
  421. /*
  422. Only Murders in the Building
  423. */
  424. {name: "Only Murders in the Building - S01E06", args: args{
  425. baseSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E06.chinese(inside).ass"),
  426. srcSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E06.chinese(简英,subhd).ass"),
  427. staticLineFileSavePath: "bar.html"},
  428. want: 0, wantErr: false},
  429. {name: "Only Murders in the Building - S01E08", args: args{
  430. baseSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E08.chinese(inside).ass"),
  431. srcSubFile: filepath.Join(testRootDirNo, "Only Murders in the Building - S01E08.chinese(简英,subhd).ass"),
  432. staticLineFileSavePath: "bar.html"},
  433. want: 0, wantErr: false},
  434. /*
  435. Ted Lasso
  436. */
  437. {name: "Ted Lasso - S02E09", args: args{
  438. baseSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(inside).ass"),
  439. srcSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(简英,subhd).ass"),
  440. staticLineFileSavePath: "bar.html"},
  441. want: 0, wantErr: false},
  442. {name: "Ted Lasso - S02E09", args: args{
  443. baseSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(inside).ass"),
  444. srcSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E09.chinese(简英,zimuku).ass"),
  445. staticLineFileSavePath: "bar.html"},
  446. want: 0, wantErr: false},
  447. {name: "Ted Lasso - S02E10", args: args{
  448. baseSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(inside).ass"),
  449. srcSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(简英,subhd).ass"),
  450. staticLineFileSavePath: "bar.html"},
  451. want: 0, wantErr: false},
  452. {name: "Ted Lasso - S02E10", args: args{
  453. baseSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(inside).ass"),
  454. srcSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(简英,zimuku).ass"),
  455. staticLineFileSavePath: "bar.html"},
  456. want: 0, wantErr: false},
  457. {name: "Ted Lasso - S02E10", args: args{
  458. baseSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(inside).ass"),
  459. srcSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E10.chinese(简英,shooter).ass"),
  460. staticLineFileSavePath: "bar.html"},
  461. want: 0, wantErr: false},
  462. {name: "Ted Lasso - S02E11", args: args{
  463. baseSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(inside).ass"),
  464. srcSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(简英,subhd).ass"),
  465. staticLineFileSavePath: "bar.html"},
  466. want: 0, wantErr: false},
  467. {name: "Ted Lasso - S02E11", args: args{
  468. baseSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(inside).ass"),
  469. srcSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E11.chinese(简英,zimuku).ass"),
  470. staticLineFileSavePath: "bar.html"},
  471. want: 0, wantErr: false},
  472. {name: "Ted Lasso - S02E12", args: args{
  473. baseSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(inside).ass"),
  474. srcSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(简英,subhd).ass"),
  475. staticLineFileSavePath: "bar.html"},
  476. want: 0, wantErr: false},
  477. {name: "Ted Lasso - S02E12", args: args{
  478. baseSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(inside).ass"),
  479. srcSubFile: filepath.Join(testRootDirNo, "Ted Lasso - S02E12.chinese(简英,shooter).ass"),
  480. staticLineFileSavePath: "bar.html"},
  481. want: 0, wantErr: false},
  482. /*
  483. The Protégé
  484. */
  485. {name: "The Protégé", args: args{
  486. baseSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(inside).ass"),
  487. srcSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(简英,zimuku).ass"),
  488. staticLineFileSavePath: "bar.html"},
  489. want: 0, wantErr: false},
  490. {name: "The Protégé", args: args{
  491. baseSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(inside).srt"),
  492. srcSubFile: filepath.Join(testRootDirNo, "The Protégé (2021).chinese(简英,shooter).srt"),
  493. staticLineFileSavePath: "bar.html"},
  494. want: 0, wantErr: false},
  495. /*
  496. The Witcher Nightmare of the Wolf
  497. */
  498. {name: "The Witcher Nightmare of the Wolf", args: args{
  499. baseSubFile: filepath.Join(testRootDirNo, "The Witcher Nightmare of the Wolf.chinese(inside).ass"),
  500. srcSubFile: filepath.Join(testRootDirNo, "The Witcher Nightmare of the Wolf.chinese(简英,zimuku).ass"),
  501. staticLineFileSavePath: "bar.html"},
  502. want: 0, wantErr: false},
  503. /*
  504. What If…!
  505. */
  506. {name: "What If…! - S01E01", args: args{
  507. baseSubFile: filepath.Join(testRootDirNo, "What If…! - S01E01_英_2.srt"),
  508. srcSubFile: filepath.Join(testRootDirNo, "What If…! - S01E01 - (简英,shooter).srt"),
  509. staticLineFileSavePath: "bar.html"},
  510. want: 0, wantErr: false},
  511. {name: "What If…! - S01E07", args: args{
  512. baseSubFile: filepath.Join(testRootDirNo, "What If…! - S01E07.chinese(inside).ass"),
  513. srcSubFile: filepath.Join(testRootDirNo, "What If…! - S01E07.chinese(简英,subhd).ass"),
  514. staticLineFileSavePath: "bar.html"},
  515. want: 0, wantErr: false},
  516. {name: "What If…! - S01E09", args: args{
  517. baseSubFile: filepath.Join(testRootDirNo, "What If…! - S01E09.chinese(inside).srt"),
  518. srcSubFile: filepath.Join(testRootDirNo, "What If…! - S01E09.chinese(简英,shooter).srt"),
  519. staticLineFileSavePath: "bar.html"},
  520. want: 0, wantErr: false},
  521. }
  522. for _, tt := range tests {
  523. t.Run(tt.name, func(t *testing.T) {
  524. bFind, infoBase, err := subParserHub.DetermineFileTypeFromFile(tt.args.baseSubFile)
  525. if err != nil {
  526. t.Fatal(err)
  527. }
  528. if bFind == false {
  529. t.Fatal("sub not match")
  530. }
  531. /*
  532. 这里发现一个梗,内置的英文字幕导出的时候,有可能需要合并多个 Dialogue,见
  533. internal/pkg/sub_helper/sub_helper.go 中 MergeMultiDialogue4EngSubtitle 的实现
  534. */
  535. //sub_helper.MergeMultiDialogue4EngSubtitle(infoBase)
  536. bFind, infoSrc, err := subParserHub.DetermineFileTypeFromFile(tt.args.srcSubFile)
  537. if err != nil {
  538. t.Fatal(err)
  539. }
  540. if bFind == false {
  541. t.Fatal("sub not match")
  542. }
  543. /*
  544. 这里发现一个梗,内置的英文字幕导出的时候,有可能需要合并多个 Dialogue,见
  545. internal/pkg/sub_helper/sub_helper.go 中 MergeMultiDialogue4EngSubtitle 的实现
  546. */
  547. //sub_helper.MergeMultiDialogue4EngSubtitle(infoSrc)
  548. // ---------------------------------------------------------------------------------------
  549. // Base,截取的部分要大于 Src 的部分
  550. //baseUnitListOld, err := sub_helper.GetVADInfoFeatureFromSub(infoBase, V2_FrontAndEndPerBase, 100000, true)
  551. //if err != nil {
  552. // t.Fatal(err)
  553. //}
  554. //baseUnitOld := baseUnitListOld[0]
  555. baseUnitNew, err := sub_helper.GetVADInfoFeatureFromSubNew(infoBase, timelineFixer.FixerConfig.V2_FrontAndEndPerBase)
  556. if err != nil {
  557. t.Fatal(err)
  558. }
  559. // ---------------------------------------------------------------------------------------
  560. // Src,截取的部分要小于 Base 的部分
  561. //srcUnitListOld, err := sub_helper.GetVADInfoFeatureFromSub(infoSrc, V2_FrontAndEndPerSrc, 100000, true)
  562. //if err != nil {
  563. // t.Fatal(err)
  564. //}
  565. //srcUnitOld := srcUnitListOld[0]
  566. srcUnitNew, err := sub_helper.GetVADInfoFeatureFromSubNew(infoSrc, timelineFixer.FixerConfig.V2_FrontAndEndPerSrc)
  567. if err != nil {
  568. t.Fatal(err)
  569. }
  570. // ---------------------------------------------------------------------------------------
  571. //bok, got, sd, err := timelineFixer.GetOffsetTimeV2(&baseUnitOld, &srcUnitOld, nil, 0)
  572. bok, _, err := timelineFixer.GetOffsetTimeV2(baseUnitNew, srcUnitNew, nil)
  573. if (err != nil) != tt.wantErr {
  574. t.Errorf("GetOffsetTimeV2() error = %v, wantErr %v", err, tt.wantErr)
  575. return
  576. }
  577. if bok == false {
  578. t.Fatal("GetOffsetTimeV2 return false")
  579. }
  580. //if got > -0.2 && got < 0.2 && tt.want == 0 {
  581. // // 如果 offset time > -0.2 且 < 0.2 则认为无需调整时间轴,为0
  582. //} else if got > tt.want-0.1 && got < tt.want+0.1 {
  583. // // 在一个正负范围内都可以接受
  584. //} else {
  585. // t.Errorf("GetOffsetTimeV2() got = %v, want %v", got, tt.want)
  586. //}
  587. debug_view.SaveDebugChart(*baseUnitNew, tt.name+" -- baseUnitNew", "baseUnitNew")
  588. debug_view.SaveDebugChart(*srcUnitNew, tt.name+" -- srcUnitNew", "srcUnitNew")
  589. //if bok == true && got != 0 {
  590. //_, err = timelineFixer.FixSubTimelineOneOffsetTime(infoSrc, got, tt.args.srcSubFile+FixMask+infoBase.Ext)
  591. //if err != nil {
  592. // t.Fatal(err)
  593. //}
  594. ////}
  595. //println(fmt.Sprintf("GetOffsetTimeV2: %fs SD:%f", got, sd))
  596. })
  597. }
  598. }
  599. func TestGetOffsetTimeV2_BaseAudio(t *testing.T) {
  600. subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
  601. type fields struct {
  602. fixerConfig sub_timeline_fiexer.SubTimelineFixerConfig
  603. }
  604. type args struct {
  605. audioInfo vad.AudioInfo
  606. subFilePath string
  607. }
  608. tests := []struct {
  609. name string
  610. fields fields
  611. args args
  612. want bool
  613. want1 float64
  614. want2 float64
  615. wantErr bool
  616. }{
  617. // Rick and Morty - S05E01
  618. {name: "Rick and Morty - S05E01 -- 0",
  619. args: args{audioInfo: vad.AudioInfo{
  620. FileFullPath: "C:\\Tmp\\Rick and Morty - S05E01\\未知语言_1.pcm"},
  621. subFilePath: "C:\\Tmp\\Rick and Morty - S05E01\\英_2.ass"},
  622. want: true, want1: 0,
  623. },
  624. {name: "Rick and Morty - S05E01 -- 0",
  625. args: args{audioInfo: vad.AudioInfo{
  626. FileFullPath: "C:\\WorkSpace\\Go2hell\\src\\github.com\\allanpk716\\ChineseSubFinder\\internal\\pkg\\ffmpeg_helper\\CSF-SubFixCache\\Blade Runner - Black Lotus - S01E03 - The Human Condition WEBDL-1080p\\日_1.pcm"},
  627. subFilePath: "C:\\WorkSpace\\Go2hell\\src\\github.com\\allanpk716\\ChineseSubFinder\\CSF-SubFixCache\\Blade Runner - Black Lotus - S01E03 - The Human Condition WEBDL-1080p\\tar.ass"},
  628. want: true, want1: -5.1,
  629. },
  630. }
  631. for _, tt := range tests {
  632. t.Run(tt.name, func(t *testing.T) {
  633. bFind, infoSrc, err := subParserHub.DetermineFileTypeFromFile(tt.args.subFilePath)
  634. if err != nil {
  635. t.Fatal(err)
  636. }
  637. if bFind == false {
  638. t.Fatal("sub not match")
  639. }
  640. /*
  641. 这里发现一个梗,内置的英文字幕导出的时候,有可能需要合并多个 Dialogue,见
  642. internal/pkg/sub_helper/sub_helper.go 中 MergeMultiDialogue4EngSubtitle 的实现
  643. */
  644. //sub_helper.MergeMultiDialogue4EngSubtitle(infoSrc)
  645. // Src,截取的部分要小于 Base 的部分
  646. srcUnitNew, err := sub_helper.GetVADInfoFeatureFromSubNew(infoSrc, timelineFixer.FixerConfig.V2_FrontAndEndPerSrc)
  647. if err != nil {
  648. t.Fatal(err)
  649. }
  650. audioVADInfos, err := vad.GetVADInfoFromAudio(vad.AudioInfo{
  651. FileFullPath: tt.args.audioInfo.FileFullPath,
  652. SampleRate: 16000,
  653. BitDepth: 16,
  654. }, true)
  655. if err != nil {
  656. t.Fatal(err)
  657. }
  658. println("-------New--------")
  659. bok, _, err := timelineFixer.GetOffsetTimeV2(nil, srcUnitNew, audioVADInfos)
  660. if (err != nil) != tt.wantErr {
  661. t.Errorf("GetOffsetTimeV2() error = %v, wantErr %v", err, tt.wantErr)
  662. return
  663. }
  664. //debug_view.SaveDebugChartBase(audioVADInfos, tt.name+" audioVADInfos", "audioVADInfos")
  665. //debug_view.SaveDebugChart(*srcUnitNew, tt.name+" srcUnitNew", "srcUnitNew")
  666. if bok != tt.want {
  667. t.Errorf("GetOffsetTimeV2() bok = %v, want %v", bok, tt.want)
  668. }
  669. //if offsetTime > -0.2 && offsetTime < 0.2 && tt.want1 == 0 {
  670. // // 如果 offset time > -0.2 且 < 0.2 则认为无需调整时间轴,为0
  671. //} else if offsetTime > tt.want1-0.1 && offsetTime < tt.want1+0.1 {
  672. // // 在一个正负范围内都可以接受
  673. //} else {
  674. // t.Errorf("GetOffsetTimeV2() bok = %v, want %v", offsetTime, tt.want1)
  675. //}
  676. //_, err = timelineFixer.FixSubTimelineOneOffsetTime(infoSrc, offsetTime, tt.args.subFilePath+FixMask+infoSrc.Ext)
  677. //if err != nil {
  678. // t.Fatal(err)
  679. //}
  680. //println(fmt.Sprintf("GetOffsetTimeV2: %vs SD:%v", offsetTime, sd))
  681. })
  682. }
  683. }
  684. func TestGetOffsetTimeV2_MoreTest(t *testing.T) {
  685. subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
  686. type args struct {
  687. baseSubFile string
  688. srcSubFile string
  689. }
  690. tests := []struct {
  691. name string
  692. args args
  693. want float64
  694. wantErr bool
  695. }{
  696. {name: "BL S01E03", args: args{
  697. baseSubFile: "C:\\Tmp\\BL - S01E03\\英_2.ass",
  698. srcSubFile: "C:\\Tmp\\BL - S01E03\\org.ass",
  699. }, want: -4.1, wantErr: false},
  700. {name: "Rick and Morty - S05E10", args: args{
  701. baseSubFile: "C:\\Tmp\\Rick and Morty - S05E10\\英_2.ass",
  702. srcSubFile: "C:\\Tmp\\Rick and Morty - S05E10\\org.ass",
  703. }, want: -4.1, wantErr: false},
  704. {name: "mix", args: args{
  705. baseSubFile: "C:\\Tmp\\Rick and Morty - S05E10\\英_2.ass",
  706. srcSubFile: "C:\\Tmp\\BL - S01E03\\org.ass",
  707. }, want: -4.1, wantErr: false},
  708. }
  709. for _, tt := range tests {
  710. t.Run(tt.name, func(t *testing.T) {
  711. bFind, infoBase, err := subParserHub.DetermineFileTypeFromFile(tt.args.baseSubFile)
  712. if err != nil {
  713. t.Fatal(err)
  714. }
  715. if bFind == false {
  716. t.Fatal("sub not match")
  717. }
  718. bFind, infoSrc, err := subParserHub.DetermineFileTypeFromFile(tt.args.srcSubFile)
  719. if err != nil {
  720. t.Fatal(err)
  721. }
  722. if bFind == false {
  723. t.Fatal("sub not match")
  724. }
  725. // ---------------------------------------------------------------------------------------
  726. // Base,截取的部分要大于 Src 的部分
  727. baseUnitNew, err := sub_helper.GetVADInfoFeatureFromSubNew(infoBase, timelineFixer.FixerConfig.V2_FrontAndEndPerBase)
  728. if err != nil {
  729. t.Fatal(err)
  730. }
  731. // ---------------------------------------------------------------------------------------
  732. // Src,截取的部分要小于 Base 的部分
  733. srcUnitNew, err := sub_helper.GetVADInfoFeatureFromSubNew(infoSrc, timelineFixer.FixerConfig.V2_FrontAndEndPerSrc)
  734. if err != nil {
  735. t.Fatal(err)
  736. }
  737. // ---------------------------------------------------------------------------------------
  738. //bok, got, sd, err := timelineFixer.GetOffsetTimeV1(infoBase, infoSrc, tt.args.srcSubFile+"-bar.html", tt.args.srcSubFile+".log")
  739. //if (err != nil) != tt.wantErr {
  740. // t.Errorf("GetOffsetTimeV1() error = %v, wantErr %v", err, tt.wantErr)
  741. // return
  742. //}
  743. bok, fixedResults, err := timelineFixer.GetOffsetTimeV2(baseUnitNew, srcUnitNew, nil)
  744. if (err != nil) != tt.wantErr {
  745. t.Errorf("GetOffsetTimeV2() error = %v, wantErr %v", err, tt.wantErr)
  746. return
  747. }
  748. if bok == false {
  749. t.Fatal("GetOffsetTimeV2 return false")
  750. }
  751. //debug_view.SaveDebugChart(*baseUnitNew, tt.name+" -- baseUnitNew", "baseUnitNew")
  752. //debug_view.SaveDebugChart(*srcUnitNew, tt.name+" -- srcUnitNew", "srcUnitNew")
  753. println("baseUnitNew:", fmt.Sprintf("%f", baseUnitNew.GetStartTimeNumber(true)))
  754. println("srcUnitNew:", fmt.Sprintf("%f", srcUnitNew.GetStartTimeNumber(true)))
  755. _, err = timelineFixer.FixSubTimelineByFixResults(infoSrc, srcUnitNew, fixedResults, tt.args.srcSubFile+FixMask+infoBase.Ext)
  756. if err != nil {
  757. t.Fatal(err)
  758. }
  759. })
  760. }
  761. }
  762. func TestGetOffsetTimeV3_MoreTest(t *testing.T) {
  763. subParserHub := sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser())
  764. type args struct {
  765. baseSubFile string
  766. orgFixSubFile string
  767. srcSubFile string
  768. }
  769. tests := []struct {
  770. name string
  771. args args
  772. want float64
  773. wantErr bool
  774. }{
  775. {name: "BL S01E03", args: args{
  776. baseSubFile: "C:\\Tmp\\BL - S01E03\\英_2.ass",
  777. orgFixSubFile: "C:\\Tmp\\BL - S01E03\\org-fix.ass",
  778. srcSubFile: "C:\\Tmp\\BL - S01E03\\org.ass",
  779. }, want: -4.1, wantErr: false},
  780. {name: "Rick and Morty - S05E10", args: args{
  781. baseSubFile: "C:\\Tmp\\Rick and Morty - S05E10\\英_2.ass",
  782. orgFixSubFile: "C:\\Tmp\\Rick and Morty - S05E10\\org-fix.ass",
  783. srcSubFile: "C:\\Tmp\\Rick and Morty - S05E10\\org.ass",
  784. }, want: -4.1, wantErr: false},
  785. {name: "Foundation - S01E09", args: args{
  786. baseSubFile: "C:\\Tmp\\Foundation - S01E09\\英_2.ass",
  787. orgFixSubFile: "C:\\Tmp\\Foundation - S01E09\\org-fix.ass",
  788. srcSubFile: "C:\\Tmp\\Foundation - S01E09\\org.ass",
  789. }, want: -4.1, wantErr: false},
  790. {name: "mix", args: args{
  791. baseSubFile: "C:\\Tmp\\Rick and Morty - S05E10\\英_2.ass",
  792. srcSubFile: "C:\\Tmp\\BL - S01E03\\org.ass",
  793. }, want: -4.1, wantErr: false},
  794. }
  795. for _, tt := range tests {
  796. t.Run(tt.name, func(t *testing.T) {
  797. bFind, infoBase, err := subParserHub.DetermineFileTypeFromFile(tt.args.baseSubFile)
  798. if err != nil {
  799. t.Fatal(err)
  800. }
  801. if bFind == false {
  802. t.Fatal("sub not match")
  803. }
  804. bFind, infoSrc, err := subParserHub.DetermineFileTypeFromFile(tt.args.srcSubFile)
  805. if err != nil {
  806. t.Fatal(err)
  807. }
  808. if bFind == false {
  809. t.Fatal("sub not match")
  810. }
  811. bFind, orgFix, err := subParserHub.DetermineFileTypeFromFile(tt.args.orgFixSubFile)
  812. if err != nil {
  813. t.Fatal(err)
  814. }
  815. if bFind == false {
  816. t.Fatal("sub not match")
  817. }
  818. // ---------------------------------------------------------------------------------------
  819. err = timelineFixer.GetOffsetTimeV3(infoBase, infoSrc, orgFix, nil)
  820. if (err != nil) != tt.wantErr {
  821. t.Errorf("GetOffsetTimeV3() error = %v, wantErr %v", err, tt.wantErr)
  822. return
  823. }
  824. })
  825. }
  826. }
  827. var timelineFixer = NewSubTimelineFixer(sub_timeline_fiexer.SubTimelineFixerConfig{
  828. // V1
  829. V1_MaxCompareDialogue: 3,
  830. V1_MaxStartTimeDiffSD: 0.1,
  831. V1_MinMatchedPercent: 0.1,
  832. V1_MinOffset: 0.1,
  833. // V2
  834. V2_SubOneUnitProcessTimeOut: 5 * 60,
  835. V2_FrontAndEndPerBase: 0.1,
  836. V2_FrontAndEndPerSrc: 0.2,
  837. V2_WindowMatchPer: 0.2,
  838. V2_CompareParts: 3,
  839. V2_FixThreads: 1,
  840. V2_MaxStartTimeDiffSD: 0.1,
  841. V2_MinOffset: 0.2,
  842. })