-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOptions.cs
More file actions
129 lines (107 loc) · 3.83 KB
/
Options.cs
File metadata and controls
129 lines (107 loc) · 3.83 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
using System.ComponentModel;
using System.Xml.Serialization;
using System.Net;
using System;
using System.IO;
using CommandLine;
using CommandLine.Text;
using PrimoSoftware.AVBlocks;
namespace CliSample
{
class Options
{
// re-encode command line options
[Option('i', "input", HelpText = "input file")]
public string InputFile { get; set; }
[Option('o', "output", HelpText = "output file")]
public string OutputFile { get; set; }
[Option('a', "audio", Required = false, HelpText = "Force audio re-encoding.")]
public bool ForceAudio { get; set; }
[Option('v', "video", Required = false, HelpText = "Force video re-encoding.")]
public bool ForceVideo { get; set; }
// The program parses the command line options and sets these properties
public bool Error { get; private set; }
void ResetOptions()
{
Error = false;
InputFile = null;
OutputFile = null;
ForceAudio = false;
ForceVideo = false;
}
void SetDefaultOptions()
{
string exeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
InputFile = Path.Combine(exeDir, "../../assets/mov/big_buck_bunny_trailer.mp4");
OutputFile = Path.Combine(exeDir, "../../output/reencode/big_buck_bunny_trailer.mp4");
ForceAudio = false;
ForceVideo = false;
Console.WriteLine("Using default options: ");
Console.Write("reencode");
Console.Write(" --input " + InputFile);
Console.Write(" --output " + OutputFile);
if (ForceAudio) Console.Write(" --audio");
if (ForceVideo) Console.Write(" --video");
Console.WriteLine();
}
void SetOptions(Options other)
{
InputFile = other.InputFile;
OutputFile = other.OutputFile;
ForceAudio = other.ForceAudio;
ForceVideo = other.ForceVideo;
}
bool Validate()
{
bool res = true;
Console.Write("Input file: ");
if (InputFile == null)
{
Console.WriteLine("[not set]");
res = false;
}
else
{
Console.WriteLine(InputFile);
}
Console.Write("Output file: ");
if (InputFile == null)
{
Console.WriteLine("[not set]");
res = false;
}
else
{
Console.WriteLine(OutputFile);
}
Console.WriteLine("Re-encode audio forced: " + (ForceAudio ? "yes" : "no"));
Console.WriteLine("Re-encode video forced: " + (ForceVideo ? "yes" : "no"));
return res;
}
public bool Prepare(string[] args)
{
ResetOptions();
if (args.Length == 0)
{
SetDefaultOptions();
return true;
}
bool parseError = false;
var parserResult = Parser.Default.ParseArguments<Options>(args).WithNotParsed(_ => { parseError = true; });
if (parseError)
{
Error = true;
return false;
}
SetOptions(parserResult.Value);
if (!Validate())
{
var helpText = HelpText.AutoBuild(parserResult, h => h, e => e);
Console.WriteLine($"Usage:\n{helpText}");
Error = true;
return false;
}
return true;
}
}
}