pipeline_test.go 6.7 KB

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