-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
276 lines (250 loc) · 10.5 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
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
/*
* Copyright (C) 2020 Mark Wu. All rights reserved.
* Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
*/
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using TimecodeUtil.Options;
using TimecodeUtil.Timecode;
namespace TimecodeUtil
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
PrintHelp(Console.Out);
return;
}
Timecode.Timecode timecode;
if (args[0] == "-")
{
timecode = new Timecode.Timecode(Console.In);
}
else
{
var inputInfo = new FileInfo(args[0]);
if (!inputInfo.Exists)
{
PrintErrorAndHelp($"Error: File not found! {inputInfo.FullName}");
return;
}
timecode = new Timecode.Timecode(inputInfo.FullName);
}
try
{
var restOpts = args.Skip(2).ToArray();
switch (args[1])
{
case "info":
InfoActionHandler(timecode, restOpts);
return;
case "convert":
ConvertActionHandler(timecode, restOpts);
return;
case "query":
QueryActionHandler(timecode, restOpts);
return;
default:
PrintErrorAndHelp($"Error: No such action! {args[1]}");
return;
}
}
catch (Exception ex)
{
Console.Error.WriteLine("Unexpected Error:");
Console.Error.WriteLine(ex.Message);
Console.Error.WriteLine(ex.StackTrace);
}
}
private static void InfoActionHandler(Timecode.Timecode timecode, string[] opts)
{
switch (opts.Length)
{
case 0:
break;
case 1 when timecode.Version == TimecodeVersion.V1 && int.TryParse(opts[0], out var length):
timecode.TotalFrames = length;
break;
default:
PrintErrorAndHelp("Error: Too many arguments!");
return;
}
Console.WriteLine(
$" {"Total Length: ",-20}{timecode.TotalLength,-15:hh\\:mm\\:ss\\.fff}{"Total Frames: ",-20}{timecode.TotalFrames}");
Console.WriteLine(
$" {"Average Frame Rate: ",-20}{timecode.AverageFrameRate,-15:F3}{"Default Frame Rate: ",-20}{timecode.DefaultFrameRate:F3}");
Console.WriteLine($"+{new string('-', 23)}+{new string('-', 23)}+{new string('-', 12)}+");
Console.WriteLine($"| {"Start Frame / Time",21} | {"End Frame / Time",21} | {"Frame Rate",10} |");
Console.WriteLine($"+{new string('-', 23)}+{new string('-', 23)}+{new string('-', 12)}+");
foreach (var interval in timecode.IntervalList)
{
Console.WriteLine(
$"| {interval.StartFrame,6} / {timecode.GetTimeSpanFromFrameNumber(interval.StartFrame),-12:hh\\:mm\\:ss\\.fff} " +
$"| {interval.EndFrame,6} / {timecode.GetTimeSpanFromFrameNumber(interval.EndFrame),-12:hh\\:mm\\:ss\\.fff} " +
$"| {1e7 / interval.Interval,10:F6} |");
}
Console.WriteLine($"+{new string('-', 23)}+{new string('-', 23)}+{new string('-', 12)}+");
}
private static void ConvertActionHandler(Timecode.Timecode timecode, string[] opts)
{
if (opts.Length == 0)
{
PrintErrorAndHelp("Error: Too few arguments!");
return;
}
var options = new ConvertOptions
{
Output = opts[0],
Version = GetInvertTimecodeVersion(timecode.Version)
};
if (opts.Length > 1)
{
if (opts[1] == "--fix" || opts[1] == "-f")
{
options.Version = timecode.Version;
switch (opts.Length)
{
case 2:
break;
case 3 when options.Version == TimecodeVersion.V1 && TryParseFrameRate(opts[2], out var fps):
options.Fps = fps;
break;
case 3 when options.Version == TimecodeVersion.V1:
PrintErrorAndHelp("Error: Can not parsing arguments");
return;
case 4 when options.Version == TimecodeVersion.V1 && TryParseFrameRate(opts[2], out var fps) &&
int.TryParse(opts[3], out var length):
timecode.TotalFrames = length;
options.Fps = fps;
break;
case 4 when options.Version == TimecodeVersion.V1:
PrintErrorAndHelp("Error: Can not parsing arguments");
return;
default:
PrintErrorAndHelp("Error: Too many arguments!");
return;
}
}
else
{
if (opts.Length > 3)
{
PrintErrorAndHelp("Error: Too many arguments!");
return;
}
switch (options.Version)
{
case TimecodeVersion.V1 when TryParseFrameRate(opts[1], out var fps):
options.Fps = fps;
break;
case TimecodeVersion.V2 when int.TryParse(opts[1], out var length):
timecode.TotalFrames = length;
break;
default:
PrintErrorAndHelp("Error: Can not parsing arguments");
return;
}
}
}
if (options.Output == "-")
{
timecode.SaveTimecode(Console.Out, options.Version, options.Fps ?? 0);
}
else
{
timecode.SaveTimecode(options.Output, options.Version, options.Fps ?? 0);
}
}
private static void QueryActionHandler(Timecode.Timecode timecode, string[] restOpts)
{
if (restOpts.Length == 0)
{
PrintErrorAndHelp("Error: Please provide at least one timespan/frame for querying!");
}
Console.WriteLine($"+{new string('-', 14)}+{new string('-', 14)}+");
Console.WriteLine($"| {"Query",12} | {"Result",12} |");
Console.WriteLine($"+{new string('-', 14)}+{new string('-', 14)}+");
foreach (var opt in restOpts)
{
if (int.TryParse(opt, out var frame))
{
Console.WriteLine(
$"| {frame,12} " +
$"| {timecode.GetTimeSpanFromFrameNumber(frame),12:hh\\:mm\\:ss\\.fff} |");
}
else if (TimeSpan.TryParse(opt, out var timeSpan))
{
Console.WriteLine(
$"| {timeSpan,12:hh\\:mm\\:ss\\.fff} " +
$"| {timecode.GetFrameNumberFromTimeSpan(timeSpan),12} |");
}
else
{
Console.WriteLine(
$"| {opt.Substring(0, Math.Min(opt.Length, 12)),12} " +
$"| {"Error!",12} |");
}
}
Console.WriteLine($"+{new string('-', 14)}+{new string('-', 14)}+");
}
private static bool TryParseFrameRate(string str, out double fps)
{
fps = default;
if (double.TryParse(str, out fps)) return true;
var fractionReg = new Regex(@"(\d+)/(\d+)");
var match = fractionReg.Match(str);
if (!match.Success) return false;
var num = int.Parse(match.Groups[1].Value);
var den = int.Parse(match.Groups[2].Value);
if (num == 0) return false;
fps = 1.0 * num / den;
return true;
}
private static TimecodeVersion GetInvertTimecodeVersion(TimecodeVersion version)
{
switch (version)
{
case TimecodeVersion.V1:
return TimecodeVersion.V2;
case TimecodeVersion.V2:
return TimecodeVersion.V1;
default:
throw new ArgumentOutOfRangeException(nameof(version), version, null);
}
}
private static void PrintErrorAndHelp(string error)
{
Console.Error.WriteLine(error);
Console.Error.WriteLine();
PrintHelp();
}
private static void PrintHelp()
{
PrintHelp(Console.Error);
}
private static void PrintHelp(TextWriter textWriter)
{
var versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
textWriter.WriteLine($"Timecode Util v{versionInfo.FileVersion}");
textWriter.WriteLine(versionInfo.LegalCopyright);
textWriter.WriteLine();
textWriter.WriteLine("Usage:");
textWriter.WriteLine("TimecodeUtil INPUT ACTION [...]");
textWriter.WriteLine();
textWriter.WriteLine("Action:");
textWriter.WriteLine("\tinfo: Show information about a timecode file");
textWriter.WriteLine("\t\tTimecodeUtil INPUT info [LENGTH]");
textWriter.WriteLine("\tconvert: Convert a timecode file");
textWriter.WriteLine("\t\tTimecodeUtil INPUT convert OUTPUT --fix [FPS(V1) [LENGTH(V1)]]");
textWriter.WriteLine("\t\tTimecodeUtil INPUT convert OUTPUT [LENGTH(V1) | FPS(V2)]");
textWriter.WriteLine("\tquery: Query the frame number or timestamp from the other");
textWriter.WriteLine("\t\tTimecodeUtil INPUT query [FRAME NUMBER | TIMESTAMP] ...");
}
}
}