Răsfoiți Sursa

新增更多 Debug 日志

Signed-off-by: allan716 <[email protected]>
allan716 4 ani în urmă
părinte
comite
446857c50b

+ 5 - 0
internal/common/constvalue.go

@@ -30,3 +30,8 @@ const (
 
 	SubTmpFolderName = "subtmp"
 )
+
+const (
+	TimeFormatAss = "15:04:05.00"
+	TimeFormatSrt = "15:04:05,000"
+)

+ 2 - 0
internal/logic/sub_parser/ass/ass.go

@@ -4,6 +4,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/common"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/language"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
 	"github.com/emirpasic/gods/maps/treemap"
 	"io/ioutil"
@@ -51,6 +52,7 @@ func (p Parser) DetermineFileTypeFromBytes(inBytes []byte, nowExt string) (bool,
 	// 找到 start end text
 	matched := sub_parser.ReMatchDialogueASS.FindAllStringSubmatch(allString, -1)
 	if len(matched) < 1 {
+		log_helper.GetLogger().Debugln("DetermineFileTypeFromBytes can't found Dialogues, Skip")
 		return false, nil, nil
 	}
 	subFileInfo := subparser.FileInfo{}

+ 2 - 0
internal/logic/sub_parser/srt/srt.go

@@ -4,6 +4,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/common"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/language"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
 	"io/ioutil"
 	"path/filepath"
@@ -52,6 +53,7 @@ func (p Parser) DetermineFileTypeFromBytes(inBytes []byte, nowExt string) (bool,
 	// 找到 start end text
 	matched := sub_parser.ReMatchDialogueSRT.FindAllStringSubmatch(allString, -1)
 	if len(matched) < 1 {
+		log_helper.GetLogger().Debugln("DetermineFileTypeFromBytes can't found Dialogues, Skip")
 		return false, nil, nil
 	}
 	subFileInfo := subparser.FileInfo{}

+ 21 - 5
internal/logic/sub_timeline_fixer/sub_timeline_fixer_helper.go

@@ -75,14 +75,14 @@ func (s SubTimelineFixerHelper) FixRecentlyItemsSubTimeline(movieRootDir, series
 
 	// 输出调试信息
 	log_helper.GetLogger().Debugln("FixRecentlyItemsSubTimeline - DebugInfo - movieList Start")
-	for s, _ := range movieList {
-		log_helper.GetLogger().Debugln(s)
+	for s, value := range movieList {
+		log_helper.GetLogger().Debugln(s, value)
 	}
 	log_helper.GetLogger().Debugln("FixRecentlyItemsSubTimeline - DebugInfo - movieList End")
 
 	log_helper.GetLogger().Debugln("FixRecentlyItemsSubTimeline - DebugInfo - seriesList Start")
-	for s, value := range seriesList {
-		log_helper.GetLogger().Debugln(s, value)
+	for s, _ := range seriesList {
+		log_helper.GetLogger().Debugln(s)
 	}
 	log_helper.GetLogger().Debugln("FixRecentlyItemsSubTimeline - DebugInfo - seriesList End")
 
@@ -118,14 +118,19 @@ func (s SubTimelineFixerHelper) FixRecentlyItemsSubTimeline(movieRootDir, series
 }
 
 func (s SubTimelineFixerHelper) fixOneVideoSub(videoId string, videoRootPath string) error {
+	log_helper.GetLogger().Debugln("fixOneVideoSub VideoROotPath:", videoRootPath)
 	// internalEngSub 默认第一个是 srt 然后第二个是 ass,就不要去遍历了
 	found, internalEngSub, exCh_EngSub, err := s.embyHelper.GetInternalEngSubAndExChineseEnglishSub(videoId)
 	if err != nil {
 		return err
 	}
+
 	if found == false {
+		log_helper.GetLogger().Debugln("GetInternalEngSubAndExChineseEnglishSub - found == false")
 		return nil
 	}
+
+	log_helper.GetLogger().Debugln("internalEngSub:", len(internalEngSub), "exCh_EngSub:", len(exCh_EngSub))
 	// 需要先把原有的外置字幕带有 -fix 的删除,然后再做修正
 	// 不然如果调整了条件,之前修复的本次其实就不修正了,那么就会“残留”下来,误以为是本次配置的信息导致的
 	for _, exSubInfo := range exCh_EngSub {
@@ -134,10 +139,15 @@ func (s SubTimelineFixerHelper) fixOneVideoSub(videoId string, videoRootPath str
 			continue
 		}
 
+		subFileNeedRemove := filepath.Join(videoRootPath, exSubInfo.FileName)
+
 		if videoRootPath == "" {
+			log_helper.GetLogger().Debugln("videoRootPath == \"\", Skip Remove:", subFileNeedRemove)
 			continue
 		}
-		err = os.Remove(filepath.Join(videoRootPath, exSubInfo.FileName))
+
+		log_helper.GetLogger().Debugln("Remove fixed sub:", subFileNeedRemove)
+		err = os.Remove(subFileNeedRemove)
 		if err != nil {
 			return err
 		}
@@ -153,15 +163,19 @@ func (s SubTimelineFixerHelper) fixOneVideoSub(videoId string, videoRootPath str
 		if strings.Contains(exSubInfo.FileName, sub_timeline_fixer.FixMask) == true {
 			continue
 		}
+
+		log_helper.GetLogger().Debugln("fixSubTimeline start")
 		bFound, subFixInfos, subNewName, err := s.fixSubTimeline(internalEngSub[inSelectSubIndex], exSubInfo)
 		if err != nil {
 			return err
 		}
 		if bFound == false {
+			log_helper.GetLogger().Debugln("fixSubTimeline bFound == false", exSubInfo.FileName)
 			continue
 		}
 		// 调试的时候用
 		if videoRootPath == "" {
+			log_helper.GetLogger().Debugln("videoRootPath == \"\", Skip fix sub:", exSubInfo.FileName)
 			continue
 		}
 		for _, info := range subFixInfos {
@@ -180,6 +194,7 @@ func (s SubTimelineFixerHelper) fixOneVideoSub(videoId string, videoRootPath str
 
 func (s SubTimelineFixerHelper) fixSubTimeline(enSubFile emby.SubInfo, ch_enSubFile emby.SubInfo) (bool, []sub_timeline_fixer.SubFixInfo, string, error) {
 	fixedSubName := ""
+	log_helper.GetLogger().Debugln("fixSubTimeline - DetermineFileTypeFromBytes", enSubFile.FileName)
 	bFind, infoBase, err := s.subParserHub.DetermineFileTypeFromBytes(enSubFile.Content, enSubFile.Ext)
 	if err != nil {
 		return false, nil, fixedSubName, err
@@ -194,6 +209,7 @@ func (s SubTimelineFixerHelper) fixSubTimeline(enSubFile emby.SubInfo, ch_enSubF
 	*/
 	sub_helper.MergeMultiDialogue4EngSubtitle(infoBase)
 
+	log_helper.GetLogger().Debugln("fixSubTimeline - DetermineFileTypeFromBytes", ch_enSubFile.FileName)
 	bFind, infoSrc, err := s.subParserHub.DetermineFileTypeFromBytes(ch_enSubFile.Content, ch_enSubFile.Ext)
 	if err != nil {
 		return false, nil, fixedSubName, err

+ 1 - 0
internal/pkg/sub_parser_hub/subParserHub.go

@@ -52,6 +52,7 @@ func (p SubParserHub) DetermineFileTypeFromFile(filePath string) (bool, *subpars
 }
 
 // DetermineFileTypeFromBytes 确定字幕文件的类型,是双语字幕或者某一种语言等等信息,如果返回 nil ,那么就说明都没有字幕的格式匹配上
+// 如果要做字幕的时间轴匹配,很可能需要一个功能 sub_helper.MergeMultiDialogue4EngSubtitle,但是仅仅是合并了 English 字幕时间轴
 func (p SubParserHub) DetermineFileTypeFromBytes(inBytes []byte, nowExt string) (bool, *subparser.FileInfo, error) {
 
 	for _, parser := range p.Parser {

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

@@ -160,9 +160,9 @@ func (s *SubTimelineFixer) GetOffsetTime(infoBase, infoSrc *subparser.FileInfo,
 
 	timeFormat := ""
 	if infoBase.Ext == common.SubExtASS || infoBase.Ext == common.SubExtSSA {
-		timeFormat = timeFormatAss
+		timeFormat = common.TimeFormatAss
 	} else {
-		timeFormat = timeFormatSrt
+		timeFormat = common.TimeFormatSrt
 	}
 
 	var startDiffTimeLineData = make([]opts.LineData, 0)
@@ -325,9 +325,9 @@ func (s *SubTimelineFixer) FixSubTimeline(infoSrc *subparser.FileInfo, inOffsetT
 	offsetTime := time.Duration(inOffsetTime*1000) * time.Millisecond
 	timeFormat := ""
 	if infoSrc.Ext == common.SubExtASS || infoSrc.Ext == common.SubExtSSA {
-		timeFormat = timeFormatAss
+		timeFormat = common.TimeFormatAss
 	} else {
-		timeFormat = timeFormatSrt
+		timeFormat = common.TimeFormatSrt
 	}
 	fixContent := infoSrc.Content
 	for _, srcOneDialogue := range infoSrc.Dialogues {
@@ -362,7 +362,4 @@ func (s *SubTimelineFixer) FixSubTimeline(infoSrc *subparser.FileInfo, inOffsetT
 	return fixContent, nil
 }
 
-const timeFormatAss = "15:04:05.00"
-const timeFormatSrt = "15:04:05,000"
-
 const FixMask = "-fix"

+ 100 - 0
internal/pkg/vad/vad_helper.go

@@ -0,0 +1,100 @@
+package vad
+
+import (
+	"bufio"
+	"errors"
+	"fmt"
+	"github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
+	webRTCVAD "github.com/baabaaox/go-webrtcvad"
+	"io"
+	"os"
+	"time"
+)
+
+// GetVADInfoFromAudio 分析音频文件,得到 VAD 分析信息,看样子是不支持并发的,只能单线程使用
+func GetVADInfoFromAudio(audioInfo AudioInfo) ([]VADInfo, error) {
+
+	var (
+		frameIndex  = 0
+		frameSize   = audioInfo.SampleRate / 1000 * FrameDuration
+		frameBuffer = make([]byte, audioInfo.SampleRate/1000*FrameDuration*audioInfo.BitDepth/8)
+		frameActive = false
+		vadInfos    = make([]VADInfo, 0)
+	)
+
+	audioFile, err := os.Open(audioInfo.FileFullPath)
+	if err != nil {
+		return nil, err
+	}
+	defer audioFile.Close()
+
+	reader := bufio.NewReader(audioFile)
+
+	vadInst := webRTCVAD.Create()
+	defer webRTCVAD.Free(vadInst)
+
+	err = webRTCVAD.Init(vadInst)
+	if err != nil {
+		return nil, err
+	}
+
+	if err != nil {
+		return nil, err
+	}
+	err = webRTCVAD.SetMode(vadInst, Mode)
+	if err != nil {
+		return nil, err
+	}
+
+	if ok := webRTCVAD.ValidRateAndFrameLength(audioInfo.SampleRate, frameSize); !ok {
+		return nil, errors.New(fmt.Sprintf("invalid rate or frame length, %v", audioInfo.FileFullPath))
+	}
+	var offset int
+
+	report := func() {
+		t := time.Duration(offset) * time.Second / time.Duration(audioInfo.SampleRate) / 2
+		//log.Printf("Frame: %v, offset: %v, Active: %v, t = %v", frameIndex, offset, frameActive, t)
+		vadInfos = append(vadInfos, VADInfo{
+			Frame:  frameIndex,
+			Offset: offset,
+			Active: frameActive,
+			Time:   t,
+		})
+	}
+
+	for {
+		_, err = io.ReadFull(reader, frameBuffer)
+		if err == io.EOF || err == io.ErrUnexpectedEOF {
+			break
+		}
+		tmpFrameActive, err := webRTCVAD.Process(vadInst, audioInfo.SampleRate, frameBuffer, frameSize)
+		if err != nil {
+			return nil, err
+		}
+		if tmpFrameActive != frameActive || offset == 0 {
+			frameActive = tmpFrameActive
+		}
+		report()
+		offset += len(frameBuffer)
+		frameIndex++
+	}
+
+	report()
+
+	return vadInfos, nil
+}
+
+// GetVADInfoFromSubtitle 分析字幕文件(暂时考虑的是外置的字幕),得到 VAD 分析信息,看样子是不支持并发的,只能单线程使用
+func GetVADInfoFromSubtitle(subFileInfo *subparser.FileInfo) ([]VADInfo, error) {
+
+	var vadInfos = make([]VADInfo, 0)
+	// 考虑的是外置字幕,所以就应该是有中文的
+	for _, oneDialogueEx := range subFileInfo.DialoguesEx {
+		if oneDialogueEx.ChLine == "" {
+			continue
+		}
+
+	}
+
+	return vadInfos, nil
+}

+ 20 - 0
internal/pkg/vad/vad_helper_test.go

@@ -0,0 +1,20 @@
+package vad
+
+import (
+	"testing"
+)
+
+func TestGetVADInfo(t *testing.T) {
+
+	var audioInfo = AudioInfo{
+		FileFullPath: "C:\\Tmp\\Rick and Morty - S05E10\\英_1.pcm",
+		SampleRate:   16000,
+		BitDepth:     16,
+	}
+	vadInfo, err := GetVADInfoFromAudio(audioInfo)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	println(len(vadInfo))
+}

+ 0 - 77
internal/pkg/vad/vad_info.go

@@ -1,86 +1,9 @@
 package vad
 
 import (
-	"bufio"
-	"errors"
-	"fmt"
-	webRTCVAD "github.com/baabaaox/go-webrtcvad"
-	"io"
-	"os"
 	"time"
 )
 
-// GetVADInfo 分析音频文件,得到 VAD 分析信息,看样子是不支持并发的,只能单线程使用
-func GetVADInfo(audioInfo AudioInfo) ([]VADInfo, error) {
-
-	var (
-		frameIndex  = 0
-		frameSize   = audioInfo.SampleRate / 1000 * FrameDuration
-		frameBuffer = make([]byte, audioInfo.SampleRate/1000*FrameDuration*audioInfo.BitDepth/8)
-		frameActive = false
-		vadInfos    = make([]VADInfo, 0)
-	)
-
-	audioFile, err := os.Open(audioInfo.FileFullPath)
-	if err != nil {
-		return nil, err
-	}
-	defer audioFile.Close()
-
-	reader := bufio.NewReader(audioFile)
-
-	vadInst := webRTCVAD.Create()
-	defer webRTCVAD.Free(vadInst)
-
-	err = webRTCVAD.Init(vadInst)
-	if err != nil {
-		return nil, err
-	}
-
-	if err != nil {
-		return nil, err
-	}
-	err = webRTCVAD.SetMode(vadInst, Mode)
-	if err != nil {
-		return nil, err
-	}
-
-	if ok := webRTCVAD.ValidRateAndFrameLength(audioInfo.SampleRate, frameSize); !ok {
-		return nil, errors.New(fmt.Sprintf("invalid rate or frame length, %v", audioInfo.FileFullPath))
-	}
-	var offset int
-
-	report := func() {
-		t := time.Duration(offset) * time.Second / time.Duration(audioInfo.SampleRate) / 2
-		//log.Printf("Frame: %v, offset: %v, Active: %v, t = %v", frameIndex, offset, frameActive, t)
-		vadInfos = append(vadInfos, VADInfo{
-			Active: frameActive,
-			Time:   t,
-		})
-	}
-
-	for {
-		_, err = io.ReadFull(reader, frameBuffer)
-		if err == io.EOF || err == io.ErrUnexpectedEOF {
-			break
-		}
-		tmpFrameActive, err := webRTCVAD.Process(vadInst, audioInfo.SampleRate, frameBuffer, frameSize)
-		if err != nil {
-			return nil, err
-		}
-		if tmpFrameActive != frameActive || offset == 0 {
-			frameActive = tmpFrameActive
-			report()
-		}
-		offset += len(frameBuffer)
-		frameIndex++
-	}
-
-	report()
-
-	return vadInfos, nil
-}
-
 type VADInfo struct {
 	Frame  int           // 第几帧
 	Offset int           // 音频的偏移