-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
132 lines (120 loc) · 3.07 KB
/
index.js
File metadata and controls
132 lines (120 loc) · 3.07 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
130
131
132
import { spawn } from 'child_process';
import { consola } from 'consola';
import { lstat, readdir, stat } from 'fs/promises';
import { existsSync, mkdirSync } from 'fs';
import { parse, join, isAbsolute } from 'path';
//* Define __dirname
const __dirname = import.meta.dirname;
//* Extensions
const ExtArr = ['bmp', 'ico', 'jpg', 'svg', 'tif', 'webp', 'png'];
let repeat = 0;
/**
* mkdir (If not exists)
* @param {string} path
*/
export function mkdirIfnotExists(path) {
console.log(path);
try {
if (!existsSync(path)) {
mkdirSync(path, { recursive: true });
}
} catch (e) {
throw new Error(e);
}
}
/**
* Convert file with ffmpeg
* @param {string} path
* @param {string} Ext_to
* @returns true | Error
*/
function Convert(path, ext) {
return new Promise((resolve, reject) => {
const is = ExtArr.includes(ext);
console.log(is);
if (!is) {
throw new Error(`Unsupported extension (${ext})`);
} else {
//* Without extension
const filename = parse(path).name;
const outputDir = join(__dirname, 'output');
mkdirIfnotExists(outputDir);
const output = join(outputDir, `${filename}.${ext}`);
const cmd = 'ffmpeg';
const args = ['-i', `"${path}"`, `"${output}"`];
const cp = spawn(cmd, args, { shell: true, stdio: 'inherit' });
cp.on('close', (code) => {
if (code == 0) resolve(true);
else reject(new Error(`[FFmpeg] Failed with code ${code}`));
});
}
});
}
/**
* main function
*/
async function main() {
const pathstr = await consola.prompt('Enter the target folder (absolute path):', {
type: 'text',
});
if (!pathstr) {
if (repeat >= 1) {
process.exit(0);
}
consola.fail('Please enter the folder path');
repeat++;
return main();
}
const path = pathstr.replace(/"/g, '');
console.log(path);
console.log(isAbsolute(path));
try {
if (!(await lstat(path)).isDirectory()) {
consola.fail('That path is not directory.');
return main();
}
} catch (e) {
consola.fail('Failed to check path (incorrect path?)');
consola.error(e);
return main();
}
const ext = await consola.prompt('Choose the file extension that after converted', {
type: 'select',
required: true,
options: ExtArr,
});
// const path = join(pathStr);
consola.info(`Reading ${path}...`);
const files = await readdir(path);
let all = 0;
let success = 0;
let failed = 0;
await Promise.all(
files.map(async (file) => {
const FsStat = await stat(join(path, file));
if (FsStat.isDirectory()) {
//* Ignore child directory
return;
} else {
consola.info(`Converting ${file}...`);
all++;
try {
await Convert(join(path, file), ext);
consola.success(`Converted ${file}`);
success++;
} catch (e) {
consola.fail(`Error occured while converting ${file}`);
consola.error(e);
failed++;
}
}
})
);
consola.success(failed == 0 ? `Successfully converted ${success} files` : `Successfully converted ${success}/${all} files (${failed} files failed to convert! please check the error log.)`);
}
function kill() {
process.exit(0);
}
process.on('SIGINT', kill);
process.on('SIGTERM', kill);
main();