pipeline_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package sub_timeline_fixer
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "testing"
  6. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/log_helper"
  7. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/logic/sub_parser/ass"
  8. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/logic/sub_parser/srt"
  9. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/sub_parser_hub"
  10. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/unit_test_helper"
  11. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/vad"
  12. )
  13. func TestPipeline_getFramerateRatios2Try(t *testing.T) {
  14. outList := NewPipeline(DefaultMaxOffsetSeconds).getFramerateRatios2Try()
  15. for i, value := range outList {
  16. println(i, fmt.Sprintf("%v", value))
  17. }
  18. }
  19. func TestPipeline_FitGSS(t *testing.T) {
  20. log := log_helper.GetLogger4Tester()
  21. dirRoot := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_timeline_fixer"}, 4, true)
  22. dirRoot = filepath.Join(dirRoot, "mix")
  23. subParserHub := sub_parser_hub.NewSubParserHub(log, ass.NewParser(log), srt.NewParser(log))
  24. type args struct {
  25. baseSubFile string
  26. ffsubSyncSubFile string
  27. srcSubFile string
  28. srcFixedSubFile string
  29. }
  30. tests := []struct {
  31. name string
  32. args args
  33. want float64
  34. wantErr bool
  35. }{
  36. {name: "BL S01E03", args: args{
  37. baseSubFile: filepath.Join(dirRoot, "BL - S01E03", "英_2.ass"),
  38. ffsubSyncSubFile: filepath.Join(dirRoot, "BL - S01E03", "ffsubsync.ass"),
  39. srcSubFile: filepath.Join(dirRoot, "BL - S01E03", "org.ass"),
  40. srcFixedSubFile: filepath.Join(dirRoot, "BL - S01E03", "org-fix.ass"),
  41. }, want: -4.290000, wantErr: false},
  42. {name: "Rick and Morty - S05E01", args: args{
  43. baseSubFile: filepath.Join(dirRoot, "Rick and Morty - S05E01", "英_2.ass"),
  44. ffsubSyncSubFile: filepath.Join(dirRoot, "Rick and Morty - S05E01", "ffsubsync.ass"),
  45. srcSubFile: filepath.Join(dirRoot, "Rick and Morty - S05E01", "org.ass"),
  46. srcFixedSubFile: filepath.Join(dirRoot, "Rick and Morty - S05E01", "org-fix.ass"),
  47. }, want: -6.170000, wantErr: false},
  48. {name: "Rick and Morty - S05E10", args: args{
  49. baseSubFile: filepath.Join(dirRoot, "Rick and Morty - S05E10", "英_2.ass"),
  50. ffsubSyncSubFile: filepath.Join(dirRoot, "Rick and Morty - S05E10", "ffsubsync.ass"),
  51. srcSubFile: filepath.Join(dirRoot, "Rick and Morty - S05E10", "org.ass"),
  52. srcFixedSubFile: filepath.Join(dirRoot, "Rick and Morty - S05E10", "org-fix.ass"),
  53. }, want: -6.020000, wantErr: false},
  54. {name: "Foundation - S01E09", args: args{
  55. baseSubFile: filepath.Join(dirRoot, "Foundation - S01E09", "英_2.ass"),
  56. ffsubSyncSubFile: filepath.Join(dirRoot, "Foundation - S01E09", "ffsubsync.ass"),
  57. srcSubFile: filepath.Join(dirRoot, "Foundation - S01E09", "org.ass"),
  58. srcFixedSubFile: filepath.Join(dirRoot, "Foundation - S01E09", "org-fix.ass"),
  59. }, want: -29.890000, wantErr: false},
  60. {name: "Yellowstone S04E05", args: args{
  61. baseSubFile: filepath.Join(dirRoot, "Yellowstone S04E05", "英_2.ass"),
  62. ffsubSyncSubFile: filepath.Join(dirRoot, "Yellowstone S04E05", "ffsubsync.ass"),
  63. srcSubFile: filepath.Join(dirRoot, "Yellowstone S04E05", "org.ass"),
  64. srcFixedSubFile: filepath.Join(dirRoot, "Yellowstone S04E05", "org-fix.ass"),
  65. }, want: -62.84, wantErr: false},
  66. //{name: "Yellowstone S04E06", args: args{
  67. // baseSubFile: filepath.Join(dirRoot, "Yellowstone S04E06", "英_2.ass"),
  68. // ffsubSyncSubFile: filepath.Join(dirRoot, "Yellowstone S04E06", "ffsubsync.ass"),
  69. // srcSubFile: filepath.Join(dirRoot, "Yellowstone S04E06", "org.ass"),
  70. // srcFixedSubFile: filepath.Join(dirRoot, "Yellowstone S04E06", "org-fix.ass"),
  71. //}, want: -62.84, wantErr: false},
  72. }
  73. for _, tt := range tests {
  74. t.Run(tt.name, func(t *testing.T) {
  75. bFind, infoBase, err := subParserHub.DetermineFileTypeFromFile(tt.args.baseSubFile)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. if bFind == false {
  80. t.Fatal("sub not match")
  81. }
  82. bFind, infoSrc, err := subParserHub.DetermineFileTypeFromFile(tt.args.srcSubFile)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. if bFind == false {
  87. t.Fatal("sub not match")
  88. }
  89. // ---------------------------------------------------------------------------------------
  90. p := NewPipeline(DefaultMaxOffsetSeconds)
  91. pipeResult, err := p.CalcOffsetTime(infoBase, infoSrc, nil, false)
  92. if (err != nil) != tt.wantErr {
  93. t.Errorf("FitGSS() error = %v, wantErr %v", err, tt.wantErr)
  94. return
  95. }
  96. if pipeResult.GetOffsetTime() != tt.want {
  97. t.Errorf("FitGSS() offsetTome = %v, want %v", pipeResult.GetOffsetTime(), tt.want)
  98. }
  99. _, err = p.FixSubFileTimeline(infoSrc, pipeResult.ScaledFileInfo,
  100. pipeResult.GetOffsetTime(),
  101. tt.args.srcFixedSubFile)
  102. println(fmt.Sprintf("Offset: %f, Score: %f, Scale:%f",
  103. pipeResult.GetOffsetTime(),
  104. pipeResult.Score,
  105. pipeResult.ScaleFactor))
  106. })
  107. }
  108. }
  109. func TestPipeline_FitGSSByAudio(t *testing.T) {
  110. dirRoot := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_timeline_fixer"}, 4, true)
  111. dirRoot = filepath.Join(dirRoot, "mix")
  112. log := log_helper.GetLogger4Tester()
  113. subParserHub := sub_parser_hub.NewSubParserHub(log, ass.NewParser(log), srt.NewParser(log))
  114. type args struct {
  115. audioInfo vad.AudioInfo
  116. subFilePath string
  117. srcFixedSubFile string
  118. }
  119. tests := []struct {
  120. name string
  121. args args
  122. want bool
  123. want1 float64
  124. want2 float64
  125. wantErr bool
  126. }{
  127. // Rick and Morty - S05E01
  128. {name: "Rick and Morty - S05E01 -- 0",
  129. args: args{
  130. audioInfo: vad.AudioInfo{
  131. FileFullPath: filepath.Join(dirRoot, "Rick and Morty - S05E01", "未知语言_1.pcm"),
  132. },
  133. subFilePath: filepath.Join(dirRoot, "Rick and Morty - S05E01", "英_2.ass"),
  134. srcFixedSubFile: filepath.Join(dirRoot, "Rick and Morty - S05E01", "org-fix.ass")},
  135. want: true, want1: 0.33,
  136. },
  137. // Rick and Morty - S05E01
  138. {name: "Rick and Morty - S05E01 -- 1",
  139. args: args{
  140. audioInfo: vad.AudioInfo{
  141. FileFullPath: filepath.Join(dirRoot, "Rick and Morty - S05E01", "未知语言_1.pcm"),
  142. },
  143. subFilePath: filepath.Join(dirRoot, "Rick and Morty - S05E01", "org.ass"),
  144. srcFixedSubFile: filepath.Join(dirRoot, "Rick and Morty - S05E01", "org-fix.ass")},
  145. want: true, want1: -6.1,
  146. },
  147. }
  148. for _, tt := range tests {
  149. t.Run(tt.name, func(t *testing.T) {
  150. audioVADInfos, err := vad.GetVADInfoFromAudio(vad.AudioInfo{
  151. FileFullPath: tt.args.audioInfo.FileFullPath,
  152. SampleRate: 16000,
  153. BitDepth: 16,
  154. }, true)
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. bFind, infoSrc, err := subParserHub.DetermineFileTypeFromFile(tt.args.subFilePath)
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. if bFind == false {
  163. t.Fatal("sub not match")
  164. }
  165. // ---------------------------------------------------------------------------------------
  166. p := NewPipeline(DefaultMaxOffsetSeconds)
  167. pipeResult, err := p.CalcOffsetTime(nil, infoSrc, audioVADInfos, false)
  168. if (err != nil) != tt.wantErr {
  169. t.Errorf("FitGSS() error = %v, wantErr %v", err, tt.wantErr)
  170. return
  171. }
  172. if pipeResult.GetOffsetTime() != tt.want1 {
  173. t.Errorf("FitGSS() offsetTome = %v, want1 %v", pipeResult.GetOffsetTime(), tt.want1)
  174. }
  175. _, err = p.FixSubFileTimeline(infoSrc, pipeResult.ScaledFileInfo,
  176. pipeResult.GetOffsetTime(),
  177. tt.args.srcFixedSubFile)
  178. println(fmt.Sprintf("Offset: %f, Score: %f, Scale:%f",
  179. pipeResult.GetOffsetTime(),
  180. pipeResult.Score,
  181. pipeResult.ScaleFactor))
  182. })
  183. }
  184. }