Browse Source

修复,srt 解析某些类型字幕遇到的问题,比如 what if s01e01 srt ;

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

+ 1 - 8
internal/downloader.go

@@ -604,6 +604,7 @@ func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFil
 	if err != nil {
 		return err
 	}
+	d.log.Infoln("----------------------------------")
 	d.log.Infoln("OrgSubName:", finalSubFile.Name)
 	d.log.Infoln("SubDownAt:", desSubFullPath)
 
@@ -613,14 +614,6 @@ func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFil
 		if err != nil {
 			return err
 		}
-	} else {
-		// 如何不使用时间轴校正的功能,那么就需要把备份的给删除了,免得引起误会
-		if my_util.IsFile(desSubFullPath+sub_timeline_fixer.BackUpExt) == true {
-			err = os.Remove(desSubFullPath + sub_timeline_fixer.BackUpExt)
-			if err != nil {
-				return err
-			}
-		}
 	}
 
 	return nil

+ 20 - 15
internal/logic/sub_timeline_fixer/SubTimelineFixerHelperEx.go

@@ -169,16 +169,8 @@ func (s SubTimelineFixerHelperEx) processBySub(baseSubFileFPath, srcSubFileFPath
 		log_helper.GetLogger().Warnln("processSub.GetOffsetTimeV2 return false -- " + baseSubFileFPath)
 		return false, nil, 0, nil
 	}
-	// SD 要达标
-	if sd > s.timelineFixer.FixerConfig.V2_MaxStartTimeDiffSD {
-		log_helper.GetLogger().Infoln(fmt.Sprintf("skip, processBySub sd: %v > %v", sd, s.timelineFixer.FixerConfig.V2_MaxStartTimeDiffSD))
-		return false, nil, 0, nil
-	}
-	// 时间偏移的最小值才修正
-	if offsetTime < s.timelineFixer.FixerConfig.V2_MinOffset && offsetTime > -s.timelineFixer.FixerConfig.V2_MinOffset {
-		log_helper.GetLogger().Infoln(fmt.Sprintf("Skip, processBySub offset: %v > -%v && %v < %v",
-			offsetTime, s.timelineFixer.FixerConfig.V2_MinOffset,
-			offsetTime, s.timelineFixer.FixerConfig.V2_MinOffset))
+
+	if s.jugOffsetAndSD("processBySub", offsetTime, sd) == false {
 		return false, nil, 0, nil
 	}
 
@@ -219,20 +211,33 @@ func (s SubTimelineFixerHelperEx) processByAudio(baseAudioFileFPath, srcSubFileF
 		return false, nil, 0, nil
 	}
 
+	if s.jugOffsetAndSD("processByAudio", offsetTime, sd) == false {
+		return false, nil, 0, nil
+	}
+
+	return true, infoSrc, offsetTime, nil
+}
+
+func (s SubTimelineFixerHelperEx) jugOffsetAndSD(processName string, offsetTime, sd float64) bool {
 	// SD 要达标
 	if sd > s.timelineFixer.FixerConfig.V2_MaxStartTimeDiffSD {
-		log_helper.GetLogger().Infoln(fmt.Sprintf("processByAudio sd: %v > %v", sd, s.timelineFixer.FixerConfig.V2_MaxStartTimeDiffSD))
-		return false, nil, 0, nil
+		log_helper.GetLogger().Infoln(fmt.Sprintf("skip, %s sd: %v > %v", processName, sd, s.timelineFixer.FixerConfig.V2_MaxStartTimeDiffSD))
+		return false
 	}
 	// 时间偏移的最小值才修正
 	if offsetTime < s.timelineFixer.FixerConfig.V2_MinOffset && offsetTime > -s.timelineFixer.FixerConfig.V2_MinOffset {
-		log_helper.GetLogger().Infoln(fmt.Sprintf("skip, processByAudio offset: %v > -%v && %v < %v",
+		log_helper.GetLogger().Infoln(fmt.Sprintf("Skip, %s offset: %v > -%v && %v < %v",
+			processName,
 			offsetTime, s.timelineFixer.FixerConfig.V2_MinOffset,
 			offsetTime, s.timelineFixer.FixerConfig.V2_MinOffset))
-		return false, nil, 0, nil
+		return false
+	}
+	// sub_timeline_fixer.calcMeanAndSD 输出的可能的极小值
+	if offsetTime <= sub_timeline_fixer.MinValue+0.1 || offsetTime >= sub_timeline_fixer.MinValue-0.1 {
+		return false
 	}
 
-	return true, infoSrc, offsetTime, nil
+	return true
 }
 
 func (s SubTimelineFixerHelperEx) changeTimeLineAndSave(infoSrc *subparser.FileInfo, offsetTime float64, desSubSaveFPath string) error {

+ 2 - 2
internal/pkg/regex_things/regex_things.go

@@ -20,8 +20,8 @@ var RegMatchSpString = regexp.MustCompile(`(?i)[^\w\s]`)
 // 字幕文件对话的每一行
 // regStringASS = `Dialogue: [^,.]*[0-9]*,([1-9]?[0-9]*:[0-9]*:[0-9]*.[0-9]*),([1-9]?[0-9]*:[0-9]*:[0-9]*.[0-9]*),[^,.]*,[^,.]*,[0-9]*,[0-9]*,[0-9]*,[^,.]*,(.*)`
 const regStringASS = `Dialogue: [^,.]*[0-9]*,([1-9]?[0-9]*:[0-9]*:[0-9]*.[0-9]*),([1-9]?[0-9]*:[0-9]*:[0-9]*.[0-9]*),([^,.]*),[^,.]*,[0-9]*,[0-9]*,[0-9]*,[^,.]*,(.*)`
-const regStringSRT = `(\d+)\n([\d:,]+)\s+-{2}\>\s+([\d:,]+)\n([\s\S]*?(\n{2}|$))`
-const regStringSRT2 = `(\d+)\n([\d:.]+)\s+-{2}\>\s+([\d:.]+)\n([\s\S]*?(\n{2}|$))`
+const regStringSRT = `(\d+)\n([\d:,]+)\s+-{2}\>\s+([\d:,]+)\n([\s\S]*?(\n{1,2}|$))`
+const regStringSRT2 = `(\d+)\n([\d:.]+)\s+-{2}\>\s+([\d:.]+)\n([\s\S]*?(\n{1,2}|$))`
 
 var ReMatchDialogueASS = regexp.MustCompile(regStringASS)
 var ReMatchDialogueSRT = regexp.MustCompile(regStringSRT)

+ 6 - 5
internal/pkg/sub_timeline_fixer/fixer.go

@@ -542,11 +542,11 @@ func (s *SubTimelineFixer) GetOffsetTimeV2(baseUnit, srcUnit *sub_helper.SubUnit
 }
 
 func (s *SubTimelineFixer) calcMeanAndSD(startDiffTimeList stat.Float64Slice, tmpStartDiffTime []float64) FixResult {
-	const minValue = -9999.0
+
 	oldMean := stat.Mean(startDiffTimeList)
 	oldSd := stat.Sd(startDiffTimeList)
-	newMean := minValue
-	newSd := minValue
+	newMean := MinValue
+	newSd := MinValue
 	per := 1.0
 
 	if len(tmpStartDiffTime) < 3 {
@@ -590,10 +590,10 @@ func (s *SubTimelineFixer) calcMeanAndSD(startDiffTimeList stat.Float64Slice, tm
 		newSd = stat.Sd(startDiffTimeList)
 	}
 
-	if newMean == minValue {
+	if newMean == MinValue {
 		newMean = oldMean
 	}
-	if newSd == minValue {
+	if newSd == MinValue {
 		newSd = oldSd
 	}
 	return FixResult{
@@ -606,6 +606,7 @@ func (s *SubTimelineFixer) calcMeanAndSD(startDiffTimeList stat.Float64Slice, tm
 }
 
 const FixMask = "-fix"
+const MinValue = -9999.0
 
 var mutexFixV2 sync.Mutex
 

+ 7 - 2
internal/pkg/sub_timeline_fixer/fixer_test.go

@@ -548,6 +548,11 @@ func TestGetOffsetTimeV2_BaseSub(t *testing.T) {
 		/*
 			What If…!
 		*/
+		{name: "What If…! - S01E01", args: args{
+			baseSubFile:            filepath.Join(testRootDirNo, "What If…! - S01E01_英_2.srt"),
+			srcSubFile:             filepath.Join(testRootDirNo, "What If…! - S01E01 - (简英,shooter).srt"),
+			staticLineFileSavePath: "bar.html"},
+			want: 0, wantErr: false},
 		{name: "What If…! - S01E07", args: args{
 			baseSubFile:            filepath.Join(testRootDirNo, "What If…! - S01E07.chinese(inside).ass"),
 			srcSubFile:             filepath.Join(testRootDirNo, "What If…! - S01E07.chinese(简英,subhd).ass"),
@@ -629,8 +634,8 @@ func TestGetOffsetTimeV2_BaseSub(t *testing.T) {
 				t.Errorf("GetOffsetTimeV2() got = %v, want %v", got, tt.want)
 			}
 
-			debug_view.SaveDebugChart(*baseUnitNew, "baseUnitNew", "baseUnitNew")
-			debug_view.SaveDebugChart(*srcUnitNew, "srcUnitNew", "srcUnitNew")
+			debug_view.SaveDebugChart(*baseUnitNew, tt.name+" -- baseUnitNew", "baseUnitNew")
+			debug_view.SaveDebugChart(*srcUnitNew, tt.name+" -- srcUnitNew", "srcUnitNew")
 
 			//if bok == true && got != 0 {
 			//	_, err = timelineFixer.FixSubTimeline(infoSrc, got, tt.args.srcSubFile+FixMask+infoBase.Ext)