Browse Source

修复,字幕校正细节问题

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

+ 8 - 0
internal/downloader.go

@@ -613,6 +613,14 @@ 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

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

@@ -84,6 +84,9 @@ func (s SubTimelineFixerHelperEx) Process(videoFileFullPath, srcSubFPath string)
 			return nil
 		}
 		bProcess, infoSrc, offSetTime, err = s.processByAudio(ffmpegInfo.AudioInfoList[0].FullPath, srcSubFPath)
+		if err != nil {
+			return err
+		}
 	} else {
 		// 使用内置的字幕进行时间轴的校正,这里需要考虑一个问题,内置的字幕可能是有问题的(先考虑一种,就是字幕的长度不对,是一小段的)
 		// 那么就可以比较多个内置字幕的大小选择大的去使用
@@ -106,6 +109,9 @@ func (s SubTimelineFixerHelperEx) Process(videoFileFullPath, srcSubFPath string)
 			baseSubFPath = ffmpegInfo.SubtitleInfoList[0].FullPath
 		}
 		bProcess, infoSrc, offSetTime, err = s.processBySub(baseSubFPath, srcSubFPath)
+		if err != nil {
+			return err
+		}
 	}
 
 	// 开始调整字幕时间轴
@@ -119,7 +125,7 @@ func (s SubTimelineFixerHelperEx) Process(videoFileFullPath, srcSubFPath string)
 	}
 
 	log_helper.GetLogger().Infoln("Fix Offset:", offSetTime, srcSubFPath)
-	log_helper.GetLogger().Infoln("BackUp Org SubFile:", offSetTime, srcSubFPath+backUpExt)
+	log_helper.GetLogger().Infoln("BackUp Org SubFile:", offSetTime, srcSubFPath+BackUpExt)
 
 	return nil
 }
@@ -131,7 +137,7 @@ func (s SubTimelineFixerHelperEx) processBySub(baseSubFileFPath, srcSubFileFPath
 		return false, nil, 0, err
 	}
 	if bFind == false {
-		log_helper.GetLogger().Warnln("sub not match --", baseSubFileFPath)
+		log_helper.GetLogger().Warnln("processBySub.DetermineFileTypeFromFile sub not match --", baseSubFileFPath)
 		return false, nil, 0, nil
 	}
 	bFind, infoSrc, err := s.subParserHub.DetermineFileTypeFromFile(srcSubFileFPath)
@@ -139,7 +145,7 @@ func (s SubTimelineFixerHelperEx) processBySub(baseSubFileFPath, srcSubFileFPath
 		return false, nil, 0, err
 	}
 	if bFind == false {
-		log_helper.GetLogger().Warnln("sub not match --", srcSubFileFPath)
+		log_helper.GetLogger().Warnln("processBySub.DetermineFileTypeFromFile sub not match --", srcSubFileFPath)
 		return false, nil, 0, nil
 	}
 	// ---------------------------------------------------------------------------------------
@@ -157,20 +163,19 @@ func (s SubTimelineFixerHelperEx) processBySub(baseSubFileFPath, srcSubFileFPath
 		return false, nil, 0, err
 	}
 	if bok == false {
-		log_helper.GetLogger().Warnln("processSub.GetOffsetTimeV2 return false -- " + 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().Warnln(fmt.Sprintf("skip, processBySub sd: %v > %v -- %s", sd, s.timelineFixer.FixerConfig.V2_MaxStartTimeDiffSD, srcSubFileFPath))
+		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().Warnln(fmt.Sprintf("skip, processBySub offset: %v > -%v && %v < %v-- %s",
+		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,
-			srcSubFileFPath))
+			offsetTime, s.timelineFixer.FixerConfig.V2_MinOffset))
 		return false, nil, 0, nil
 	}
 
@@ -193,7 +198,7 @@ func (s SubTimelineFixerHelperEx) processByAudio(baseAudioFileFPath, srcSubFileF
 		return false, nil, 0, err
 	}
 	if bFind == false {
-		log_helper.GetLogger().Warnln("sub not match --", srcSubFileFPath)
+		log_helper.GetLogger().Warnln("processByAudio.DetermineFileTypeFromFile sub not match --", srcSubFileFPath)
 		return false, nil, 0, nil
 	}
 	// ---------------------------------------------------------------------------------------
@@ -207,21 +212,20 @@ func (s SubTimelineFixerHelperEx) processByAudio(baseAudioFileFPath, srcSubFileF
 		return false, nil, 0, err
 	}
 	if bok == false {
-		log_helper.GetLogger().Warnln("processSub.GetOffsetTimeV2 return false -- " + baseAudioFileFPath + " -- " + srcSubFileFPath)
+		log_helper.GetLogger().Warnln("processSub.GetOffsetTimeV2 return false -- " + baseAudioFileFPath)
 		return false, nil, 0, nil
 	}
 
 	// SD 要达标
 	if sd > s.timelineFixer.FixerConfig.V2_MaxStartTimeDiffSD {
-		log_helper.GetLogger().Warnln(fmt.Sprintf("processByAudio sd: %v > %v -- %s", sd, s.timelineFixer.FixerConfig.V2_MaxStartTimeDiffSD, srcSubFileFPath))
+		log_helper.GetLogger().Infoln(fmt.Sprintf("processByAudio 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().Warnln(fmt.Sprintf("skip, processByAudio offset: %v > -%v && %v < %v-- %s",
-			offsetTime, s.timelineFixer.FixerConfig.V2_MinOffset,
+		log_helper.GetLogger().Infoln(fmt.Sprintf("skip, processByAudio offset: %v > -%v && %v < %v",
 			offsetTime, s.timelineFixer.FixerConfig.V2_MinOffset,
-			srcSubFileFPath))
+			offsetTime, s.timelineFixer.FixerConfig.V2_MinOffset))
 		return false, nil, 0, nil
 	}
 
@@ -232,7 +236,7 @@ func (s SubTimelineFixerHelperEx) changeTimeLineAndSave(infoSrc *subparser.FileI
 	/*
 		修复的字幕先存放到缓存目录,然后需要把原有的字幕进行“备份”,改名,然后再替换过来
 	*/
-	subFileName := desSubSaveFPath + tmpExt
+	subFileName := desSubSaveFPath + TmpExt
 	if my_util.IsFile(subFileName) == true {
 		err := os.Remove(subFileName)
 		if err != nil {
@@ -244,14 +248,14 @@ func (s SubTimelineFixerHelperEx) changeTimeLineAndSave(infoSrc *subparser.FileI
 		return err
 	}
 
-	if my_util.IsFile(desSubSaveFPath+backUpExt) == true {
-		err = os.Remove(desSubSaveFPath + backUpExt)
+	if my_util.IsFile(desSubSaveFPath+BackUpExt) == true {
+		err = os.Remove(desSubSaveFPath + BackUpExt)
 		if err != nil {
 			return err
 		}
 	}
 
-	err = os.Rename(desSubSaveFPath, desSubSaveFPath+backUpExt)
+	err = os.Rename(desSubSaveFPath, desSubSaveFPath+BackUpExt)
 	if err != nil {
 		return err
 	}
@@ -264,5 +268,5 @@ func (s SubTimelineFixerHelperEx) changeTimeLineAndSave(infoSrc *subparser.FileI
 	return nil
 }
 
-const tmpExt = ".csf-tmp"
-const backUpExt = ".csf-bk"
+const TmpExt = ".csf-tmp"
+const BackUpExt = ".csf-bk"

+ 7 - 0
internal/logic/sub_timeline_fixer/SubTimelineFixerHelperEx_test.go

@@ -35,9 +35,16 @@ func TestSubTimelineFixerHelperEx_Process(t *testing.T) {
 				srcSubFPath:       "C:\\WorkSpace\\Go2Hell\\src\\github.com\\allanpk716\\ChineseSubFinder\\internal\\logic\\sub_timeline_fixer\\CSF-SubFixCache\\Foundation (2021) - S01E09 - The First Crisis WEBDL-1080p\\chinese(简英,zimuku).default.ass"},
 			wantErr: false,
 		},
+		{
+			name: "The Night House (2021)", args: args{
+				videoFileFullPath: "X:\\TestMovie\\The Night House (2021)\\The Night House (2021) Bluray-1080p.mkv",
+				srcSubFPath:       "X:\\TestMovie\\The Night House (2021)\\The Night House (2021) Bluray-1080p.chinese(简英,zimuku).ass"},
+			wantErr: false,
+		},
 	}
 
 	s := NewSubTimelineFixerHelperEx(config.GetConfig().SubTimelineFixerConfig)
+	s.Check()
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
 

+ 18 - 0
internal/pkg/ffmpeg_helper/ffmpeg_helper.go

@@ -111,6 +111,17 @@ func (f *FFMPEGHelper) GetFFMPEGInfo(videoFileFullPath string, exportType Export
 			bok = false
 			return bok, nil, err
 		}
+		// 导出后,需要把现在导出的文件的路径给复制给 ffMPEGInfo 中
+		// 音频是否导出了
+		ffMPEGInfo.isAudioExported(nowCacheFolderPath)
+		// 字幕都要导出了
+		ffMPEGInfo.isSubExported(nowCacheFolderPath)
+		// 创建 exportedMakeFileName 这个文件
+		// 成功,那么就需要生成这个 exportedMakeFileName 文件
+		err = ffMPEGInfo.CreateExportedMask()
+		if err != nil {
+			return false, nil, err
+		}
 	}
 
 	return bok, ffMPEGInfo, nil
@@ -261,6 +272,10 @@ func (f *FFMPEGHelper) parseJsonString2GetFFProbeInfo(videoFileFullPath, inputFF
 		// 这里需要区分是字幕还是音频
 		if oneCodecType.String() == codecTypeSub {
 			// 字幕
+			// 只解析 subrip 类型的,不支持 hdmv_pgs_subtitle 的字幕导出
+			if oneCodecName.String() != streamCodec_subrip {
+				continue
+			}
 			// 这里非必须解析到 language 字段,把所有的都导出来,然后通过额外字幕语言判断即可
 			oneDurationTS := gjson.Get(inputFFProbeString, fmt.Sprintf("streams.%d.duration_ts", i))
 			oneDuration := gjson.Get(inputFFProbeString, fmt.Sprintf("streams.%d.duration", i))
@@ -431,6 +446,7 @@ func (f *FFMPEGHelper) getAudioAndSubExportArgs(videoFileFullPath string, ffmpeg
 	}
 
 	for _, subtitleInfo := range ffmpegInfo.SubtitleInfoList {
+
 		f.addSubMapArg(&subArgs, subtitleInfo.Index,
 			filepath.Join(nowCacheFolderPath, subtitleInfo.GetName()+common.SubExtSRT))
 		f.addSubMapArg(&subArgs, subtitleInfo.Index,
@@ -560,3 +576,5 @@ const (
 	Audio                              // 导出音频
 	SubtitleAndAudio                   // 导出字幕和音频
 )
+
+const streamCodec_subrip = "subrip"

+ 58 - 7
internal/pkg/ffmpeg_helper/ffmpeg_info.go

@@ -7,6 +7,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
+	"os"
 	"path/filepath"
 	"strings"
 )
@@ -37,14 +38,31 @@ func (f *FFMPEGInfo) GetCacheFolderFPath() (string, error) {
 // IsExported 是否已经导出过,如果没有导出或者导出不完整为 false
 func (f *FFMPEGInfo) IsExported(exportType ExportType) bool {
 
+	bProcessDone := false
 	nowCacheFolder, err := f.GetCacheFolderFPath()
 	if err != nil {
 		log_helper.GetLogger().Errorln("FFMPEGInfo.IsExported.GetCacheFolderFPath", f.VideoFullPath, err.Error())
 		return false
 	}
+	tmpNowExportedMaskFile := filepath.Join(nowCacheFolder, exportedMakeFileName)
+
+	defer func() {
+		// 函数执行完毕,再进行 check,是否需要删除 exportedMakeFileName 这个文件
+		if bProcessDone == false {
+			// 失败就需要删除这个 exportedMakeFileName 文件
+			if my_util.IsFile(tmpNowExportedMaskFile) == true {
+				_ = os.Remove(tmpNowExportedMaskFile)
+			}
+		}
+	}()
+
 	// 首先存储的缓存目录要存在
 	if my_util.IsDir(nowCacheFolder) == false {
-		return false
+		return bProcessDone
+	}
+
+	if my_util.IsFile(tmpNowExportedMaskFile) == false {
+		return bProcessDone
 	}
 
 	switch exportType {
@@ -52,30 +70,60 @@ func (f *FFMPEGInfo) IsExported(exportType ExportType) bool {
 		// 音频是否导出了
 		done := f.isAudioExported(nowCacheFolder)
 		if done == false {
-			return false
+			return bProcessDone
 		}
 		break
 	case Subtitle:
 		// 字幕都要导出了
 		done := f.isSubExported(nowCacheFolder)
 		if done == false {
-			return false
+			return bProcessDone
 		}
 	case SubtitleAndAudio:
 		// 音频是否导出了
 		done := f.isAudioExported(nowCacheFolder)
 		if done == false {
-			return false
+			return bProcessDone
 		}
 		// 字幕都要导出了
 		done = f.isSubExported(nowCacheFolder)
 		if done == false {
-			return false
+			return bProcessDone
 		}
 	default:
-		return false
+		return bProcessDone
 	}
-	return true
+
+	bProcessDone = true
+
+	return bProcessDone
+}
+
+func (f FFMPEGInfo) CreateExportedMask() error {
+	maskFileFPath, err := f.getExportedMaskFileFPath()
+	if err != nil {
+		return err
+	}
+	if my_util.IsFile(maskFileFPath) == false {
+		create, err := os.Create(maskFileFPath)
+		if err != nil {
+			return err
+		}
+		defer create.Close()
+	}
+
+	return nil
+}
+
+func (f FFMPEGInfo) getExportedMaskFileFPath() (string, error) {
+	nowCacheFolder, err := f.GetCacheFolderFPath()
+	if err != nil {
+		return "", err
+	}
+
+	tmpNowExportedMaskFile := filepath.Join(nowCacheFolder, exportedMakeFileName)
+
+	return tmpNowExportedMaskFile, nil
 }
 
 func (f *FFMPEGInfo) isAudioExported(nowCacheFolder string) bool {
@@ -128,3 +176,6 @@ func (f *FFMPEGInfo) GetExternalSubInfos(subParserHub *sub_parser_hub.SubParserH
 
 	return nil
 }
+
+// 导出成功才生成这个文件
+const exportedMakeFileName = "Exported"

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

@@ -456,8 +456,10 @@ func (s *SubTimelineFixer) GetOffsetTimeV2(baseUnit, srcUnit *sub_helper.SubUnit
 		}
 		// 时间差值
 		TimeDiffStartCorrelation := nowBaseStartTime - nowSrcStartTime
-		println("------------")
-		println("OffsetTime:", fmt.Sprintf("%v", TimeDiffStartCorrelation), "offsetIndex:", offsetIndex, "score:", fmt.Sprintf("%v", score))
+		log_helper.GetLogger().Debugln("------------")
+		log_helper.GetLogger().Debugln("OffsetTime:", fmt.Sprintf("%v", TimeDiffStartCorrelation),
+			"offsetIndex:", offsetIndex,
+			"score:", fmt.Sprintf("%v", score))
 
 		mutexFixV2.Lock()
 		tmpStartDiffTimeList = append(tmpStartDiffTimeList, TimeDiffStartCorrelation)
@@ -530,11 +532,11 @@ func (s *SubTimelineFixer) GetOffsetTimeV2(baseUnit, srcUnit *sub_helper.SubUnit
 		return false, 0, 0, nil
 	}
 	outCorrelationFixResult := s.calcMeanAndSD(tmpStartDiffTimeListEx, tmpStartDiffTimeList)
-	println(fmt.Sprintf("FFTAligner Old Mean: %v SD: %v Per: %v", outCorrelationFixResult.OldMean, outCorrelationFixResult.OldSD, outCorrelationFixResult.Per))
-	println(fmt.Sprintf("FFTAligner New Mean: %v SD: %v Per: %v", outCorrelationFixResult.NewMean, outCorrelationFixResult.NewSD, outCorrelationFixResult.Per))
+	log_helper.GetLogger().Infoln(fmt.Sprintf("FFTAligner Old Mean: %v SD: %v Per: %v", outCorrelationFixResult.OldMean, outCorrelationFixResult.OldSD, outCorrelationFixResult.Per))
+	log_helper.GetLogger().Infoln(fmt.Sprintf("FFTAligner New Mean: %v SD: %v Per: %v", outCorrelationFixResult.NewMean, outCorrelationFixResult.NewSD, outCorrelationFixResult.Per))
 
 	value, index := tmpStartDiffTimeMap.Max()
-	println("FFTAligner Max score:", fmt.Sprintf("%v", value.(float64)), "Time:", fmt.Sprintf("%v", tmpStartDiffTimeList[index.(int)]))
+	log_helper.GetLogger().Infoln("FFTAligner Max score:", fmt.Sprintf("%v", value.(float64)), "Time:", fmt.Sprintf("%v", tmpStartDiffTimeList[index.(int)]))
 
 	return true, outCorrelationFixResult.NewMean, outCorrelationFixResult.NewSD, nil
 }