-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: allan716 <525223688@qq.com>
- Loading branch information
1 parent
210f184
commit 78a834b
Showing
5 changed files
with
223 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
pkg/sub_formatter/same_as_video_name/same_as_video_name.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
129
pkg/sub_formatter/same_as_video_name/same_as_video_name_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters