-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbin.js
executable file
·142 lines (117 loc) · 4.18 KB
/
bin.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
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
#!/usr/bin/env node
/*
[wx-voice]
Convert audio files between Tencent apps (Weixin / Wechat, QQ) and Silk codec with other general format such as MP3 and M4A
Github: https://github.com/Ang-YC/wx-voice
Author: AngYC <me@angyc.com>
*/
// TODO: Support Windows
// TODO: Use Promise
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
const cmdArgs = require('command-line-args');
const Spinner = require('cli-spinner').Spinner;
const WxVoice = require('./index.js');
const args = cmdArgs([
{ name: "command", defaultOption: true, type: String },
{ name: "input", alias: "i", type: String },
{ name: "output", alias: "o", type: String },
{ name: "format", alias: "f", type: String },
{ name: "bitrate", type: Number },
{ name: "frequency", type: Number },
{ name: "channels", type: Number }
]);
var binSdk = path.resolve(__dirname, "silk");
sdk = path.resolve(process.cwd(), "node_modules", "wx-voice", "silk");
var cmdName = "[wx-voice] ",
cmdError = "\x1b[41m\x1b[37mERROR\x1b[0m ";
switch (args.command) {
case "decode":
case "encode":
convert(args.command, args);
break;
case "compile":
exec("make -C " + binSdk, exec_cb(args.command + "-1", function() {
if (!fs.existsSync(sdk)) return;
exec("make -C " + sdk, exec_cb(args.command + "-2"));
}));
break;
case "clean":
exec("make -C " + binSdk + " clean", exec_cb(args.command + "-1", function() {
if (!fs.existsSync(sdk)) return;
exec("make -C " + sdk + " clean", exec_cb(args.command + "-2"));
}));
break;
default:
help();
}
function convert(type, args) {
if (args.input && args.output) {
var options = {};
["format", "bitrate", "frequency", "channels"].forEach((key) => {
if (args[key])
options[key] = args[key];
});
var wxVoice = new WxVoice();
wxVoice.on("error", (err) => console.log(cmdName + cmdError + err));
wxVoice[type](args.input, args.output, options, convert_cb(type));
} else {
help();
}
}
function convert_cb(type) {
var spinner = loading(type);
spinner.start();
return function(output) {
setTimeout(function() {
spinner.stop();
console.log();
if (output == undefined) {
console.log(cmdName + cmdError + type + " not successful, file not supported");
} else {
console.log(cmdName + type + " success");
}
}, 100);
}
}
function exec_cb(type, cb) {
var spinner = loading(type);
spinner.start();
return function(e, out, err) {
setTimeout(function() {
spinner.stop();
console.log();
if (err && err.length > 0 && !err.startsWith("ar")) {
console.log(cmdName + cmdError + err);
} else {
console.log(cmdName + type + " success");
}
if (cb) cb();
}, 100);
}
}
function loading(type) {
var spinner = new Spinner(cmdName + "%s Running command: " + type + "");
spinner.setSpinnerString("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏");
return spinner;
}
function help() {
console.log("| wxVoice by AngYC | v0.2.1 |");
console.log("Usage: wx-voice <command> <options>\n");
console.log("Command:");
console.log(" decode decode to general audio format");
console.log(" encode encode from general audio format");
console.log(" compile compile wx-voice library");
console.log(" clean remove compiled library\n");
console.log("Options:");
console.log(" -i <input> input file path");
console.log(" -o <output> output file path");
console.log(" -f <format> format of the output file");
console.log(" --bitrate bitrate of the output file");
console.log(" --frequency frequency of the output file");
console.log(" --channels channels of the output file\n");
console.log("Tested format:");
console.log(" decode mp3, m4a, wav, pcm");
console.log(" encode silk, silk_amr, webm");
}