-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
315 lines (252 loc) · 12.1 KB
/
MainForm.cs
File metadata and controls
315 lines (252 loc) · 12.1 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace SA
{
public partial class MainForm : Form
{
private String filename_;
private delegate void changeProgressLabel(string str);
public MainForm()
{
InitializeComponent();
labelVersion.Text = "Version " + Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
filename_ = "";
}
private void MainForm_Load(object sender, EventArgs e)
{
/*
string patternTime = @"[\d][\d]:[0-5][\d]:[0-5][\d],[\d][\d][\d]";
string patternTimeLine = @"^" + patternTime + " --> " + patternTime + "$";
Regex rgxTimeLine = new Regex(patternTimeLine, RegexOptions.IgnoreCase);
Regex rgxTime = new Regex(patternTime, RegexOptions.IgnoreCase);
string text = "00:56:48,874 --> 01:24:48,874";
MatchCollection m1 = rgxTimeLine.Matches(text);
int mc1 = m1.Count;
MatchCollection m2 = rgxTime.Matches(text);
int mc2 = m2.Count;
foreach (Match match in m2)
{
string val = match.Value;
}
*/
/*
Tools.SubtitleType type;
long milli1 = Tools.TimeToMilliseconds("01:36:28,345", out type);
string time1 = Tools.MillisecondsToTime(milli1, type);
long milli2 = Tools.TimeToMilliseconds("01:36:28.34", out type);
string time2 = Tools.MillisecondsToTime(milli2, type);
string timeA1 = Tools.AdjustTime("01:36:28,345", 2, 1000); // SubRip
string timeA2 = Tools.AdjustTime("01:36:28.34", 2, 1000); // SubViewer
string timeA2b = Tools.AdjustTime("01:36:28.34", 1, 1006); // SubViewer
*/
}
#region Interface callbacks
private void buttonOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
Filter = "Subtitles files|*.srt;*.sub",
FileName = ""
};
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filename_ = openFileDialog1.FileName;
textboxFilename.Text = filename_;
// update GUI and reset values
tooltipFilename.SetToolTip(textboxFilename, filename_);
radiobuttonSpeedDelay.Checked = true;
panelAdjust.Enabled = true;
numericupdownSpeed.Value = 1;
numericupdownSpeed.Enabled = true;
numericupdownDelay.Value = 0;
numericupdownDelay.Enabled = true;
timepickerPosCur1.Value = new System.DateTime(2017, 2, 2, 0, 0, 0, 0);
timepickerPosCur2.Value = new System.DateTime(2017, 2, 2, 1, 0, 0, 0);
timepickerPosExp1.Value = new System.DateTime(2017, 2, 2, 0, 0, 0, 0);
timepickerPosExp2.Value = new System.DateTime(2017, 2, 2, 1, 0, 0, 0);
numericupdownMilliPosCur1.Value = 0;
numericupdownMilliPosCur2.Value = 0;
numericupdownMilliPosExp1.Value = 0;
numericupdownMilliPosExp2.Value = 0;
buttonSave.Enabled = false;
labelStatus.Visible = false;
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
// check if file exists
if (!File.Exists(filename_))
{
labelStatus.Text = "Error:" + Environment.NewLine;
labelStatus.Text += Environment.NewLine;
labelStatus.Text += "Subtitle file not found";
labelStatus.Visible = true;
return;
}
// get speed and delay
double speed = 1.0;
long delay = 0;
if (radiobuttonSpeedDelay.Checked)
{
speed = (double)numericupdownSpeed.Value;
delay = (long)numericupdownDelay.Value;
}
else if (radiobuttonPositions.Checked)
computeSpeedDelayFromPositions(out speed, out delay);
if (speed == 1.0 && delay == 0)
return; // secure
string line;
string adjustedSubtitles = "";
Encoding encoding = Tools.GetEncoding(filename_); // not functional
//Encoding encoding = Encoding.UTF7;
StreamReader sr = new StreamReader(filename_, encoding);
// Subtitles formats
// SubRip: hh:mm:ss,mmm --> hh:mm:ss,mmm
string patternSubRipTime = @"[\d][\d]:[0-5][\d]:[0-5][\d],[\d][\d][\d]";
string patternSubRipTimeLine = @"^" + patternSubRipTime + " --> " + patternSubRipTime + "$";
Regex rgxSubRipTimeLine = new Regex(patternSubRipTimeLine, RegexOptions.IgnoreCase);
Regex rgxSubRipTime = new Regex(patternSubRipTime, RegexOptions.IgnoreCase);
// SubViewer: hh:mm:ss.mm,hh:mm:ss.mm
string patternSubViewerTime = @"[\d][\d]:[0-5][\d]:[0-5][\d].[\d][\d]";
string patternSubViewerTimeLine = @"^" + patternSubViewerTime + "," + patternSubViewerTime + "$";
Regex rgxSubViewerTimeLine = new Regex(patternSubViewerTimeLine, RegexOptions.IgnoreCase);
Regex rgxSubViewerTime = new Regex(patternSubViewerTime, RegexOptions.IgnoreCase);
while (sr.Peek() >= 0)
{
line = sr.ReadLine();
MatchCollection matchesSubRipTimeLine = rgxSubRipTimeLine.Matches(line);
MatchCollection matchesSubViewerTimeLine = rgxSubViewerTimeLine.Matches(line);
if (matchesSubRipTimeLine.Count > 0) // SubRip format
{
MatchCollection matchesTime = rgxSubRipTime.Matches(line);
if (matchesTime.Count == 2)
{
string subtitleStart = matchesTime[0].Value;
string subtitleStop = matchesTime[1].Value;
string subtitleStartAdjusted = Tools.AdjustTime(subtitleStart, speed, delay);
string subtitleStopAdjusted = Tools.AdjustTime(subtitleStop, speed, delay);
adjustedSubtitles += subtitleStartAdjusted + " --> " + subtitleStopAdjusted + "\r\n";
}
}
else if (matchesSubViewerTimeLine.Count > 0) // SubViewer format
{
MatchCollection matchesTime = rgxSubViewerTime.Matches(line);
if (matchesTime.Count == 2)
{
string subtitleStart = matchesTime[0].Value;
string subtitleStop = matchesTime[1].Value;
string subtitleStartAdjusted = Tools.AdjustTime(subtitleStart, speed, delay);
string subtitleStopAdjusted = Tools.AdjustTime(subtitleStop, speed, delay);
adjustedSubtitles += subtitleStartAdjusted + "," + subtitleStopAdjusted + "\r\n";
}
}
else // subtitle
adjustedSubtitles += line + "\r\n";
}
sr.Close();
// save adjusted subtitles into file
string adjustedFilename = Path.GetFileNameWithoutExtension(filename_);
string extension = Path.GetExtension(filename_);
if (speed != 1.0)
adjustedFilename += "_s" + speed.ToString();
if (delay != 0.0)
adjustedFilename += "_d" + delay.ToString();
adjustedFilename += extension;
string adjustedDir = Path.GetDirectoryName(filename_);
string adjustedPath = Path.Combine(adjustedDir, adjustedFilename);
File.WriteAllText(adjustedPath, adjustedSubtitles);
labelStatus.Visible = true;
labelStatus.Text = "Adjusted subtitles saved to file:" + Environment.NewLine;
labelStatus.Text += adjustedPath;
}
private void buttonQuit_Click(object sender, EventArgs e)
{
this.Close();
}
private void radiobuttonSpeedDelay_CheckedChanged(object sender, EventArgs e)
{
bool selected = radiobuttonSpeedDelay.Checked;
panelSpeedDelay.Enabled = selected;
panelPositions.Enabled = !selected;
}
private void radiobuttonPositions_CheckedChanged(object sender, EventArgs e)
{
bool selected = radiobuttonPositions.Checked;
panelSpeedDelay.Enabled = !selected;
panelPositions.Enabled = selected;
}
// speed / delay
private void numericupdownSpeedDelay_ValueChanged(object sender, EventArgs e)
{
double speed = (double)numericupdownSpeed.Value;
long delay = (long)numericupdownDelay.Value;
bool idle = (speed == 1.0 && delay == 0);
buttonSave.Enabled = !idle;
labelStatus.Visible = false;
}
// positions
private void timepickerPosition_ValueChanged(object sender, EventArgs e)
{
// update speed and delay displayed values
computeSpeedDelayFromPositions(out double speed, out long delay);
bool idle = (speed == 1.0 && delay == 0);
labelComputedSpeed.Text = "Speed: " + speed.ToString("0.00") + "x";
labelComputedDelay.Text = "Delay: " + (delay / 1000.0).ToString("0.000") + " s";
labelComputedSpeed.Visible = !idle;
labelComputedDelay.Visible = !idle;
buttonSave.Enabled = !idle;
labelStatus.Visible = false;
}
private void numericupdownMilliseconds_KeyPress(object sender, KeyPressEventArgs e)
{
timepickerPosition_ValueChanged(sender, e);
}
/// <summary>
/// Checks for only up to 4 digits and no negatives
/// in a Numeric Up/Down box
/// </summary>
private void numericupdownMilliseconds_KeyDown(object sender, KeyEventArgs e)
{
//NumericUpDown control = (NumericUpDown)sender;
//if (!(e.KeyData == Keys.Back || e.KeyData == Keys.Delete))
//{
// // TODO: check if text is selected
// if (control.Text.Length >= 3 || e.KeyValue == 109)
// {
// e.SuppressKeyPress = true;
// e.Handled = true;
// }
//}
}
#endregion
private void computeSpeedDelayFromPositions(out double speed, out long delay)
{
string posCur1 = timepickerPosCur1.Value.ToLongTimeString() + "," + numericupdownMilliPosCur1.Value.ToString("000");
string posCur2 = timepickerPosCur2.Value.ToLongTimeString() + "," + numericupdownMilliPosCur2.Value.ToString("000");
string posExp1 = timepickerPosExp1.Value.ToLongTimeString() + "," + numericupdownMilliPosExp1.Value.ToString("000");
string posExp2 = timepickerPosExp2.Value.ToLongTimeString() + "," + numericupdownMilliPosExp2.Value.ToString("000");
Tools.SubtitleType type;
long milliCur1 = Tools.TimeToMilliseconds(posCur1, out type);
long milliCur2 = Tools.TimeToMilliseconds(posCur2, out type);
long milliExp1 = Tools.TimeToMilliseconds(posExp1, out type);
long milliExp2 = Tools.TimeToMilliseconds(posExp2, out type);
if (milliCur1 == milliCur2 || milliExp1 == milliExp2) // idle
{
speed = 1.0;
delay = 0;
return;
}
// compute speed
speed = (double)((double)(milliCur2 - milliCur1) / (double)(milliExp2 - milliExp1));
// compute delay
double invSpeed = 1.0 / speed;
double delay1 = milliExp1 - invSpeed * milliCur1;
double delay2 = milliExp2 - invSpeed * milliCur2;
delay = (long)Math.Round(0.5 * (delay1 + delay2));
}
}
}