Skip to content

Commit

Permalink
新增字幕与视频名称相同的命名格式,还需要前端对接
Browse files Browse the repository at this point in the history
Signed-off-by: allan716 <525223688@qq.com>
  • Loading branch information
allanpk716 committed Apr 24, 2023
1 parent 210f184 commit 78a834b
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 6 deletions.
8 changes: 6 additions & 2 deletions pkg/sub_formatter/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package common

const FormatterNameString_Normal = "normal formatter"
const FormatterNameString_Emby = "emby formatter"
const FormatterNameString_SampleAsVideoName = "sample as video name formatter"
const NoMatchFormatter = "No Match formatter"

type FormatterName int

const (
Emby FormatterName = iota // Emby 格式 xxx.chinese.(简,shooter).ass
Normal // 常规 xxx.zh.ass
Emby FormatterName = iota // Emby 格式 xxx.chinese.(简,shooter).ass
Normal // 常规 xxx.zh.ass
SampleAsVideoName // 与视频文件名称相同
)

func (f FormatterName) String() string {
Expand All @@ -17,6 +19,8 @@ func (f FormatterName) String() string {
return FormatterNameString_Normal
case Emby:
return FormatterNameString_Emby
case SampleAsVideoName:
return FormatterNameString_SampleAsVideoName
default:
return NoMatchFormatter
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/sub_formatter/normal/normal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package normal

import (
"github.com/WQGroup/logger"
"path/filepath"
"testing"

Expand All @@ -11,7 +12,7 @@ import (
)

func TestFormatter_GetFormatterName(t *testing.T) {
f := NewFormatter()
f := NewFormatter(logger.GetLogger())
if f.GetFormatterName() != subCommon.FormatterNameString_Normal {
t.Errorf("GetFormatterName error")
}
Expand Down
75 changes: 75 additions & 0 deletions pkg/sub_formatter/same_as_video_name/same_as_video_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package same_as_video_name

import (
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/logic/sub_parser/ass"
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/logic/sub_parser/srt"
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/sub_formatter/common"
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/sub_parser_hub"
language2 "github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/language"
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/subparser"
"github.com/sirupsen/logrus"
"path/filepath"
"strings"
)

type Formatter struct {
log *logrus.Logger
subParser *sub_parser_hub.SubParserHub
}

func NewFormatter(log *logrus.Logger) *Formatter {
return &Formatter{log: log, subParser: sub_parser_hub.NewSubParserHub(log, ass.NewParser(log), srt.NewParser(log))}
}

// GetFormatterName 当前的 Formatter 是那个
func (f Formatter) GetFormatterName() string {
return common.FormatterNameString_SampleAsVideoName
}

func (f Formatter) GetFormatterFormatterName() int {
return int(common.SampleAsVideoName)
}

// IsMatchThisFormat 是否满足当前实现接口的字幕命名格式 - 是否符合规则、fileNameWithOutExt string, subExt string, subLang types.MyLanguage, extraSubPreName string
func (f Formatter) IsMatchThisFormat(subName string) (bool, string, string, language2.MyLanguage, string) {
/*
这里要判断的是跟视频文件名称一样的字幕文件命名格式:
The Boss Baby Family Business (2021) WEBDL-1080p.mp4
对应字幕:
The Boss Baby Family Business (2021) WEBDL-1080p.ass
*/
subNameBase := filepath.Base(subName)
subNameDir := filepath.Dir(subName)
// 这个情况下,字幕只可能有一个 . 符号存在,如果没有或者有多个,则认为不属于
if strings.Contains(subNameBase, ".") == false {
return false, "", "", language2.Unknown, ""
}
if strings.Count(subNameBase, ".") > 1 {
return false, "", "", language2.Unknown, ""
}
// 获取文件的后缀名
subExt := filepath.Ext(subNameBase)
fileNameWithOutExt := strings.ReplaceAll(subNameBase, subExt, "")

return true, filepath.Join(subNameDir, fileNameWithOutExt), subExt, language2.Unknown, ""
}

// GenerateMixSubName 通过视频和字幕信息,生成当前实现接口的字幕命名格式。extraSubPreName 一般是填写字幕网站,不填写则留空 - 新名称、新名称带有 default 标记,新名称带有 forced 标记
func (f Formatter) GenerateMixSubName(videoFileName, subExt string, subLang language2.MyLanguage, extraSubPreName string) (string, string, string) {
/*
这里会生成类似的文件名 xxxx.zh
*/
videoFileNameWithOutExt := strings.ReplaceAll(filepath.Base(videoFileName),
filepath.Ext(videoFileName), "")
return f.GenerateMixSubNameBase(videoFileNameWithOutExt, subExt, subLang, extraSubPreName)
}

func (f Formatter) GenerateMixSubNameBase(fileNameWithOutExt, subExt string, subLang language2.MyLanguage, extraSubPreName string) (string, string, string) {
// 这里传入字幕后缀名的时候,可能会带有 default 或者 forced 字段,需要剔除
nowSubExt := strings.ReplaceAll(subExt, subparser.Sub_Ext_Mark_Default, "")
nowSubExt = strings.ReplaceAll(nowSubExt, subparser.Sub_Ext_Mark_Forced, "")

subNewName := fileNameWithOutExt + nowSubExt

return subNewName, subNewName, subNewName
}
129 changes: 129 additions & 0 deletions pkg/sub_formatter/same_as_video_name/same_as_video_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package same_as_video_name

import (
subCommon "github.com/ChineseSubFinder/ChineseSubFinder/pkg/sub_formatter/common"
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/common"
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/language"
"github.com/WQGroup/logger"
"path/filepath"
"testing"
)

func TestFormatter_GetFormatterName(t *testing.T) {
f := NewFormatter(logger.GetLogger())
if f.GetFormatterName() != subCommon.FormatterNameString_SampleAsVideoName {
t.Errorf("GetFormatterName error")
}
}

func TestFormatter_IsMatchThisFormat(t *testing.T) {

const fileWithOutExt = "The Boss Baby Family Business (2021) WEBDL-1080p"

type args struct {
subName string
}
tests := []struct {
name string
args args
want bool
want1 string
want2 string
want3 language.MyLanguage
want4 string
}{
{name: "00", args: args{subName: "The Boss Baby Family Business (2021) WEBDL-1080p.ass"},
want: true,
want1: fileWithOutExt,
want2: ".ass",
want3: language.Unknown,
want4: ""},
{name: "01", args: args{subName: "The Boss Baby Family Business (2021) WEBDL-1080p.zho.ass"},
want: false,
want1: "",
want2: "",
want3: language.Unknown,
want4: ""},
{name: "02", args: args{subName: "The Boss Baby Family Business (2021) WEBDL-1080p.zh.default.ass"},
want: false,
want1: "",
want2: "",
want3: language.Unknown,
want4: ""},
{name: "03", args: args{subName: filepath.FromSlash("../../../TestData/sub_format_changer/test/movie_org_emby/AAA/AAA.chinese(简英,subhd).ass")},
want: false,
want1: "",
want2: "",
want3: language.Unknown,
want4: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := Formatter{}
got, got1, got2, got3, got4 := f.IsMatchThisFormat(tt.args.subName)
if got != tt.want {
t.Errorf("IsMatchThisFormat() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("IsMatchThisFormat() got1 = %v, want %v", got1, tt.want1)
}
if got2 != tt.want2 {
t.Errorf("IsMatchThisFormat() got2 = %v, want %v", got2, tt.want2)
}
if got3 != tt.want3 {
t.Errorf("IsMatchThisFormat() got3 = %v, want %v", got3, tt.want3)
}
if got4 != tt.want4 {
t.Errorf("IsMatchThisFormat() got4 = %v, want %v", got4, tt.want4)
}
})
}
}

func TestFormatter_GenerateMixSubName(t *testing.T) {

const videoFileName = "Django Unchained (2012) Bluray-1080p.mp4"
const videoFileNamePre = "Django Unchained (2012) Bluray-1080p"

type args struct {
videoFileName string
subExt string
subLang language.MyLanguage
extraSubPreName string
}
tests := []struct {
name string
args args
want string
want1 string
want2 string
}{
{name: "zh", args: args{videoFileName: videoFileName, subExt: common.SubExtASS, subLang: language.ChineseSimple, extraSubPreName: ""},
want: videoFileNamePre + ".ass",
want1: videoFileNamePre + ".ass",
want2: videoFileNamePre + ".ass"},
{name: "zh_shooter", args: args{videoFileName: videoFileName, subExt: common.SubExtASS, subLang: language.ChineseSimple, extraSubPreName: "shooter"},
want: videoFileNamePre + ".ass",
want1: videoFileNamePre + ".ass",
want2: videoFileNamePre + ".ass"},
{name: "zh_shooter2", args: args{videoFileName: videoFileName, subExt: common.SubExtASS, subLang: language.ChineseSimpleEnglish, extraSubPreName: "shooter"},
want: videoFileNamePre + ".ass",
want1: videoFileNamePre + ".ass",
want2: videoFileNamePre + ".ass"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := Formatter{}
got, got1, got2 := f.GenerateMixSubName(tt.args.videoFileName, tt.args.subExt, tt.args.subLang, tt.args.extraSubPreName)
if got != tt.want {
t.Errorf("GenerateMixSubName() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("GenerateMixSubName() got1 = %v, want %v", got1, tt.want1)
}
if got2 != tt.want2 {
t.Errorf("GenerateMixSubName() got2 = %v, want %v", got2, tt.want2)
}
})
}
}
14 changes: 11 additions & 3 deletions pkg/sub_formatter/sub_format_changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sub_formatter
import (
"errors"
"fmt"
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/sub_formatter/same_as_video_name"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -47,6 +48,9 @@ func NewSubFormatChanger(log *logrus.Logger, movieRootDirs []string, seriesRootD
// emby
embyM := emby.NewFormatter()
formatter.formatter[embyM.GetFormatterName()] = embyM
// same as video name
savnM := same_as_video_name.NewFormatter(log)
formatter.formatter[savnM.GetFormatterName()] = savnM
return &formatter
}

Expand All @@ -66,19 +70,19 @@ func (s *SubFormatChanger) AutoDetectThenChangeTo(desFormatter common.FormatterN
return RenameResults{}, err
}

s.log.Infoln("AutoDetectThenChangeTo Movie Index", i, dir, "Start")
s.log.Infoln("AutoDetectThenChangeTo Movie Index", i, dir, "End")
}

for i, dir := range s.seriesRootDirs {
s.log.Infoln("AutoDetectThenChangeTo Series Index", i, dir, "Start")

err := s.autoDetectMovieThenChangeTo(&outStruct, desFormatter, dir)
err := s.autoDetectMSeriesThenChangeTo(&outStruct, desFormatter, dir)
if err != nil {
s.log.Infoln("AutoDetectThenChangeTo Series Index", i, dir, "End")
return RenameResults{}, err
}

s.log.Infoln("AutoDetectThenChangeTo Series Index", i, dir, "Start")
s.log.Infoln("AutoDetectThenChangeTo Series Index", i, dir, "End")
}

return outStruct, nil
Expand Down Expand Up @@ -236,6 +240,10 @@ func GetSubFormatter(log *logrus.Logger, subNameFormatter int) ifaces.ISubFormat
subFormatter = normal.NewFormatter(log)
break
}
case int(common.SampleAsVideoName):
{
subFormatter = same_as_video_name.NewFormatter(log)
}
default:
{
subFormatter = emby.NewFormatter()
Expand Down

0 comments on commit 78a834b

Please sign in to comment.