Added translation of .srt formatting

Add support to translate bold, italic, underlining and font color (font color may be wrong, I have yet to test it extensively)
This commit is contained in:
2021-03-23 17:50:09 +01:00
committed by GitHub
parent 0efbecc80a
commit d32816489c
+39 -4
View File
@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@@ -23,11 +23,45 @@ namespace ConvertSRTto3DASS
File.WriteAllText(Path.GetFileNameWithoutExtension(args[0]) + ".ass", finished); File.WriteAllText(Path.GetFileNameWithoutExtension(args[0]) + ".ass", finished);
} }
//Is there a better way to do it?
private static Dictionary<Regex, string> regexReplacementDict =
new Dictionary<Regex, string> {
{new Regex("<b>"), "{\\b1}"},
{new Regex("(</b>)"),"{\\b0}"},
{new Regex("(<i>)"),"{\\i1}"},
{new Regex("(</i>)"),"{\\i0}"},
{new Regex("(<u>)"),"{\\u1}" },
{new Regex("(</u>)"),"{\\u0}"},
{new Regex("(</font>)"),"{\\c&HFFFFFF&}"} //TODO custom colors. When you add custom colors, it needs to change this too
//TODO: Add formatting for the string. Have to look into the SSA v4.00+ specification on how to handle it };
private static Regex color = new Regex("<font color=\"#.{6}\">");
//TODO Add positional data to the formatting.
private static string ChangeFormatting(string line) private static string ChangeFormatting(string line)
{ {
return ""; foreach (var tuple in regexReplacementDict)
{
line = tuple.Key.Replace(line, tuple.Value);
}
while (color.IsMatch(line))
{
var match = color.Match(line);
string str_match = match.Value;
var color_str = str_match.Substring(14, 6); //RGB value in HEX
//ASS uses RGB value, but in reverse order so got to reverse it
var char_array = color_str.ToCharArray();
Array.Reverse(char_array);
color_str = new string(char_array);
line = color.Replace(line, "{\\c&" + color_str + "&}");
}
return removeFormatting(line);
} }
@@ -195,7 +229,7 @@ namespace ConvertSRTto3DASS
if (line == "" | line == "\r") if (line == "" | line == "\r")
{ {
j = 0; j = 0;
converted.Add(new Tuple<string, string, string, string>(timestamp_start, timestamp_end, removeFormatting(subtitiles), "")); converted.Add(new Tuple<string, string, string, string>(timestamp_start, timestamp_end, ChangeFormatting(subtitiles), ""));
subtitiles = ""; subtitiles = "";
continue; continue;
} }
@@ -206,6 +240,7 @@ namespace ConvertSRTto3DASS
if (k != i) if (k != i)
{ {
Console.Error.WriteLine("Something went wrong"); Console.Error.WriteLine("Something went wrong");
Console.Error.WriteLine("Something wrong on line:" + linecounter);
System.Environment.Exit(-1); System.Environment.Exit(-1);
} }
else else