-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
98 lines (87 loc) · 2.95 KB
/
index.js
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
const path = require('path');
const fs = require('fs');
const { exec } = require('child_process');
class Mp3Convert {
regFileFilter = /\.wav$/i;
regArgs = /^([^\=]+)\=([^\=]+)$/;
files;
constructor() {
this.options = Object.assign({
input: './input',
output:'./output'
}, this.getProcessArgs());
this.options.input = path.join(__dirname, this.options.input);
this.options.output = path.join(__dirname, this.options.output);
}
getFiles(list, root='') {
list.forEach((p)=>{
const filePath = path.join(root, p);
if(fs.existsSync(filePath)) {
const stat = fs.lstatSync(filePath);
if(stat.isDirectory()) {
this.getFiles(fs.readdirSync(filePath), filePath);
} else if(stat.isFile() && this.regFileFilter.test(filePath)) {
this.files[path.relative(__dirname, filePath)]=path.relative(__dirname, this.getOutput(filePath));
}
}
});
}
getOutput(filePath) {
let p = path.relative(this.options.input, filePath);
let folders = path.dirname(p).split(path.sep);
folders.push(path.basename(p));
folders = folders.map((name)=>{
return name.replace(/\s|\_/g, '-').replace(/(?<!^|-)([A-Z])/g, "-$1").toLowerCase().replace(this.regFileFilter, '.mp3');
});
p = folders.join(path.sep);
let output = path.join(this.options.output, p);
let outBase = path.dirname(output);
if (!fs.existsSync(outBase)){
fs.mkdirSync(outBase, {recursive: true});
}
return output;
}
getProcessArgs = ()=> {
const args = process.argv.slice(2);
const ret = {};
args.forEach((str)=>{
const result = str.match(this.regArgs);
if(result && typeof result.length === 'number' && result.length===3) {
ret[result[1]] = result[2];
}
});
return ret;
}
exec = (input, output)=> {
return new Promise((resolve, reject)=>{
console.log(`=======lame -b320 ${input} ${output}`);
exec(`lame -b128 ${input} ${output}`, (err, stdout, stderr) => {
resolve(1);
});
});
}
convert() {
return new Promise((resolve)=>{
this.files= {};
this.getFiles(fs.readdirSync(this.options.input), this.options.input);
let keys = Object.keys(this.files);
let todo= keys.length;
let callback = ()=>{
todo--;
if(todo<=0) {
resolve(1);
}
};
keys.forEach((k)=>{
this.exec(k, this.files[k])
.finally(()=>{
callback();
});
});
});
}
}
async function run() {
await new Mp3Convert().convert();
}
run();