Browse Source

修复 Sub2VAD 新版本的 bug

Signed-off-by: allan716 <[email protected]>
allan716 4 years ago
parent
commit
f1a85b7cc4

+ 10 - 9
internal/pkg/sub_helper/sub_helper.go

@@ -460,16 +460,16 @@ func GetVADInfoFeatureFromSubNeedOffsetTimeWillInsert(fileInfo *subparser.FileIn
 }
 
 /*
-	GetVADInfosFromSub 将 Sub 文件转换为 VAD List 信息
+	GetVADInfoFeatureFromSubNew 将 Sub 文件转换为 VAD List 信息
 */
-func GetVADInfosFromSub(fileInfo *subparser.FileInfo, SkipFrontAndEndPer float64, pieces int) ([]SubUnit, error) {
+func GetVADInfoFeatureFromSubNew(fileInfo *subparser.FileInfo, SkipFrontAndEndPer float64, pieces int) ([]SubUnit, error) {
 	// 至少分为一份
 	if pieces <= 0 {
 		pieces = 1
 	}
 	outSubUnits := make([]SubUnit, 0)
 	if len(fileInfo.DialoguesEx) <= 0 {
-		return nil, errors.New("GetVADInfosFromSub fileInfo Dialogue Length is 0")
+		return nil, errors.New("GetVADInfoFeatureFromSubNew fileInfo Dialogue Length is 0")
 	}
 	/*
 		先拼凑出完整的一个 VAD List
@@ -501,8 +501,9 @@ func GetVADInfosFromSub(fileInfo *subparser.FileInfo, SkipFrontAndEndPer float64
 		subVADs[i] = *vad.NewVADInfoBase(false, time.Duration((subStartTimeFloor10ms+float64(i))*math.Pow10(7)))
 	}
 	// 计算出需要截取的片段,起始和结束
-	skipStartIndex := int(float64(vadLen) * SkipFrontAndEndPer)
-	skipEndIndex := vadLen - skipStartIndex
+	skipLen := int(float64(vadLen) * SkipFrontAndEndPer)
+	skipStartIndex := int(subStartTimeFloor10ms) + skipLen
+	skipEndIndex := skipStartIndex + (vadLen - 2*skipLen)
 	// 现在需要从 fileInfo 的每一句对白也就对应一段连续的 VAD active = true 来进行改写,记得向下取整
 	for index, dialogueEx := range fileInfo.DialoguesEx {
 
@@ -532,27 +533,27 @@ func GetVADInfosFromSub(fileInfo *subparser.FileInfo, SkipFrontAndEndPer float64
 			continue
 		}
 		// 如果上一个对白的最后一个 OffsetIndex 链接着当前这一句的索引的 VAD 信息 active 是 true 就设置为 false
-		lastDialogueEndIndex := changeVADStartIndex - 1
+		lastDialogueEndIndex := changeVADStartIndex - int(subStartTimeFloor10ms) - 1
 		if lastDialogueEndIndex >= 0 {
 			if subVADs[lastDialogueEndIndex].Active == true {
 				subVADs[lastDialogueEndIndex].Active = false
 			}
 		}
 		// 调整之前做好的整体 VAD 的信息,符合 VAD active = true
-		for i := changeVADStartIndex; i < changeVADEndIndex; i++ {
+		for i := changeVADStartIndex - int(subStartTimeFloor10ms); i < changeVADEndIndex-int(subStartTimeFloor10ms); i++ {
 			subVADs[i].Active = true
 		}
 	}
 	// 整体的 VAD 信息构建完了,现在需要进行切割,分成多份
 	// 需要根据去头去尾,调整整体的总长度再进行多分的拆分
-	afterCutVADLen := vadLen - 2*skipStartIndex
+	afterCutVADLen := vadLen - 2*skipLen
 	onePartLen := afterCutVADLen / pieces
 	// 余下的不要了,暂定
 	//yu := vadLen % pieces
 	for i := 0; i < pieces; i++ {
 		tmpSubUnit := NewSubUnit()
 		// 截取出来当前这一段
-		tmpVADList := subVADs[skipStartIndex+i*onePartLen : skipStartIndex+i*onePartLen+onePartLen]
+		tmpVADList := subVADs[skipStartIndex+i*onePartLen-int(subStartTimeFloor10ms) : skipStartIndex+i*onePartLen+onePartLen-int(subStartTimeFloor10ms)]
 		tmpSubUnit.VADList = tmpVADList
 
 		tmpStartTime := time.Time{}

+ 2 - 2
internal/pkg/sub_helper/sub_helper_test.go

@@ -53,12 +53,12 @@ func TestGetVADInfosFromSub(t *testing.T) {
 			len(infoBase.DialoguesEx), len(infoSrc.DialoguesEx)))
 	}
 
-	baseSubUnits, err := GetVADInfosFromSub(infoBase, FrontAndEndPerBase, 1)
+	baseSubUnits, err := GetVADInfoFeatureFromSubNew(infoBase, FrontAndEndPerBase, 1)
 	if err != nil {
 		t.Fatal(err)
 	}
 	baseSubUnit := baseSubUnits[0]
-	srcSubUnits, err := GetVADInfosFromSub(infoSrc, FrontAndEndPerBase, 1)
+	srcSubUnits, err := GetVADInfoFeatureFromSubNew(infoSrc, FrontAndEndPerBase, 1)
 	if err != nil {
 		t.Fatal(err)
 	}

+ 37 - 13
internal/pkg/sub_timeline_fixer/fixer_test.go

@@ -622,7 +622,7 @@ func TestGetOffsetTimeV2_BaseSub(t *testing.T) {
 			//sub_helper.MergeMultiDialogue4EngSubtitle(infoSrc)
 			// ---------------------------------------------------------------------------------------
 			// Base,截取的部分要大于 Src 的部分
-			//baseUnitList, err := sub_helper.GetVADInfosFromSub(infoBase, FrontAndEndPerBase, 1)
+			//baseUnitList, err := sub_helper.GetVADInfoFeatureFromSubNew(infoBase, FrontAndEndPerBase, 1)
 			//if err != nil {
 			//	t.Fatal(err)
 			//}
@@ -638,7 +638,7 @@ func TestGetOffsetTimeV2_BaseSub(t *testing.T) {
 			//baseUnit = baseUnitList2[0]
 			// ---------------------------------------------------------------------------------------
 			// Src,截取的部分要小于 Base 的部分
-			//srcUnitList, err := sub_helper.GetVADInfosFromSub(infoSrc, FrontAndEndPerSrc, 1)
+			//srcUnitList, err := sub_helper.GetVADInfoFeatureFromSubNew(infoSrc, FrontAndEndPerSrc, 1)
 			//if err != nil {
 			//	t.Fatal(err)
 			//}
@@ -700,7 +700,7 @@ func TestGetOffsetTimeV2_BaseAudio(t *testing.T) {
 		want2   float64
 		wantErr bool
 	}{
-		{name: "Rick and Morty - S05E10 -0 0",
+		{name: "Rick and Morty - S05E10 -- 0",
 			args: args{audioInfo: vad.AudioInfo{
 				FileFullPath: "C:\\Tmp\\Rick and Morty - S05E10\\英_1.pcm"},
 				subFilePath: "C:\\Tmp\\Rick and Morty - S05E10\\英_2.ass"},
@@ -718,18 +718,42 @@ func TestGetOffsetTimeV2_BaseAudio(t *testing.T) {
 				subFilePath: "C:\\Tmp\\Rick and Morty - S05E01\\英_2.ass"},
 			want: false, want1: 0,
 		},
-		{name: "Rick and Morty - S05E01 -- 0",
+		{name: "Rick and Morty - S05E01 -- 1",
 			args: args{audioInfo: vad.AudioInfo{
 				FileFullPath: "C:\\Tmp\\Rick and Morty - S05E01\\未知语言_1.pcm"},
 				subFilePath: "C:\\Tmp\\Rick and Morty - S05E01\\英_2.srt"},
 			want: false, want1: 0,
 		},
-		{name: "Rick and Morty - S05E01 -- 1",
+		{name: "Rick and Morty - S05E01 -- 2",
 			args: args{audioInfo: vad.AudioInfo{
 				FileFullPath: "C:\\Tmp\\Rick and Morty - S05E01\\未知语言_1.pcm"},
 				subFilePath: "C:\\Tmp\\Rick and Morty - S05E01\\Rick and Morty - S05E01 - Mort Dinner Rick Andre WEBDL-1080p.chinese(简英,zimuku).ass"},
 			want: true, want1: -6.4,
 		},
+		{name: "Foundation - S01E09 -- 0",
+			args: args{audioInfo: vad.AudioInfo{
+				FileFullPath: "C:\\Tmp\\Foundation - S01E09\\英_1.pcm"},
+				subFilePath: "C:\\Tmp\\Foundation - S01E09\\英_2.srt"},
+			want: true, want1: 0,
+		},
+		{name: "Foundation - S01E09 -- 1",
+			args: args{audioInfo: vad.AudioInfo{
+				FileFullPath: "C:\\Tmp\\Foundation - S01E09\\英_1.pcm"},
+				subFilePath: "C:\\Tmp\\Foundation - S01E09\\简_6.srt"},
+			want: true, want1: 0,
+		},
+		{name: "Foundation - S01E09 -- 2",
+			args: args{audioInfo: vad.AudioInfo{
+				FileFullPath: "C:\\Tmp\\Foundation - S01E09\\英_1.pcm"},
+				subFilePath: "C:\\Tmp\\Foundation - S01E09\\Foundation (2021) - S01E09 - The First Crisis WEBDL-1080p.chinese(简英,zimuku).default.ass"},
+			want: true, want1: 0,
+		},
+		{name: "Foundation - S01E09 -- 3",
+			args: args{audioInfo: vad.AudioInfo{
+				FileFullPath: "C:\\Tmp\\Foundation - S01E09\\英_1.pcm"},
+				subFilePath: "C:\\Tmp\\Foundation - S01E09\\Foundation (2021) - S01E09 - The First Crisis WEBDL-1080p.chinese(简英,zimuku-fix).ass"},
+			want: true, want1: 0,
+		},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
@@ -750,17 +774,17 @@ func TestGetOffsetTimeV2_BaseAudio(t *testing.T) {
 			*/
 			//sub_helper.MergeMultiDialogue4EngSubtitle(infoSrc)
 			// Src,截取的部分要小于 Base 的部分
-			srcUnitList, err := sub_helper.GetVADInfosFromSub(infoSrc, FrontAndEndPerSrc, 1)
+			srcUnitNewList, err := sub_helper.GetVADInfoFeatureFromSubNew(infoSrc, FrontAndEndPerSrc, 1)
 			if err != nil {
 				t.Fatal(err)
 			}
-			srcUnit := srcUnitList[0]
+			srcUnitNew := srcUnitNewList[0]
 
-			srcUnitList2, err := sub_helper.GetVADInfoFeatureFromSub(infoSrc, FrontAndEndPerSrc, 10000, true)
+			srcUnitOldList, err := sub_helper.GetVADInfoFeatureFromSub(infoSrc, FrontAndEndPerSrc, 10000, true)
 			if err != nil {
 				t.Fatal(err)
 			}
-			srcUnit2 := srcUnitList2[0]
+			srcUnitOld := srcUnitOldList[0]
 
 			audioVADInfos, err := vad.GetVADInfoFromAudio(vad.AudioInfo{
 				FileFullPath: tt.args.audioInfo.FileFullPath,
@@ -777,16 +801,16 @@ func TestGetOffsetTimeV2_BaseAudio(t *testing.T) {
 				t.Fatal(err)
 			}
 
-			got, got1, got2, err := s.GetOffsetTimeV2(nil, &srcUnit, audioVADInfos, duration)
-			got, got1, got2, err = s.GetOffsetTimeV2(nil, &srcUnit2, audioVADInfos, duration)
+			got, got1, got2, err := s.GetOffsetTimeV2(nil, &srcUnitNew, audioVADInfos, duration)
+			got, got1, got2, err = s.GetOffsetTimeV2(nil, &srcUnitOld, audioVADInfos, duration)
 			if (err != nil) != tt.wantErr {
 				t.Errorf("GetOffsetTimeV3() error = %v, wantErr %v", err, tt.wantErr)
 				return
 			}
 
 			debug_view.SaveDebugChartBase(audioVADInfos, "audioVADInfos", "audioVADInfos")
-			debug_view.SaveDebugChart(srcUnit, "srcUnit", "srcUnit")
-			debug_view.SaveDebugChart(srcUnit2, "srcUnit2", "srcUnit2")
+			debug_view.SaveDebugChart(srcUnitNew, "srcUnitNew", "srcUnitNew")
+			debug_view.SaveDebugChart(srcUnitOld, "srcUnitOld", "srcUnitOld")
 
 			if got != tt.want {
 				t.Errorf("GetOffsetTimeV3() got = %v, want %v", got, tt.want)