-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
121 lines (114 loc) · 5.81 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace WebVTT_to_ASS
{
class Program
{
public static void Main(string[] args) {
if(args.Length != 0) {
if(args[0].Equals("--version") == true) {
Console.WriteLine("1.0.0");
}
else if(args[0].Equals("--folder") == true && args.Length == 3) {
if(Directory.Exists(args[1])) {
string[] vttArray = Directory.GetFiles(args[1], "*.vtt");
if(!Directory.Exists(args[2])) {
Directory.CreateDirectory(args[2]);
}
int count = 0;
foreach (string path in vttArray) {
FileInfo fi = new FileInfo(path);
Console.WriteLine("Convert file {0}/{1} [{2}] in course...", ++count, vttArray.Length, fi.Name);
convert(path, args[2]
+ (Environment.OSVersion.Platform.ToString().Contains("Win") ? "\\" : "/")
+ fi.Name.Substring(0,fi.Name.LastIndexOf("."))
+ ".ass");
}
}
}
else if(args.Length == 2) {
try {
Console.WriteLine("Convert in course...");
convert(args[0], args[1]);
} catch(Exception) {
sendInstruction();
}
}
}
else {
sendInstruction();
}
}
private static void sendInstruction() {
Console.WriteLine("Usage:\n"
+ "vttPath assPath Convert a WebVTT file to ASS file\n"
+ "--folder vttFolder assFolder Convert WebVTT files to ASS files\n"
+ "--version See version");
}
private static void convert(string vtt, string ass) {
if (File.Exists(vtt)) {
string readText = File.ReadAllText(vtt);
try{
using (StringReader sr = new StringReader(readText))
using (StreamWriter sw = new StreamWriter(ass))
{
// ASS header
sw.WriteLine("[Script Info]");
sw.WriteLine("; Script generated by vtt2ass");
sw.WriteLine("");
sw.WriteLine("[V4+ Styles]");
sw.WriteLine("Format: Name, Fontname, Fontsize, "
+ "PrimaryColour, SecondaryColour, OutlineColour, BackColour, "
+ "Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, "
+ "BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding");
sw.WriteLine("Style: Default,Arial,28,"
+ "&H0000FFFF,&H0000FFFF,&H00000000,&H00000000,0,0,0,0,"
+ "100,100,0,0,1,2,0,2,10,10,10,0");
sw.WriteLine("");
sw.WriteLine("[Events]");
sw.WriteLine("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text");
// WebVTT parsing
string line;
string timeStart = string.Empty, timeStop = string.Empty, vttText = string.Empty;
bool editingNeeded = false;
while((line = sr.ReadLine()) != null) {
if(editingNeeded == true && line.Length > 0) {
//1
//00:00:03.100 --> 00:00:05.166 position:50.00%,middle align:middle size:80.00% line:84.67%
//<i><c.bg_transparent>Le royaume de Fiore.</c.bg_transparent></i>
if(line.StartsWith("<") == false) {
timeStart = line.Substring(1, 10);
timeStop = line.Substring(18, 10);
}
else {
if(vttText == string.Empty) {
vttText = Regex.Replace(line, "<.*?>", String.Empty);
}
else {
vttText += "\\N";
vttText += Regex.Replace(line, "<.*?>", String.Empty);
}
}
}
else if (Regex.IsMatch(line, @"^\d+") == true) {
editingNeeded = true;
}
else if(line.Length == 0 && editingNeeded == true) {
// Dialogue: 0,0:00:00.00,0:00:00.00,Default,,0,0,0,,0000000000000000
sw.WriteLine("Dialogue: 0," + timeStart + "," + timeStop + ",Default,,0,0,0,," + vttText);
timeStart = string.Empty;
timeStop = string.Empty;
vttText = string.Empty;
editingNeeded = false;
}
}
Console.WriteLine("Success!");
}
}catch(Exception){
Console.WriteLine("Error!");
}
}
}
}
}