-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparseFlags.ts
121 lines (109 loc) · 2.67 KB
/
parseFlags.ts
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
import { parse } from "https://deno.land/std/flags/mod.ts";
import { FFTrimError } from "./error.ts";
import { hmsRangeToSeconds } from "./timeFormats.ts";
export type Flags = {
start: number | undefined;
end: number | undefined;
size: number | undefined;
mergeWeights: number[] | undefined;
inputFile: string;
outputFile: string;
};
export function parseFlags(): Flags {
const {
trim: trimRaw,
size: sizeRaw,
inputFile,
outputFile,
merge = "",
} = parseFlagsRaw();
const [start, end] = trimRaw === undefined ? [undefined, undefined] : hmsRangeToSeconds(trimRaw);
const audioWeightsParsed = merge === "" ? undefined : parseAudioWeights(merge);
const size = sizeRaw ? shortFormToBytes(sizeRaw) : undefined;
if (inputFile === undefined) {
throw new FFTrimError("input file is required (-i)");
}
if (outputFile === undefined) {
throw new FFTrimError("output file is required (-o)");
}
return {
start,
end,
size,
inputFile,
outputFile,
mergeWeights: audioWeightsParsed,
};
}
function shortFormToBytes(shortForm: string): number {
const lastChar = shortForm.charAt(shortForm.length - 1).toLocaleLowerCase();
// if the last character is a number, it's likely bytes
if (!isNaN(parseFloat(lastChar))) {
const num = parseInt(shortForm, 10);
if (isNaN(num)) {
throw new FFTrimError("invalid number: " + shortForm);
}
return num;
}
const shortFormNumPart = shortForm.substring(0, shortForm.length - 1);
const num = parseInt(shortFormNumPart, 10);
if (isNaN(num)) {
throw new FFTrimError("invalid number: " + shortFormNumPart);
}
switch (lastChar) {
case "b":
return num;
case "k":
return num * 1_000;
case "m":
return num * 1_000_000;
case "g":
return num * 1_000_000_000;
default:
throw new FFTrimError(
`invalid unit: ${lastChar} (must be one of {b,k,m,g})`,
);
}
}
function parseAudioWeights(weights: string): number[] {
try {
return weights.split(",").map((str) => {
const i = parseInt(str);
if (isNaN(i)) {
throw new FFTrimError("audio weights must be in the form w1,w2,w3...");
}
return i;
});
} catch (e) {
throw new FFTrimError("audio weights must be in the form w1,w2,w3...");
}
}
// returns the raw, unormalized form of all of the flags
function parseFlagsRaw() {
const flags = parse(Deno.args, {
string: [
"trim",
"t",
"input",
"i",
"output",
"o",
"size",
"s",
"merge",
"m",
],
boolean: [
"dampen",
"d",
],
});
return {
trim: flags.trim ?? flags.t,
size: flags.size ?? flags.s,
inputFile: flags.input ?? flags.i ?? flags._[0],
outputFile: flags.output ?? flags.o,
merge: flags.merge ?? flags.m,
dampenAudio: flags.dampen || flags.d,
};
}