Преглед на файлове

新增,自动校正字幕的还原功能

Signed-off-by: 716 <[email protected]>
716 преди 3 години
родител
ревизия
b54b941c75

+ 7 - 0
cmd/chinesesubfinder/main.go

@@ -175,6 +175,13 @@ func DownLoadStart(httpProxy string) {
 	if err != nil {
 		log.Errorln("ReadSpeFile", err)
 	}
+	// 从 csf-bk 文件还原时间轴修复前的字幕文件
+	if downloader.NeedRestoreFixTimeLineBK == true {
+		err = downloader.RestoreFixTimelineBK(config.MovieFolder, config.SeriesFolder)
+		if err != nil {
+			log.Errorln("RestoreFixTimelineBK", err)
+		}
+	}
 	// 刷新 Emby 的字幕,如果下载了字幕倒是没有刷新,则先刷新一次,便于后续的 Emby api 统计逻辑
 	err = downloader.RefreshEmbySubList()
 	if err != nil {

+ 24 - 2
internal/downloader.go

@@ -6,6 +6,7 @@ import (
 	embyHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/emby_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/forced_scan_and_down_sub"
 	markSystem "github.com/allanpk716/ChineseSubFinder/internal/logic/mark_system"
+	"github.com/allanpk716/ChineseSubFinder/internal/logic/restore_fix_timeline_bk"
 	seriesHelper "github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
 	subSupplier "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier/shooter"
@@ -17,6 +18,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	subcommon "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
+	sub_timeline_fixer_pkg "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_timeline_fixer"
 	"github.com/allanpk716/ChineseSubFinder/internal/types"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/emby"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/series"
@@ -42,6 +44,7 @@ type Downloader struct {
 	subFormatter             ifaces.ISubFormatter          //	字幕格式化命名的实现
 	subNameFormatter         subcommon.FormatterName       // 从 inSubFormatter 推断出来
 	needForcedScanAndDownSub bool                          // 将会强制扫描所有的视频,下载字幕,替换已经存在的字幕,不进行时间段和已存在则跳过的判断。且不会进过 Emby API 的逻辑,智能进行强制去以本程序的方式去扫描。
+	NeedRestoreFixTimeLineBK bool                          // 从 csf-bk 文件还原时间轴修复前的字幕文件
 
 	subTimelineFixerHelperEx *sub_timeline_fixer.SubTimelineFixerHelperEx // 字幕时间轴校正
 }
@@ -97,11 +100,19 @@ func NewDownloader(inSubFormatter ifaces.ISubFormatter, _reqParam ...types.ReqPa
 // ReadSpeFile 优先级最高。读取特殊文件,启用一些特殊的功能,比如 forced_scan_and_down_sub
 func (d *Downloader) ReadSpeFile() error {
 	// 理论上是一次性的,用了这个文件就应该没了
-	needProcess, err := forced_scan_and_down_sub.CheckSpeFile()
+	// 强制的字幕扫描
+	needProcess_forced_scan_and_down_sub, err := forced_scan_and_down_sub.CheckSpeFile()
 	if err != nil {
 		return err
 	}
-	d.needForcedScanAndDownSub = needProcess
+	d.needForcedScanAndDownSub = needProcess_forced_scan_and_down_sub
+	// 从 csf-bk 文件还原时间轴修复前的字幕文件
+	needProcess_restore_fix_timeline_bk, err := restore_fix_timeline_bk.CheckSpeFile()
+	if err != nil {
+		return err
+	}
+	d.NeedRestoreFixTimeLineBK = needProcess_restore_fix_timeline_bk
+
 	return nil
 }
 
@@ -426,6 +437,17 @@ func (d Downloader) DownloadSub4Series(dir string) error {
 	return nil
 }
 
+func (d Downloader) RestoreFixTimelineBK(moviesDir, seriesDir string) error {
+
+	defer d.log.Infoln("End Restore Fix Timeline BK")
+	d.log.Infoln("Start Restore Fix Timeline BK...")
+	err := sub_timeline_fixer_pkg.Restore(moviesDir, seriesDir)
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
 // oneVideoSelectBestSub 一个视频,选择最佳的一个字幕(也可以保存所有网站第一个最佳字幕)
 func (d Downloader) oneVideoSelectBestSub(oneVideoFullPath string, organizeSubFiles []string) {
 

+ 1 - 1
internal/logic/forced_scan_and_down_sub/forced_scan_and_down_sub.go

@@ -47,6 +47,6 @@ func getSpeFileName() string {
 	对于 MacOS 需要自行实现
 */
 const (
-	specialFileNameLinux   = "/config/ForceFullScanAndDownloadSub"
 	specialFileNameWindows = "ForceFullScanAndDownloadSub"
+	specialFileNameLinux   = "/config/" + specialFileNameWindows
 )

+ 52 - 0
internal/logic/restore_fix_timeline_bk/restore_fix_timeline_bk.go

@@ -0,0 +1,52 @@
+package restore_fix_timeline_bk
+
+import (
+	"errors"
+	"fmt"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
+	"os"
+	"runtime"
+)
+
+// CheckSpeFile 目标是检测特定的文件,找到后,先删除,返回一个标志位用于后面的逻辑
+func CheckSpeFile() (bool, error) {
+
+	nowSpeFileName := getSpeFileName()
+	if nowSpeFileName == "" {
+		return false, errors.New(fmt.Sprintf(`restore_fix_timeline_bk.getSpeFileName() is empty, not support this OS. 
+you needd implement getSpeFileName() in internal/logic/restore_fix_timeline_bk/restore_fix_timeline_bk.go`))
+	}
+	if my_util.IsFile(nowSpeFileName) == false {
+		return false, nil
+	}
+	// 先删除这个文件,然后再标记执行该逻辑
+	err := os.Remove(nowSpeFileName)
+	if err != nil {
+		return false, err
+	}
+
+	return true, nil
+}
+
+func getSpeFileName() string {
+	nowSpeFileName := ""
+	sysType := runtime.GOOS
+	if sysType == "linux" {
+		nowSpeFileName = specialFileNameLinux
+	}
+	if sysType == "windows" {
+		nowSpeFileName = specialFileNameWindows
+	}
+	return nowSpeFileName
+}
+
+/*
+	识别 config 文件夹下面由这个特殊的文件,就会执行从 csf-bk 文件还原时间轴修复前的字幕文件
+	对于 Linux 是 /config 文件夹下
+	对于 Windows 是程序根目录下
+	对于 MacOS 需要自行实现
+*/
+const (
+	specialFileNameWindows = "RestoreFixTimelineBK"
+	specialFileNameLinux   = "/config/" + specialFileNameWindows
+)

+ 5 - 8
internal/logic/sub_timeline_fixer/SubTimelineFixerHelperEx.go

@@ -132,7 +132,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+sub_timeline_fixer.BackUpExt)
 
 	return nil
 }
@@ -248,7 +248,7 @@ func (s SubTimelineFixerHelperEx) changeTimeLineAndSave(infoSrc *subparser.FileI
 	/*
 		修复的字幕先存放到缓存目录,然后需要把原有的字幕进行“备份”,改名,然后再替换过来
 	*/
-	subFileName := desSubSaveFPath + TmpExt
+	subFileName := desSubSaveFPath + sub_timeline_fixer.TmpExt
 	if my_util.IsFile(subFileName) == true {
 		err := os.Remove(subFileName)
 		if err != nil {
@@ -260,14 +260,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+sub_timeline_fixer.BackUpExt) == true {
+		err = os.Remove(desSubSaveFPath + sub_timeline_fixer.BackUpExt)
 		if err != nil {
 			return err
 		}
 	}
 
-	err = os.Rename(desSubSaveFPath, desSubSaveFPath+BackUpExt)
+	err = os.Rename(desSubSaveFPath, desSubSaveFPath+sub_timeline_fixer.BackUpExt)
 	if err != nil {
 		return err
 	}
@@ -279,6 +279,3 @@ func (s SubTimelineFixerHelperEx) changeTimeLineAndSave(infoSrc *subparser.FileI
 
 	return nil
 }
-
-const TmpExt = ".csf-tmp"
-const BackUpExt = ".csf-bk"

+ 76 - 0
internal/pkg/sub_timeline_fixer/restore.go

@@ -0,0 +1,76 @@
+package sub_timeline_fixer
+
+import (
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
+	"io/ioutil"
+	"os"
+	"path/filepath"
+	"strings"
+)
+
+// Restore 从备份还原自动校正的字幕文件
+func Restore(movieDir, seriesDir string) error {
+	// 搜索出所有的 csf-bk 文件
+	backUpSubMoviesFilePathList, err := searchBackUpSubFile(movieDir)
+	if err != nil {
+		return err
+	}
+	backUpSubSeriesFilePathList, err := searchBackUpSubFile(seriesDir)
+	if err != nil {
+		return err
+	}
+	allBkFilesPath := make([]string, len(backUpSubMoviesFilePathList)+len(backUpSubSeriesFilePathList))
+	allBkFilesPath = append(allBkFilesPath, backUpSubMoviesFilePathList...)
+	allBkFilesPath = append(allBkFilesPath, backUpSubSeriesFilePathList...)
+	// 通过这些文件,判断当前每个 bk 下面是否有相应的文件,如果在则删除,然后再重命名 bk 文件回原来的文件名称
+	// Fargo - S04E04 - The Pretend War WEBDL-1080p.chinese(简英,shooter).default.ass.csf-bk
+	// Fargo - S04E04 - The Pretend War WEBDL-1080p.chinese(简英,shooter).default.ass
+	for index, oneBkFile := range allBkFilesPath {
+
+		fixedFileName := strings.ReplaceAll(oneBkFile, BackUpExt, "")
+		if my_util.IsFile(fixedFileName) == true {
+			err = os.Remove(fixedFileName)
+			if err != nil {
+				return err
+			}
+			err = os.Rename(oneBkFile, fixedFileName)
+			if err != nil {
+				return err
+			}
+			log_helper.GetLogger().Infoln("Restore", index, fixedFileName)
+		}
+	}
+
+	return nil
+}
+
+func searchBackUpSubFile(dir string) ([]string, error) {
+
+	var fileFullPathList = make([]string, 0)
+	pathSep := string(os.PathSeparator)
+	files, err := ioutil.ReadDir(dir)
+	if err != nil {
+		return nil, err
+	}
+	for _, curFile := range files {
+		fullPath := dir + pathSep + curFile.Name()
+		if curFile.IsDir() {
+			// 内层的错误就无视了
+			oneList, _ := searchBackUpSubFile(fullPath)
+			if oneList != nil {
+				fileFullPathList = append(fileFullPathList, oneList...)
+			}
+		} else {
+			// 这里就是文件了
+			if filepath.Ext(curFile.Name()) == BackUpExt {
+				fileFullPathList = append(fileFullPathList, fullPath)
+			}
+		}
+	}
+
+	return fileFullPathList, nil
+}
+
+const TmpExt = ".csf-tmp"
+const BackUpExt = ".csf-bk"

+ 23 - 0
internal/pkg/sub_timeline_fixer/restore_test.go

@@ -0,0 +1,23 @@
+package sub_timeline_fixer
+
+import (
+	"testing"
+)
+
+func Test_searchBackUpSubFile(t *testing.T) {
+
+	dir := "X:\\连续剧"
+	files, err := searchBackUpSubFile(dir)
+	if err != nil {
+		t.Fatal(err)
+	}
+	println(len(files))
+}
+
+func TestRestore(t *testing.T) {
+
+	err := Restore("X:\\电影", "X:\\连续剧")
+	if err != nil {
+		t.Fatal(err)
+	}
+}