Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
feat: add subtitle style model
wulien3eedf5dfcc55
diff --git a/internal/subtitle_style/style.go b/internal/subtitle_style/style.gonew file mode 100644index 00000000..97211dad--- /dev/null+++ b/internal/subtitle_style/style.go@@ -0,0 +1,501 @@+package subtitlestyle++import (+ "bytes"+ "encoding/json"+ "fmt"+ "os"+ "strconv"+ "strings"+)++type StyleSet struct {+ Horizontal ScreenStyle `json:"horizontal"`+ Vertical ScreenStyle `json:"vertical"`+}++type ScreenStyle struct {+ Major Style `json:"major"`+ Minor Style `json:"minor"`+}++type Style struct {+ Name string `json:"name"`+ FontName string `json:"font_name"`+ FontSize *int `json:"font_size"`+ PrimaryColor string `json:"primary_color"`+ SecondaryColor string `json:"secondary_color"`+ OutlineColor string `json:"outline_color"`+ BackColor string `json:"back_color"`+ Bold *bool `json:"bold"`+ Italic *bool `json:"italic"`+ Underline *bool `json:"underline"`+ StrikeOut *bool `json:"strike_out"`+ ScaleX *int `json:"scale_x"`+ ScaleY *int `json:"scale_y"`+ Spacing *float64 `json:"spacing"`+ Angle *float64 `json:"angle"`+ BorderStyle *int `json:"border_style"`+ Outline *float64 `json:"outline"`+ Shadow *float64 `json:"shadow"`+ AlignmentValue *int `json:"alignment"`+ MarginL *int `json:"margin_l"`+ MarginR *int `json:"margin_r"`+ MarginV *int `json:"margin_v"`+ Encoding *int `json:"encoding"`+ RawASSStyle string `json:"raw_ass_style"`+ FadeInMS *int `json:"fade_in_ms"`+ FadeOutMS *int `json:"fade_out_ms"`+ OverrideTags string `json:"override_tags"`+}++func DefaultStyleSet() *StyleSet {+ return &StyleSet{+ Horizontal: ScreenStyle{+ Major: defaultStyle("Major", 14, 2.5, 1.5, 20),+ Minor: defaultStyle("Minor", 10, 2.5, 1.5, 30),+ },+ Vertical: ScreenStyle{+ Major: defaultStyle("Major", 12, 2.2, 1.2, 92),+ Minor: defaultStyle("Minor", 7, 2.0, 1.0, 101),+ },+ }+}++func LoadOverrideFile(path string) (*StyleSet, error) {+ data, err := os.ReadFile(path)+ if err != nil {+ return nil, err+ }+ return Decode(data, path)+}++func Decode(data []byte, source string) (*StyleSet, error) {+ var set StyleSet+ decoder := json.NewDecoder(bytes.NewReader(data))+ decoder.DisallowUnknownFields()+ if err := decoder.Decode(&set); err != nil {+ return nil, fmt.Errorf("%s: %w", source, err)+ }+ if err := Validate(&set); err != nil {+ return nil, fmt.Errorf("%s: %w", source, err)+ }+ return &set, nil+}++func Merge(base, override *StyleSet) (*StyleSet, error) {+ if base == nil {+ base = &StyleSet{}+ }+ merged := cloneStyleSet(base)+ if override != nil {+ mergeScreen(&merged.Horizontal, override.Horizontal)+ mergeScreen(&merged.Vertical, override.Vertical)+ }+ if err := Validate(&merged); err != nil {+ return nil, err+ }+ return &merged, nil+}++func Validate(set *StyleSet) error {+ if set == nil {+ return nil+ }+ if err := validateScreen("horizontal", set.Horizontal); err != nil {+ return err+ }+ return validateScreen("vertical", set.Vertical)+}++func BuildAssHeader(set *StyleSet, horizontal bool) string {+ if set == nil {+ set = DefaultStyleSet()+ }+ screen := set.Vertical+ if horizontal {+ screen = set.Horizontal+ }+ var builder strings.Builder+ builder.WriteString("[Script Info]\n")+ builder.WriteString("Title: Example\n")+ builder.WriteString("Original Script: \n")+ builder.WriteString("ScriptType: v4.00+\n")+ builder.WriteString("PlayDepth: 0\n\n")+ builder.WriteString("[V4+ Styles]\n")+ builder.WriteString("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n")+ builder.WriteString(assStyleLine(screen.Major, "Major"))+ builder.WriteByte('\n')+ builder.WriteString(assStyleLine(screen.Minor, "Minor"))+ builder.WriteString("\n\n\n")+ builder.WriteString("[Events]\n")+ builder.WriteString("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n")+ return builder.String()+}++func DialogueTags(s Style) string {+ var tags strings.Builder+ if s.FadeInMS != nil || s.FadeOutMS != nil || s.OverrideTags != "" {+ tags.WriteString("{")+ if s.FadeInMS != nil || s.FadeOutMS != nil {+ in := 0+ out := 0+ if s.FadeInMS != nil {+ in = *s.FadeInMS+ }+ if s.FadeOutMS != nil {+ out = *s.FadeOutMS+ }+ tags.WriteString(fmt.Sprintf(`\fad(%d,%d)`, in, out))+ }+ tags.WriteString(s.OverrideTags)+ tags.WriteString("}")+ }+ return tags.String()+}++func Alignment(s Style) int {+ if s.AlignmentValue == nil || *s.AlignmentValue < 1 || *s.AlignmentValue > 9 {+ return 2+ }+ return *s.AlignmentValue+}++func NormalizeASSColor(input string) (string, error) {+ color := strings.TrimSpace(input)+ if color == "" {+ return "", fmt.Errorf("color is empty")+ }+ if strings.HasPrefix(color, "&H") || strings.HasPrefix(color, "&h") {+ if len(color) != 10 {+ return "", fmt.Errorf("ASS color %q must be &HAABBGGRR", input)+ }+ hex := strings.ToUpper(color[2:])+ if _, err := strconv.ParseUint(hex, 16, 32); err != nil {+ return "", fmt.Errorf("invalid ASS color %q: %w", input, err)+ }+ return "&H" + hex, nil+ }+ if !strings.HasPrefix(color, "#") {+ return "", fmt.Errorf("color %q must start with # or &H", input)+ }+ hex := color[1:]+ if len(hex) != 6 && len(hex) != 8 {+ return "", fmt.Errorf("HTML color %q must be #RRGGBB or #RRGGBBAA", input)+ }+ if _, err := strconv.ParseUint(hex, 16, 32); err != nil {+ return "", fmt.Errorf("invalid HTML color %q: %w", input, err)+ }+ hex = strings.ToUpper(hex)+ alpha := "00"+ if len(hex) == 8 {+ alpha = hex[6:8]+ }+ red := hex[0:2]+ green := hex[2:4]+ blue := hex[4:6]+ return "&H" + alpha + blue + green + red, nil+}++func defaultStyle(name string, fontSize int, outline, shadow float64, marginV int) Style {+ return Style{+ Name: name,+ FontName: "Arial",+ FontSize: styleIntPtr(fontSize),+ PrimaryColor: "#FFBF00",+ SecondaryColor: "&H000000FF",+ OutlineColor: "&H00000000",+ BackColor: "&H64000000",+ Bold: styleBoolPtr(true),+ Italic: styleBoolPtr(false),+ Underline: styleBoolPtr(false),+ StrikeOut: styleBoolPtr(false),+ ScaleX: styleIntPtr(100),+ ScaleY: styleIntPtr(100),+ Spacing: styleFloatPtr(0),+ Angle: styleFloatPtr(0),+ BorderStyle: styleIntPtr(1),+ Outline: styleFloatPtr(outline),+ Shadow: styleFloatPtr(shadow),+ AlignmentValue: styleIntPtr(2),+ MarginL: styleIntPtr(10),+ MarginR: styleIntPtr(10),+ MarginV: styleIntPtr(marginV),+ Encoding: styleIntPtr(1),+ }+}++func assStyleLine(style Style, fallbackName string) string {+ if style.RawASSStyle != "" {+ return style.RawASSStyle+ }+ name := valueOr(style.Name, fallbackName)+ fontName := valueOr(style.FontName, "Arial")+ primary := normalizeOrDefault(style.PrimaryColor, "&H0000BFFF")+ secondary := normalizeOrDefault(style.SecondaryColor, "&H000000FF")+ outlineColor := normalizeOrDefault(style.OutlineColor, "&H00000000")+ backColor := normalizeOrDefault(style.BackColor, "&H64000000")+ return fmt.Sprintf("Style: %s,%s,%d,%s,%s,%s,%s,%d,%d,%d,%d,%d,%d,%s,%s,%d,%s,%s,%d,%d,%d,%d,%d",+ name,+ fontName,+ intValue(style.FontSize, 14),+ primary,+ secondary,+ outlineColor,+ backColor,+ boolASSValue(style.Bold, true),+ boolASSValue(style.Italic, false),+ boolASSValue(style.Underline, false),+ boolASSValue(style.StrikeOut, false),+ intValue(style.ScaleX, 100),+ intValue(style.ScaleY, 100),+ formatFloat(floatValue(style.Spacing, 0)),+ formatFloat(floatValue(style.Angle, 0)),+ intValue(style.BorderStyle, 1),+ formatFloat(floatValue(style.Outline, 2.5)),+ formatFloat(floatValue(style.Shadow, 1.5)),+ Alignment(style),+ intValue(style.MarginL, 10),+ intValue(style.MarginR, 10),+ intValue(style.MarginV, 20),+ intValue(style.Encoding, 1),+ )+}++func mergeScreen(base *ScreenStyle, override ScreenStyle) {+ base.Major = mergeStyle(base.Major, override.Major)+ base.Minor = mergeStyle(base.Minor, override.Minor)+}++func mergeStyle(base, override Style) Style {+ if override.Name != "" {+ base.Name = override.Name+ }+ if override.FontName != "" {+ base.FontName = override.FontName+ }+ if override.FontSize != nil {+ base.FontSize = styleIntPtr(*override.FontSize)+ }+ if override.PrimaryColor != "" {+ base.PrimaryColor = override.PrimaryColor+ }+ if override.SecondaryColor != "" {+ base.SecondaryColor = override.SecondaryColor+ }+ if override.OutlineColor != "" {+ base.OutlineColor = override.OutlineColor+ }+ if override.BackColor != "" {+ base.BackColor = override.BackColor+ }+ if override.Bold != nil {+ base.Bold = styleBoolPtr(*override.Bold)+ }+ if override.Italic != nil {+ base.Italic = styleBoolPtr(*override.Italic)+ }+ return mergeStyleRest(base, override)+}++func mergeStyleRest(base, override Style) Style {+ if override.Underline != nil {+ base.Underline = styleBoolPtr(*override.Underline)+ }+ if override.StrikeOut != nil {+ base.StrikeOut = styleBoolPtr(*override.StrikeOut)+ }+ if override.ScaleX != nil {+ base.ScaleX = styleIntPtr(*override.ScaleX)+ }+ if override.ScaleY != nil {+ base.ScaleY = styleIntPtr(*override.ScaleY)+ }+ if override.Spacing != nil {+ base.Spacing = styleFloatPtr(*override.Spacing)+ }+ if override.Angle != nil {+ base.Angle = styleFloatPtr(*override.Angle)+ }+ if override.BorderStyle != nil {+ base.BorderStyle = styleIntPtr(*override.BorderStyle)+ }+ if override.Outline != nil {+ base.Outline = styleFloatPtr(*override.Outline)+ }+ if override.Shadow != nil {+ base.Shadow = styleFloatPtr(*override.Shadow)+ }+ if override.AlignmentValue != nil {+ base.AlignmentValue = styleIntPtr(*override.AlignmentValue)+ }+ if override.MarginL != nil {+ base.MarginL = styleIntPtr(*override.MarginL)+ }+ if override.MarginR != nil {+ base.MarginR = styleIntPtr(*override.MarginR)+ }+ if override.MarginV != nil {+ base.MarginV = styleIntPtr(*override.MarginV)+ }+ if override.Encoding != nil {+ base.Encoding = styleIntPtr(*override.Encoding)+ }+ if override.RawASSStyle != "" {+ base.RawASSStyle = override.RawASSStyle+ }+ if override.FadeInMS != nil {+ base.FadeInMS = styleIntPtr(*override.FadeInMS)+ }+ if override.FadeOutMS != nil {+ base.FadeOutMS = styleIntPtr(*override.FadeOutMS)+ }+ if override.OverrideTags != "" {+ base.OverrideTags = override.OverrideTags+ }+ return base+}++func validateScreen(path string, screen ScreenStyle) error {+ if err := validateStyle(path+".major", screen.Major); err != nil {+ return err+ }+ return validateStyle(path+".minor", screen.Minor)+}++func validateStyle(path string, style Style) error {+ if err := validateRawASSStyle(path+".raw_ass_style", style.RawASSStyle); err != nil {+ return err+ }+ if err := validateInt(path+".font_size", style.FontSize, 1, 200); err != nil {+ return err+ }+ if err := validateInt(path+".scale_x", style.ScaleX, 1, 400); err != nil {+ return err+ }+ if err := validateInt(path+".scale_y", style.ScaleY, 1, 400); err != nil {+ return err+ }+ if err := validateInt(path+".alignment", style.AlignmentValue, 1, 9); err != nil {+ return err+ }+ if err := validateInt(path+".margin_l", style.MarginL, 0, 2000); err != nil {+ return err+ }+ if err := validateInt(path+".margin_r", style.MarginR, 0, 2000); err != nil {+ return err+ }+ if err := validateInt(path+".margin_v", style.MarginV, 0, 2000); err != nil {+ return err+ }+ if err := validateFloat(path+".outline", style.Outline, 0, 20); err != nil {+ return err+ }+ if err := validateFloat(path+".shadow", style.Shadow, 0, 20); err != nil {+ return err+ }+ if err := validateInt(path+".fade_in_ms", style.FadeInMS, 0, 10000); err != nil {+ return err+ }+ return validateInt(path+".fade_out_ms", style.FadeOutMS, 0, 10000)+}++func validateRawASSStyle(path, raw string) error {+ if raw == "" {+ return nil+ }+ if !strings.HasPrefix(raw, "Style: ") {+ return fmt.Errorf("%s must start with \"Style: \"", path)+ }+ if len(strings.Split(raw, ",")) != 23 {+ return fmt.Errorf("%s must contain 23 comma-separated fields", path)+ }+ return nil+}++func validateInt(path string, value *int, min, max int) error {+ if value == nil {+ return nil+ }+ if *value < min || *value > max {+ return fmt.Errorf("%s must be between %d and %d", path, min, max)+ }+ return nil+}++func validateFloat(path string, value *float64, min, max float64) error {+ if value == nil {+ return nil+ }+ if *value < min || *value > max {+ return fmt.Errorf("%s must be between %s and %s", path, formatFloat(min), formatFloat(max))+ }+ return nil+}++func cloneStyleSet(set *StyleSet) StyleSet {+ return StyleSet{+ Horizontal: ScreenStyle{+ Major: cloneStyle(set.Horizontal.Major),+ Minor: cloneStyle(set.Horizontal.Minor),+ },+ Vertical: ScreenStyle{+ Major: cloneStyle(set.Vertical.Major),+ Minor: cloneStyle(set.Vertical.Minor),+ },+ }+}++func cloneStyle(style Style) Style {+ return mergeStyle(Style{}, style)+}++func normalizeOrDefault(input, fallback string) string {+ normalized, err := NormalizeASSColor(input)+ if err != nil {+ return fallback+ }+ return normalized+}++func valueOr(value, fallback string) string {+ if value == "" {+ return fallback+ }+ return value+}++func intValue(value *int, fallback int) int {+ if value == nil {+ return fallback+ }+ return *value+}++func floatValue(value *float64, fallback float64) float64 {+ if value == nil {+ return fallback+ }+ return *value+}++func boolASSValue(value *bool, fallback bool) int {+ result := fallback+ if value != nil {+ result = *value+ }+ if result {+ return -1+ }+ return 0+}++func formatFloat(value float64) string {+ return strconv.FormatFloat(value, 'f', -1, 64)+}++func styleIntPtr(v int) *int { return &v }++func styleFloatPtr(v float64) *float64 { return &v }++func styleBoolPtr(v bool) *bool { return &v }diff --git a/internal/subtitle_style/style_test.go b/internal/subtitle_style/style_test.gonew file mode 100644index 00000000..f013bfdd--- /dev/null+++ b/internal/subtitle_style/style_test.go@@ -0,0 +1,97 @@+package subtitlestyle++import (+ "os"+ "path/filepath"+ "strings"+ "testing"+)++func TestDefaultStyleBuildsCurrentHorizontalHeader(t *testing.T) {+ style := DefaultStyleSet()+ header := BuildAssHeader(style, true)++ if !strings.Contains(header, "Style: Major,Arial,14,&H0000BFFF,&H000000FF,&H00000000,&H64000000,-1,0,0,0,100,100,0,0,1,2.5,1.5,2,10,10,20,1") {+ t.Fatalf("horizontal Major style missing current defaults:\n%s", header)+ }+ if !strings.Contains(header, "Style: Minor,Arial,10,&H0000BFFF,&H000000FF,&H00000000,&H64000000,-1,0,0,0,100,100,0,0,1,2.5,1.5,2,10,10,30,1") {+ t.Fatalf("horizontal Minor style missing current defaults:\n%s", header)+ }+}++func TestParseColorConvertsHTMLToASS(t *testing.T) {+ got, err := NormalizeASSColor("#3366CC")+ if err != nil {+ t.Fatalf("NormalizeASSColor() error = %v", err)+ }+ if got != "&H00CC6633" {+ t.Fatalf("NormalizeASSColor() = %q, want &H00CC6633", got)+ }++ got, err = NormalizeASSColor("#3366CC80")+ if err != nil {+ t.Fatalf("NormalizeASSColor(alpha) error = %v", err)+ }+ if got != "&H80CC6633" {+ t.Fatalf("NormalizeASSColor(alpha) = %q, want &H80CC6633", got)+ }+}++func TestLoadOverrideRejectsUnknownField(t *testing.T) {+ dir := t.TempDir()+ path := filepath.Join(dir, "style.json")+ if err := os.WriteFile(path, []byte(`{"horizontal":{"major":{"font_colour":"#fff"}}}`), 0644); err != nil {+ t.Fatal(err)+ }++ _, err := LoadOverrideFile(path)+ if err == nil {+ t.Fatal("LoadOverrideFile() error = nil, want unknown field error")+ }+ if !strings.Contains(err.Error(), "font_colour") {+ t.Fatalf("error = %v, want field path containing font_colour", err)+ }+}++func TestMergeKeepsDefaultsForMissingFields(t *testing.T) {+ base := DefaultStyleSet()+ override := &StyleSet{+ Horizontal: ScreenStyle{+ Major: Style{PrimaryColor: "#FFFFFF", Outline: floatPtr(3)},+ },+ }++ got, err := Merge(base, override)+ if err != nil {+ t.Fatalf("Merge() error = %v", err)+ }+ if got.Horizontal.Major.PrimaryColor != "#FFFFFF" {+ t.Fatalf("primary color = %q, want override", got.Horizontal.Major.PrimaryColor)+ }+ if got.Horizontal.Major.FontSize == nil || *got.Horizontal.Major.FontSize != 14 {+ t.Fatalf("font size not inherited: %#v", got.Horizontal.Major.FontSize)+ }+ if got.Vertical.Minor.FontSize == nil || *got.Vertical.Minor.FontSize != 7 {+ t.Fatalf("vertical minor font size not inherited: %#v", got.Vertical.Minor.FontSize)+ }+}++func TestRawStyleAndDialogueTags(t *testing.T) {+ style := DefaultStyleSet()+ style.Horizontal.Major.RawASSStyle = "Style: Major,Arial,30,&H00FFFFFF,&H000000FF,&H00000000,&H64000000,-1,0,0,0,100,100,0,0,1,4,2,2,20,20,40,1"+ style.Horizontal.Major.FadeInMS = intPtr(120)+ style.Horizontal.Major.FadeOutMS = intPtr(180)+ style.Horizontal.Major.OverrideTags = `\blur1`++ header := BuildAssHeader(style, true)+ if !strings.Contains(header, style.Horizontal.Major.RawASSStyle) {+ t.Fatalf("raw style was not used:\n%s", header)+ }+ tags := DialogueTags(style.Horizontal.Major)+ if tags != `{\fad(120,180)\blur1}` {+ t.Fatalf("DialogueTags() = %q", tags)+ }+}++func intPtr(v int) *int { return &v }+func floatPtr(v float64) *float64 { return &v }