Browse Source

新增自动转换字幕编码格式函数

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

+ 1 - 0
internal/pkg/change_file_encode_2_gbk/change_file_encode_2_gbk.go

@@ -0,0 +1 @@
+package change_file_encode_2_gbk

+ 15 - 0
internal/pkg/language/string_encoding.go

@@ -55,3 +55,18 @@ func ChangeFileCoding2UTF8(inBytes []byte) ([]byte, error) {
 
 	return []byte(validUTF8String), nil
 }
+
+func ChangeFileCoding2GBK(inBytes []byte) ([]byte, error) {
+
+	utf8Bytes, err := ChangeFileCoding2UTF8(inBytes)
+	if err != nil {
+		return nil, err
+	}
+
+	gbkString, err := charset.UTF8To("GBK", string(utf8Bytes))
+	if err != nil {
+		return nil, err
+	}
+
+	return []byte(gbkString), nil
+}

+ 118 - 0
internal/pkg/language/string_encoding_test.go

@@ -0,0 +1,118 @@
+package language
+
+import (
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
+	"os"
+	"path/filepath"
+	"reflect"
+	"testing"
+)
+
+func TestChangeFileCoding2UTF8(t *testing.T) {
+	type args struct {
+		subFileFPath string
+	}
+	tests := []struct {
+		name                string
+		args                args
+		wantDesSubFileFPath string
+		wantErr             bool
+	}{
+		{
+			name: "00",
+			args: args{
+				subFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
+					"Seven.Worlds.One.Planet.S01E01.Antarctica.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,subhd).ass"),
+			},
+			wantDesSubFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
+				"Seven.Worlds.One.Planet.S01E01.Antarctica.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,subhd-utf-8).ass"),
+			wantErr: false,
+		},
+		{
+			name: "01",
+			args: args{
+				subFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
+					"Seven.Worlds.One.Planet.S01E07.Africa.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,zimuku).default.srt"),
+			},
+			wantDesSubFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
+				"Seven.Worlds.One.Planet.S01E07.Africa.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,zimuku-utf-8).default.srt"),
+			wantErr: false,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+
+			fBytes, err := os.ReadFile(tt.args.subFileFPath)
+			if err != nil {
+				t.Fatal(err)
+			}
+			got, err := ChangeFileCoding2UTF8(fBytes)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("ChangeFileCoding2UTF8() error = %v, wantErr %v", err, tt.wantErr)
+				return
+			}
+
+			wantDesFBytes, err := os.ReadFile(tt.wantDesSubFileFPath)
+			if err != nil {
+				t.Fatal(err)
+			}
+			if !reflect.DeepEqual(got, wantDesFBytes) {
+				t.Errorf("ChangeFileCoding2UTF8() got = %v, want %v", got, wantDesFBytes)
+			}
+		})
+	}
+}
+
+func TestChangeFileCoding2GBK(t *testing.T) {
+	type args struct {
+		subFileFPath string
+	}
+	tests := []struct {
+		name                string
+		args                args
+		wantDesSubFileFPath string
+		wantErr             bool
+	}{
+		{
+			name: "00",
+			args: args{
+				subFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
+					"Seven.Worlds.One.Planet.S01E01.Antarctica.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,subhd).ass"),
+			},
+			wantDesSubFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
+				"Seven.Worlds.One.Planet.S01E01.Antarctica.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,subhd-gbk).ass"),
+			wantErr: false,
+		},
+		{
+			name: "01",
+			args: args{
+				subFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
+					"Seven.Worlds.One.Planet.S01E07.Africa.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,zimuku).default.srt"),
+			},
+			wantDesSubFileFPath: filepath.Join(unit_test_helper.GetTestDataResourceRootPath([]string{"change_sub_encode", "org-utf-8"}, 4, true),
+				"Seven.Worlds.One.Planet.S01E07.Africa.2160p.BluRay.REMUX.HEVC.DTS-HD.MA.TrueHD.7.1.Atmos-FGT.chinese(简英,zimuku-gbk).default.srt"),
+			wantErr: false,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+
+			fBytes, err := os.ReadFile(tt.args.subFileFPath)
+			if err != nil {
+				t.Fatal(err)
+			}
+			got, err := ChangeFileCoding2GBK(fBytes)
+			if (err != nil) != tt.wantErr {
+				t.Errorf("ChangeFileCoding2GBK() error = %v, wantErr %v", err, tt.wantErr)
+				return
+			}
+			wantDesFBytes, err := os.ReadFile(tt.wantDesSubFileFPath)
+			if err != nil {
+				t.Fatal(err)
+			}
+			if !reflect.DeepEqual(got, wantDesFBytes) {
+				t.Errorf("ChangeFileCoding2GBK() got = %v, want %v", got, wantDesFBytes)
+			}
+		})
+	}
+}

+ 6 - 0
internal/pkg/settings/auto_change_sub_encode.go

@@ -0,0 +1,6 @@
+package settings
+
+type AutoChangeSubEncode struct {
+	Enable        bool `json:"enable"`
+	DesEncodeType int  `json:"des_encode_type"` // 默认 0 是 UTF-8,1 是 GBK
+}

+ 10 - 0
internal/pkg/settings/experimental_function.go

@@ -0,0 +1,10 @@
+package settings
+
+// ExperimentalFunction 实验性功能
+type ExperimentalFunction struct {
+	AutoChangeSubEncode AutoChangeSubEncode `json:"auto_change_sub_encode"`
+}
+
+func NewExperimentalFunction() *ExperimentalFunction {
+	return &ExperimentalFunction{}
+}

+ 2 - 0
internal/pkg/settings/settings.go

@@ -19,6 +19,7 @@ type Settings struct {
 	EmbySettings          *EmbySettings          `json:"emby_settings"`
 	DeveloperSettings     *DeveloperSettings     `json:"developer_settings"`
 	TimelineFixerSettings *TimelineFixerSettings `json:"timeline_fixer_settings"`
+	ExperimentalFunction  *ExperimentalFunction  `json:"experimental_function"`
 }
 
 // GetSettings 获取 Settings 的实例
@@ -77,6 +78,7 @@ func NewSettings() *Settings {
 		EmbySettings:          NewEmbySettings(),
 		DeveloperSettings:     NewDeveloperSettings(),
 		TimelineFixerSettings: NewTimelineFixerSettings(),
+		ExperimentalFunction:  NewExperimentalFunction(),
 	}
 }